56 |
{ |
{ |
57 |
private static final char REPLACEMENT_CHARACTER = '\uFFFD'; |
private static final char REPLACEMENT_CHARACTER = '\uFFFD'; |
58 |
|
|
59 |
|
// If we're operating in stream mode and we encounter a surrogate pair that |
60 |
|
// we can't fit in the output buffer, we use this field to store the |
61 |
|
// second half of the surrogate pair. |
62 |
|
private int pendingChar = -1; |
63 |
|
|
64 |
/*************************************************************************/ |
/*************************************************************************/ |
65 |
|
|
66 |
/* |
/* |
86 |
charsInByteArray(byte[] buf, int offset, int len) |
charsInByteArray(byte[] buf, int offset, int len) |
87 |
{ |
{ |
88 |
int more = 0; |
int more = 0; |
89 |
|
int val = 0; |
90 |
int num_chars = 0; |
int num_chars = 0; |
91 |
|
|
92 |
for (int i = offset; i < offset + len; i++) |
for (int i = offset; i < offset + len; i++) |
106 |
num_chars++; // ? |
num_chars++; // ? |
107 |
else |
else |
108 |
{ |
{ |
109 |
|
val <<= 6; |
110 |
|
val |= b & 0x3F; |
111 |
more--; |
more--; |
112 |
if (more == 0) |
if (more == 0) |
113 |
|
{ |
114 |
num_chars++; |
num_chars++; |
115 |
|
if (val >= 0x10000) |
116 |
|
num_chars++; |
117 |
|
} |
118 |
} |
} |
119 |
} |
} |
120 |
else |
else |
122 |
if (more != 0) |
if (more != 0) |
123 |
num_chars++; // ? |
num_chars++; // ? |
124 |
|
|
125 |
if ((b & 0xF0) == 0xE0) |
if ((b & 0xF8) == 0xF0) |
126 |
|
{ |
127 |
|
val = b & 0x07; |
128 |
|
more = 3; |
129 |
|
} |
130 |
|
else if ((b & 0xF0) == 0xE0) |
131 |
|
{ |
132 |
|
val = b & 0x0F; |
133 |
more = 2; |
more = 2; |
134 |
|
} |
135 |
else if ((b & 0xE0) == 0xC0) |
else if ((b & 0xE0) == 0xC0) |
136 |
|
{ |
137 |
|
val = b & 0x1F; |
138 |
more = 1; |
more = 1; |
139 |
|
} |
140 |
else |
else |
141 |
num_chars++; // ? |
num_chars++; // ? |
142 |
} |
} |
178 |
val |= b & 0x3F; |
val |= b & 0x3F; |
179 |
more--; |
more--; |
180 |
if (more == 0) |
if (more == 0) |
181 |
cbuf[cbuf_offset++] = (char)val; |
{ |
182 |
|
if (val < 0x10000) |
183 |
|
cbuf[cbuf_offset++] = (char)val; |
184 |
|
else |
185 |
|
{ |
186 |
|
val -= 0x10000; |
187 |
|
cbuf[cbuf_offset++] = (char)(0xD800 + (val >> 10)); |
188 |
|
cbuf[cbuf_offset++] = (char)(0xDC00 + (val & 0x3FF)); |
189 |
|
} |
190 |
|
} |
191 |
} |
} |
192 |
} |
} |
193 |
else |
else |
195 |
if (more != 0) |
if (more != 0) |
196 |
cbuf[cbuf_offset++] = REPLACEMENT_CHARACTER; |
cbuf[cbuf_offset++] = REPLACEMENT_CHARACTER; |
197 |
|
|
198 |
if ((b & 0xF0) == 0xE0) |
if ((b & 0xF8) == 0xF0) |
199 |
|
{ |
200 |
|
val = b & 0x07; |
201 |
|
more = 3; |
202 |
|
} |
203 |
|
else if ((b & 0xF0) == 0xE0) |
204 |
{ |
{ |
205 |
val = b & 0x0F; |
val = b & 0x0F; |
206 |
more = 2; |
more = 2; |
230 |
int more = 0; |
int more = 0; |
231 |
int val = 0; |
int val = 0; |
232 |
|
|
233 |
|
if (pendingChar != -1 && len > 0) |
234 |
|
{ |
235 |
|
cbuf[offset++] = (char)pendingChar; |
236 |
|
pendingChar = -1; |
237 |
|
} |
238 |
|
|
239 |
while (offset < start_offset + len) |
while (offset < start_offset + len) |
240 |
{ |
{ |
241 |
int b = in.read(); |
int b = in.read(); |
265 |
val |= b & 0x3F; |
val |= b & 0x3F; |
266 |
more--; |
more--; |
267 |
if (more == 0) |
if (more == 0) |
268 |
cbuf[offset++] = (char)val; |
{ |
269 |
|
if (val < 0x10000) |
270 |
|
cbuf[offset++] = (char)val; |
271 |
|
else |
272 |
|
{ |
273 |
|
val -= 0x10000; |
274 |
|
cbuf[offset++] = (char)(0xD800 + (val >> 10)); |
275 |
|
if (offset < start_offset + len) |
276 |
|
cbuf[offset++] = (char)(0xDC00 + (val & 0x3FF)); |
277 |
|
else |
278 |
|
pendingChar = (char)(0xDC00 + (val & 0x3FF)); |
279 |
|
} |
280 |
|
} |
281 |
} |
} |
282 |
} |
} |
283 |
else |
else |
285 |
if (more != 0) |
if (more != 0) |
286 |
cbuf[offset++] = REPLACEMENT_CHARACTER; |
cbuf[offset++] = REPLACEMENT_CHARACTER; |
287 |
|
|
288 |
if ((b & 0xF0) == 0xE0) |
if ((b & 0xF8) == 0xF0) |
289 |
|
{ |
290 |
|
val = b & 0x07; |
291 |
|
more = 3; |
292 |
|
} |
293 |
|
else if ((b & 0xF0) == 0xE0) |
294 |
{ |
{ |
295 |
val = b & 0x0F; |
val = b & 0x0F; |
296 |
more = 2; |
more = 2; |