1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.giraph.utils; |
19 | |
|
20 | |
import java.io.DataInput; |
21 | |
import java.io.IOException; |
22 | |
import java.io.UTFDataFormatException; |
23 | |
import io.netty.buffer.ByteBuf; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class DynamicChannelBufferInputStream implements DataInput { |
29 | |
|
30 | |
private ByteBuf buffer; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 0 | public DynamicChannelBufferInputStream(ByteBuf buffer) { |
38 | 0 | this.buffer = buffer; |
39 | 0 | } |
40 | |
|
41 | |
@Override |
42 | |
public void readFully(byte[] b) throws IOException { |
43 | 0 | buffer.readBytes(b); |
44 | 0 | } |
45 | |
|
46 | |
@Override |
47 | |
public void readFully(byte[] b, int off, int len) throws IOException { |
48 | 0 | buffer.readBytes(b, off, len); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public int skipBytes(int n) throws IOException { |
53 | 0 | buffer.skipBytes(n); |
54 | 0 | return n; |
55 | |
} |
56 | |
|
57 | |
@Override |
58 | |
public boolean readBoolean() throws IOException { |
59 | 0 | int ch = buffer.readByte(); |
60 | 0 | if (ch < 0) { |
61 | 0 | throw new IllegalStateException("readBoolean: Got " + ch); |
62 | |
} |
63 | 0 | return ch != 0; |
64 | |
} |
65 | |
|
66 | |
@Override |
67 | |
public byte readByte() throws IOException { |
68 | 0 | return buffer.readByte(); |
69 | |
} |
70 | |
|
71 | |
@Override |
72 | |
public int readUnsignedByte() throws IOException { |
73 | 0 | return buffer.readUnsignedByte(); |
74 | |
} |
75 | |
|
76 | |
@Override |
77 | |
public short readShort() throws IOException { |
78 | 0 | return buffer.readShort(); |
79 | |
} |
80 | |
|
81 | |
@Override |
82 | |
public int readUnsignedShort() throws IOException { |
83 | 0 | return buffer.readUnsignedShort(); |
84 | |
} |
85 | |
|
86 | |
@Override |
87 | |
public char readChar() throws IOException { |
88 | 0 | return buffer.readChar(); |
89 | |
} |
90 | |
|
91 | |
@Override |
92 | |
public int readInt() throws IOException { |
93 | 0 | return buffer.readInt(); |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public long readLong() throws IOException { |
98 | 0 | return buffer.readLong(); |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public float readFloat() throws IOException { |
103 | 0 | return buffer.readFloat(); |
104 | |
} |
105 | |
|
106 | |
@Override |
107 | |
public double readDouble() throws IOException { |
108 | 0 | return buffer.readDouble(); |
109 | |
} |
110 | |
|
111 | |
@Override |
112 | |
public String readLine() throws IOException { |
113 | |
|
114 | 0 | char[] buf = new char[128]; |
115 | |
|
116 | 0 | int room = buf.length; |
117 | 0 | int offset = 0; |
118 | |
int c; |
119 | |
|
120 | |
loop: |
121 | |
while (true) { |
122 | 0 | c = buffer.readByte(); |
123 | 0 | switch (c) { |
124 | |
case -1: |
125 | |
case '\n': |
126 | 0 | break loop; |
127 | |
case '\r': |
128 | 0 | int c2 = buffer.readByte(); |
129 | 0 | if ((c2 != '\n') && (c2 != -1)) { |
130 | 0 | buffer.readerIndex(buffer.readerIndex() - 1); |
131 | |
} |
132 | |
break loop; |
133 | |
default: |
134 | 0 | if (--room < 0) { |
135 | 0 | char[] replacebuf = new char[offset + 128]; |
136 | 0 | room = replacebuf.length - offset - 1; |
137 | 0 | System.arraycopy(buf, 0, replacebuf, 0, offset); |
138 | 0 | buf = replacebuf; |
139 | |
} |
140 | 0 | buf[offset++] = (char) c; |
141 | 0 | break; |
142 | |
} |
143 | |
} |
144 | 0 | if ((c == -1) && (offset == 0)) { |
145 | 0 | return null; |
146 | |
} |
147 | 0 | return String.copyValueOf(buf, 0, offset); |
148 | |
} |
149 | |
|
150 | |
@Override |
151 | |
public String readUTF() throws IOException { |
152 | |
|
153 | 0 | int utflen = buffer.readUnsignedShort(); |
154 | |
|
155 | 0 | byte[] bytearr = new byte[utflen]; |
156 | 0 | char[] chararr = new char[utflen]; |
157 | |
|
158 | |
int c; |
159 | |
int char2; |
160 | |
int char3; |
161 | 0 | int count = 0; |
162 | 0 | int chararrCount = 0; |
163 | |
|
164 | 0 | buffer.readBytes(bytearr, 0, utflen); |
165 | |
|
166 | 0 | while (count < utflen) { |
167 | 0 | c = (int) bytearr[count] & 0xff; |
168 | 0 | if (c > 127) { |
169 | 0 | break; |
170 | |
} |
171 | 0 | count++; |
172 | 0 | chararr[chararrCount++] = (char) c; |
173 | |
} |
174 | |
|
175 | 0 | while (count < utflen) { |
176 | 0 | c = (int) bytearr[count] & 0xff; |
177 | 0 | switch (c >> 4) { |
178 | |
case 0: |
179 | |
case 1: |
180 | |
case 2: |
181 | |
case 3: |
182 | |
case 4: |
183 | |
case 5: |
184 | |
case 6: |
185 | |
case 7: |
186 | |
|
187 | 0 | count++; |
188 | 0 | chararr[chararrCount++] = (char) c; |
189 | 0 | break; |
190 | |
case 12: |
191 | |
case 13: |
192 | |
|
193 | 0 | count += 2; |
194 | 0 | if (count > utflen) { |
195 | 0 | throw new UTFDataFormatException( |
196 | |
"malformed input: partial character at end"); |
197 | |
} |
198 | 0 | char2 = (int) bytearr[count - 1]; |
199 | 0 | if ((char2 & 0xC0) != 0x80) { |
200 | 0 | throw new UTFDataFormatException( |
201 | |
"malformed input around byte " + count); |
202 | |
} |
203 | 0 | chararr[chararrCount++] = (char) (((c & 0x1F) << 6) | |
204 | |
(char2 & 0x3F)); |
205 | 0 | break; |
206 | |
case 14: |
207 | |
|
208 | 0 | count += 3; |
209 | 0 | if (count > utflen) { |
210 | 0 | throw new UTFDataFormatException( |
211 | |
"malformed input: partial character at end"); |
212 | |
} |
213 | 0 | char2 = (int) bytearr[count - 2]; |
214 | 0 | char3 = (int) bytearr[count - 1]; |
215 | 0 | if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) { |
216 | 0 | throw new UTFDataFormatException( |
217 | |
"malformed input around byte " + (count - 1)); |
218 | |
} |
219 | 0 | chararr[chararrCount++] = (char) (((c & 0x0F) << 12) | |
220 | |
((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); |
221 | 0 | break; |
222 | |
default: |
223 | |
|
224 | 0 | throw new UTFDataFormatException( |
225 | |
"malformed input around byte " + count); |
226 | |
} |
227 | |
} |
228 | |
|
229 | 0 | return new String(chararr, 0, chararrCount); |
230 | |
} |
231 | |
} |