1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.utils; |
20 | |
|
21 | |
import com.esotericsoftware.kryo.io.Input; |
22 | |
|
23 | |
import java.io.IOException; |
24 | |
import java.io.UTFDataFormatException; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
@edu.umd.cs.findbugs.annotations.SuppressWarnings( |
31 | |
"RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") |
32 | |
public abstract class UnsafeReads extends Input implements ExtendedDataInput { |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public UnsafeReads(int length) { |
40 | 0 | limit = length; |
41 | 0 | } |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public UnsafeReads(long offset, int length) { |
50 | 0 | position = (int) offset; |
51 | 0 | limit = length; |
52 | 0 | } |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public abstract int available(); |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public abstract int getPos(); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
@Override |
74 | |
protected int require(int requiredBytes) { |
75 | 0 | if (available() < requiredBytes) { |
76 | 0 | throw new IndexOutOfBoundsException("require: Only " + |
77 | 0 | available() + " bytes remaining, trying to read " + requiredBytes); |
78 | |
} |
79 | 0 | return available(); |
80 | |
} |
81 | |
|
82 | |
@Override |
83 | |
public int skipBytes(int n) { |
84 | 0 | require(n); |
85 | 0 | position += n; |
86 | 0 | return n; |
87 | |
} |
88 | |
|
89 | |
@Override |
90 | |
public String readLine() throws IOException { |
91 | |
|
92 | 0 | char[] tmpBuf = new char[128]; |
93 | |
|
94 | 0 | int room = tmpBuf.length; |
95 | 0 | int offset = 0; |
96 | |
int c; |
97 | |
|
98 | |
loop: |
99 | |
while (true) { |
100 | 0 | c = readByte(); |
101 | 0 | switch (c) { |
102 | |
case -1: |
103 | |
case '\n': |
104 | 0 | break loop; |
105 | |
case '\r': |
106 | 0 | int c2 = readByte(); |
107 | 0 | if ((c2 != '\n') && (c2 != -1)) { |
108 | 0 | position -= 1; |
109 | |
} |
110 | |
break loop; |
111 | |
default: |
112 | 0 | if (--room < 0) { |
113 | 0 | char[] replacebuf = new char[offset + 128]; |
114 | 0 | room = replacebuf.length - offset - 1; |
115 | 0 | System.arraycopy(tmpBuf, 0, replacebuf, 0, offset); |
116 | 0 | tmpBuf = replacebuf; |
117 | |
} |
118 | 0 | tmpBuf[offset++] = (char) c; |
119 | 0 | break; |
120 | |
} |
121 | |
} |
122 | 0 | if ((c == -1) && (offset == 0)) { |
123 | 0 | return null; |
124 | |
} |
125 | 0 | return String.copyValueOf(tmpBuf, 0, offset); |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
public String readUTF() throws IOException { |
130 | |
|
131 | 0 | int utflen = readUnsignedShort(); |
132 | |
|
133 | 0 | byte[] bytearr = new byte[utflen]; |
134 | 0 | char[] chararr = new char[utflen]; |
135 | |
|
136 | |
int c; |
137 | |
int char2; |
138 | |
int char3; |
139 | 0 | int count = 0; |
140 | 0 | int chararrCount = 0; |
141 | |
|
142 | 0 | readFully(bytearr, 0, utflen); |
143 | |
|
144 | 0 | while (count < utflen) { |
145 | 0 | c = (int) bytearr[count] & 0xff; |
146 | 0 | if (c > 127) { |
147 | 0 | break; |
148 | |
} |
149 | 0 | count++; |
150 | 0 | chararr[chararrCount++] = (char) c; |
151 | |
} |
152 | |
|
153 | 0 | while (count < utflen) { |
154 | 0 | c = (int) bytearr[count] & 0xff; |
155 | 0 | switch (c >> 4) { |
156 | |
case 0: |
157 | |
case 1: |
158 | |
case 2: |
159 | |
case 3: |
160 | |
case 4: |
161 | |
case 5: |
162 | |
case 6: |
163 | |
case 7: |
164 | |
|
165 | 0 | count++; |
166 | 0 | chararr[chararrCount++] = (char) c; |
167 | 0 | break; |
168 | |
case 12: |
169 | |
case 13: |
170 | |
|
171 | 0 | count += 2; |
172 | 0 | if (count > utflen) { |
173 | 0 | throw new UTFDataFormatException( |
174 | |
"malformed input: partial character at end"); |
175 | |
} |
176 | 0 | char2 = (int) bytearr[count - 1]; |
177 | 0 | if ((char2 & 0xC0) != 0x80) { |
178 | 0 | throw new UTFDataFormatException( |
179 | |
"malformed input around byte " + count); |
180 | |
} |
181 | 0 | chararr[chararrCount++] = (char) (((c & 0x1F) << 6) | |
182 | |
(char2 & 0x3F)); |
183 | 0 | break; |
184 | |
case 14: |
185 | |
|
186 | 0 | count += 3; |
187 | 0 | if (count > utflen) { |
188 | 0 | throw new UTFDataFormatException( |
189 | |
"malformed input: partial character at end"); |
190 | |
} |
191 | 0 | char2 = (int) bytearr[count - 2]; |
192 | 0 | char3 = (int) bytearr[count - 1]; |
193 | 0 | if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) { |
194 | 0 | throw new UTFDataFormatException( |
195 | |
"malformed input around byte " + (count - 1)); |
196 | |
} |
197 | 0 | chararr[chararrCount++] = (char) (((c & 0x0F) << 12) | |
198 | |
((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); |
199 | 0 | break; |
200 | |
default: |
201 | |
|
202 | 0 | throw new UTFDataFormatException( |
203 | |
"malformed input around byte " + count); |
204 | |
} |
205 | |
} |
206 | |
|
207 | 0 | return new String(chararr, 0, chararrCount); |
208 | |
} |
209 | |
} |