1 /* |
|
2 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.nio.cs.ext; |
|
27 |
|
28 import java.io.*; |
|
29 import java.nio.CharBuffer; |
|
30 import java.nio.ByteBuffer; |
|
31 import java.nio.charset.Charset; |
|
32 import java.nio.charset.CharsetDecoder; |
|
33 import java.nio.charset.CharsetEncoder; |
|
34 import java.nio.charset.CoderResult; |
|
35 import java.util.Arrays; |
|
36 import sun.nio.cs.HistoricallyNamedCharset; |
|
37 import static sun.nio.cs.CharsetMapping.*; |
|
38 |
|
39 public class EUC_TW extends Charset implements HistoricallyNamedCharset |
|
40 { |
|
41 private static final int SS2 = 0x8E; |
|
42 |
|
43 /* |
|
44 (1) EUC_TW |
|
45 Second byte of EUC_TW for cs2 is in range of |
|
46 0xA1-0xB0 for plane 1-16. According to CJKV /163, |
|
47 plane1 is coded in both cs1 and cs2. This impl |
|
48 however does not decode the codepoints of plane1 |
|
49 in cs2, so only p2-p7 and p15 are supported in cs2. |
|
50 |
|
51 Plane2 0xA2; |
|
52 Plane3 0xA3; |
|
53 Plane4 0xA4; |
|
54 Plane5 0xA5; |
|
55 Plane6 0xA6; |
|
56 Plane7 0xA7; |
|
57 Plane15 0xAF; |
|
58 |
|
59 (2) Mapping |
|
60 The fact that all supplementary characters encoded in EUC_TW are |
|
61 in 0x2xxxx range gives us the room to optimize the data tables. |
|
62 |
|
63 Decoding: |
|
64 (1) save the lower 16-bit value of all codepoints of b->c mapping |
|
65 in a String array table String[plane] b2c. |
|
66 (2) save "codepoint is supplementary" info (one bit) in a |
|
67 byte[] b2cIsSupp, so 8 codepoints (same codepoint value, different |
|
68 plane No) share one byte. |
|
69 |
|
70 Encoding: |
|
71 (1)c->b mappings are stored in |
|
72 char[]c2b/char[]c2bIndex |
|
73 char[]c2bSupp/char[]c2bIndexsupp (indexed by lower 16-bit |
|
74 (2)byte[] c2bPlane stores the "plane info" of each euc-tw codepoints, |
|
75 BMP and Supp share the low/high 4 bits of one byte. |
|
76 |
|
77 Mapping tables are stored separated in EUC_TWMapping, which |
|
78 is generated by tool. |
|
79 */ |
|
80 |
|
81 public EUC_TW() { |
|
82 super("x-EUC-TW", ExtendedCharsets.aliasesFor("x-EUC-TW")); |
|
83 } |
|
84 |
|
85 public String historicalName() { |
|
86 return "EUC_TW"; |
|
87 } |
|
88 |
|
89 public boolean contains(Charset cs) { |
|
90 return ((cs.name().equals("US-ASCII")) |
|
91 || (cs instanceof EUC_TW)); |
|
92 } |
|
93 |
|
94 public CharsetDecoder newDecoder() { |
|
95 return new Decoder(this); |
|
96 } |
|
97 |
|
98 public CharsetEncoder newEncoder() { |
|
99 return new Encoder(this); |
|
100 } |
|
101 |
|
102 public static class Decoder extends CharsetDecoder { |
|
103 public Decoder(Charset cs) { |
|
104 super(cs, 2.0f, 2.0f); |
|
105 } |
|
106 |
|
107 char[] c1 = new char[1]; |
|
108 char[] c2 = new char[2]; |
|
109 public char[] toUnicode(int b1, int b2, int p) { |
|
110 return decode(b1, b2, p, c1, c2); |
|
111 } |
|
112 |
|
113 static final String[] b2c = EUC_TWMapping.b2c; |
|
114 static final int b1Min = EUC_TWMapping.b1Min; |
|
115 static final int b1Max = EUC_TWMapping.b1Max; |
|
116 static final int b2Min = EUC_TWMapping.b2Min; |
|
117 static final int b2Max = EUC_TWMapping.b2Max; |
|
118 static final int dbSegSize = b2Max - b2Min + 1; |
|
119 static final byte[] b2cIsSupp; |
|
120 |
|
121 // adjust from cns planeNo to the plane index of b2c |
|
122 static final byte[] cnspToIndex = new byte[0x100]; |
|
123 static { |
|
124 Arrays.fill(cnspToIndex, (byte)-1); |
|
125 cnspToIndex[0xa2] = 1; cnspToIndex[0xa3] = 2; cnspToIndex[0xa4] = 3; |
|
126 cnspToIndex[0xa5] = 4; cnspToIndex[0xa6] = 5; cnspToIndex[0xa7] = 6; |
|
127 cnspToIndex[0xaf] = 7; |
|
128 } |
|
129 |
|
130 //static final BitSet b2cIsSupp; |
|
131 static { |
|
132 String b2cIsSuppStr = EUC_TWMapping.b2cIsSuppStr; |
|
133 // work on a local copy is much faster than operate |
|
134 // directly on b2cIsSupp |
|
135 byte[] flag = new byte[b2cIsSuppStr.length() << 1]; |
|
136 int off = 0; |
|
137 for (int i = 0; i < b2cIsSuppStr.length(); i++) { |
|
138 char c = b2cIsSuppStr.charAt(i); |
|
139 flag[off++] = (byte)(c >> 8); |
|
140 flag[off++] = (byte)(c & 0xff); |
|
141 } |
|
142 b2cIsSupp = flag; |
|
143 } |
|
144 |
|
145 static boolean isLegalDB(int b) { |
|
146 return b >= b1Min && b <= b1Max; |
|
147 } |
|
148 |
|
149 static char[] decode(int b1, int b2, int p, char[] c1, char[] c2) |
|
150 { |
|
151 if (b1 < b1Min || b1 > b1Max || b2 < b2Min || b2 > b2Max) |
|
152 return null; |
|
153 int index = (b1 - b1Min) * dbSegSize + b2 - b2Min; |
|
154 char c = b2c[p].charAt(index); |
|
155 if (c == UNMAPPABLE_DECODING) |
|
156 return null; |
|
157 if ((b2cIsSupp[index] & (1 << p)) == 0) { |
|
158 c1[0] = c; |
|
159 return c1; |
|
160 } else { |
|
161 c2[0] = Character.highSurrogate(0x20000 + c); |
|
162 c2[1] = Character.lowSurrogate(0x20000 + c); |
|
163 return c2; |
|
164 } |
|
165 } |
|
166 |
|
167 private CoderResult decodeArrayLoop(ByteBuffer src, |
|
168 CharBuffer dst) |
|
169 { |
|
170 byte[] sa = src.array(); |
|
171 int sp = src.arrayOffset() + src.position(); |
|
172 int sl = src.arrayOffset() + src.limit(); |
|
173 |
|
174 char[] da = dst.array(); |
|
175 int dp = dst.arrayOffset() + dst.position(); |
|
176 int dl = dst.arrayOffset() + dst.limit(); |
|
177 try { |
|
178 while (sp < sl) { |
|
179 int byte1 = sa[sp] & 0xff; |
|
180 if (byte1 == SS2) { // Codeset 2 G2 |
|
181 if ( sl - sp < 4) |
|
182 return CoderResult.UNDERFLOW; |
|
183 int cnsPlane = cnspToIndex[sa[sp + 1] & 0xff]; |
|
184 if (cnsPlane < 0) |
|
185 return CoderResult.malformedForLength(2); |
|
186 byte1 = sa[sp + 2] & 0xff; |
|
187 int byte2 = sa[sp + 3] & 0xff; |
|
188 char[] cc = toUnicode(byte1, byte2, cnsPlane); |
|
189 if (cc == null) { |
|
190 if (!isLegalDB(byte1) || !isLegalDB(byte2)) |
|
191 return CoderResult.malformedForLength(4); |
|
192 return CoderResult.unmappableForLength(4); |
|
193 } |
|
194 if (dl - dp < cc.length) |
|
195 return CoderResult.OVERFLOW; |
|
196 if (cc.length == 1) { |
|
197 da[dp++] = cc[0]; |
|
198 } else { |
|
199 da[dp++] = cc[0]; |
|
200 da[dp++] = cc[1]; |
|
201 } |
|
202 sp += 4; |
|
203 } else if (byte1 < 0x80) { // ASCII G0 |
|
204 if (dl - dp < 1) |
|
205 return CoderResult.OVERFLOW; |
|
206 da[dp++] = (char) byte1; |
|
207 sp++; |
|
208 } else { // Codeset 1 G1 |
|
209 if ( sl - sp < 2) |
|
210 return CoderResult.UNDERFLOW; |
|
211 int byte2 = sa[sp + 1] & 0xff; |
|
212 char[] cc = toUnicode(byte1, byte2, 0); |
|
213 if (cc == null) { |
|
214 if (!isLegalDB(byte1) || !isLegalDB(byte2)) |
|
215 return CoderResult.malformedForLength(1); |
|
216 return CoderResult.unmappableForLength(2); |
|
217 } |
|
218 if (dl - dp < 1) |
|
219 return CoderResult.OVERFLOW; |
|
220 da[dp++] = cc[0]; |
|
221 sp += 2; |
|
222 } |
|
223 } |
|
224 return CoderResult.UNDERFLOW; |
|
225 } finally { |
|
226 src.position(sp - src.arrayOffset()); |
|
227 dst.position(dp - dst.arrayOffset()); |
|
228 } |
|
229 } |
|
230 |
|
231 private CoderResult decodeBufferLoop(ByteBuffer src, |
|
232 CharBuffer dst) |
|
233 { |
|
234 int mark = src.position(); |
|
235 try { |
|
236 while (src.hasRemaining()) { |
|
237 int byte1 = src.get() & 0xff; |
|
238 if (byte1 == SS2) { // Codeset 2 G2 |
|
239 if ( src.remaining() < 3) |
|
240 return CoderResult.UNDERFLOW; |
|
241 int cnsPlane = cnspToIndex[src.get() & 0xff]; |
|
242 if (cnsPlane < 0) |
|
243 return CoderResult.malformedForLength(2); |
|
244 byte1 = src.get() & 0xff; |
|
245 int byte2 = src.get() & 0xff; |
|
246 char[] cc = toUnicode(byte1, byte2, cnsPlane); |
|
247 if (cc == null) { |
|
248 if (!isLegalDB(byte1) || !isLegalDB(byte2)) |
|
249 return CoderResult.malformedForLength(4); |
|
250 return CoderResult.unmappableForLength(4); |
|
251 } |
|
252 if (dst.remaining() < cc.length) |
|
253 return CoderResult.OVERFLOW; |
|
254 if (cc.length == 1) { |
|
255 dst.put(cc[0]); |
|
256 } else { |
|
257 dst.put(cc[0]); |
|
258 dst.put(cc[1]); |
|
259 } |
|
260 mark += 4; |
|
261 } else if (byte1 < 0x80) { // ASCII G0 |
|
262 if (!dst.hasRemaining()) |
|
263 return CoderResult.OVERFLOW; |
|
264 dst.put((char) byte1); |
|
265 mark++; |
|
266 } else { // Codeset 1 G1 |
|
267 if (!src.hasRemaining()) |
|
268 return CoderResult.UNDERFLOW; |
|
269 int byte2 = src.get() & 0xff; |
|
270 char[] cc = toUnicode(byte1, byte2, 0); |
|
271 if (cc == null) { |
|
272 if (!isLegalDB(byte1) || !isLegalDB(byte2)) |
|
273 return CoderResult.malformedForLength(1); |
|
274 return CoderResult.unmappableForLength(2); |
|
275 } |
|
276 if (!dst.hasRemaining()) |
|
277 return CoderResult.OVERFLOW; |
|
278 dst.put(cc[0]); |
|
279 mark +=2; |
|
280 } |
|
281 } |
|
282 return CoderResult.UNDERFLOW; |
|
283 } finally { |
|
284 src.position(mark); |
|
285 } |
|
286 } |
|
287 |
|
288 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) |
|
289 { |
|
290 if (src.hasArray() && dst.hasArray()) |
|
291 return decodeArrayLoop(src, dst); |
|
292 else |
|
293 return decodeBufferLoop(src, dst); |
|
294 } |
|
295 } |
|
296 |
|
297 public static class Encoder extends CharsetEncoder { |
|
298 private byte[] bb = new byte[4]; |
|
299 |
|
300 public Encoder(Charset cs) { |
|
301 super(cs, 4.0f, 4.0f); |
|
302 } |
|
303 |
|
304 public boolean canEncode(char c) { |
|
305 return (c <= '\u007f' || toEUC(c, bb) != -1); |
|
306 } |
|
307 |
|
308 public boolean canEncode(CharSequence cs) { |
|
309 int i = 0; |
|
310 while (i < cs.length()) { |
|
311 char c = cs.charAt(i++); |
|
312 if (Character.isHighSurrogate(c)) { |
|
313 if (i == cs.length()) |
|
314 return false; |
|
315 char low = cs.charAt(i++); |
|
316 if (!Character.isLowSurrogate(low) || toEUC(c, low, bb) == -1) |
|
317 return false; |
|
318 } else if (!canEncode(c)) { |
|
319 return false; |
|
320 } |
|
321 } |
|
322 return true; |
|
323 } |
|
324 |
|
325 public int toEUC(char hi, char low, byte[] bb) { |
|
326 return encode(hi, low, bb); |
|
327 } |
|
328 |
|
329 public int toEUC(char c, byte[] bb) { |
|
330 return encode(c, bb); |
|
331 } |
|
332 |
|
333 private CoderResult encodeArrayLoop(CharBuffer src, |
|
334 ByteBuffer dst) |
|
335 { |
|
336 char[] sa = src.array(); |
|
337 int sp = src.arrayOffset() + src.position(); |
|
338 int sl = src.arrayOffset() + src.limit(); |
|
339 |
|
340 byte[] da = dst.array(); |
|
341 int dp = dst.arrayOffset() + dst.position(); |
|
342 int dl = dst.arrayOffset() + dst.limit(); |
|
343 |
|
344 int inSize; |
|
345 int outSize; |
|
346 |
|
347 try { |
|
348 while (sp < sl) { |
|
349 char c = sa[sp]; |
|
350 inSize = 1; |
|
351 if (c < 0x80) { // ASCII |
|
352 bb[0] = (byte)c; |
|
353 outSize = 1; |
|
354 } else { |
|
355 outSize = toEUC(c, bb); |
|
356 if (outSize == -1) { |
|
357 // to check surrogates only after BMP failed |
|
358 // has the benefit of improving the BMP encoding |
|
359 // 10% faster, with the price of the slowdown of |
|
360 // supplementary character encoding. given the use |
|
361 // of supplementary characters is really rare, this |
|
362 // is something worth doing. |
|
363 if (Character.isHighSurrogate(c)) { |
|
364 if ((sp + 1) == sl) |
|
365 return CoderResult.UNDERFLOW; |
|
366 if (!Character.isLowSurrogate(sa[sp + 1])) |
|
367 return CoderResult.malformedForLength(1); |
|
368 outSize = toEUC(c, sa[sp+1], bb); |
|
369 inSize = 2; |
|
370 } else if (Character.isLowSurrogate(c)) { |
|
371 return CoderResult.malformedForLength(1); |
|
372 } |
|
373 } |
|
374 } |
|
375 if (outSize == -1) |
|
376 return CoderResult.unmappableForLength(inSize); |
|
377 if ( dl - dp < outSize) |
|
378 return CoderResult.OVERFLOW; |
|
379 for (int i = 0; i < outSize; i++) |
|
380 da[dp++] = bb[i]; |
|
381 sp += inSize; |
|
382 } |
|
383 return CoderResult.UNDERFLOW; |
|
384 } finally { |
|
385 src.position(sp - src.arrayOffset()); |
|
386 dst.position(dp - dst.arrayOffset()); |
|
387 } |
|
388 } |
|
389 |
|
390 private CoderResult encodeBufferLoop(CharBuffer src, |
|
391 ByteBuffer dst) |
|
392 { |
|
393 int outSize; |
|
394 int inSize; |
|
395 int mark = src.position(); |
|
396 |
|
397 try { |
|
398 while (src.hasRemaining()) { |
|
399 inSize = 1; |
|
400 char c = src.get(); |
|
401 if (c < 0x80) { // ASCII |
|
402 outSize = 1; |
|
403 bb[0] = (byte)c; |
|
404 } else { |
|
405 outSize = toEUC(c, bb); |
|
406 if (outSize == -1) { |
|
407 if (Character.isHighSurrogate(c)) { |
|
408 if (!src.hasRemaining()) |
|
409 return CoderResult.UNDERFLOW; |
|
410 char c2 = src.get(); |
|
411 if (!Character.isLowSurrogate(c2)) |
|
412 return CoderResult.malformedForLength(1); |
|
413 outSize = toEUC(c, c2, bb); |
|
414 inSize = 2; |
|
415 } else if (Character.isLowSurrogate(c)) { |
|
416 return CoderResult.malformedForLength(1); |
|
417 } |
|
418 } |
|
419 } |
|
420 if (outSize == -1) |
|
421 return CoderResult.unmappableForLength(inSize); |
|
422 if (dst.remaining() < outSize) |
|
423 return CoderResult.OVERFLOW; |
|
424 for (int i = 0; i < outSize; i++) |
|
425 dst.put(bb[i]); |
|
426 mark += inSize; |
|
427 } |
|
428 return CoderResult.UNDERFLOW; |
|
429 } finally { |
|
430 src.position(mark); |
|
431 } |
|
432 } |
|
433 |
|
434 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) |
|
435 { |
|
436 if (src.hasArray() && dst.hasArray()) |
|
437 return encodeArrayLoop(src, dst); |
|
438 else |
|
439 return encodeBufferLoop(src, dst); |
|
440 } |
|
441 |
|
442 static int encode(char hi, char low, byte[] bb) { |
|
443 int c = Character.toCodePoint(hi, low); |
|
444 if ((c & 0xf0000) != 0x20000) |
|
445 return -1; |
|
446 c -= 0x20000; |
|
447 int index = c2bSuppIndex[c >> 8]; |
|
448 if (index == UNMAPPABLE_ENCODING) |
|
449 return -1; |
|
450 index = index + (c & 0xff); |
|
451 int db = c2bSupp[index]; |
|
452 if (db == UNMAPPABLE_ENCODING) |
|
453 return -1; |
|
454 int p = (c2bPlane[index] >> 4) & 0xf; |
|
455 bb[0] = (byte)SS2; |
|
456 bb[1] = (byte)(0xa0 | p); |
|
457 bb[2] = (byte)(db >> 8); |
|
458 bb[3] = (byte)db; |
|
459 return 4; |
|
460 } |
|
461 |
|
462 static int encode(char c, byte[] bb) { |
|
463 int index = c2bIndex[c >> 8]; |
|
464 if (index == UNMAPPABLE_ENCODING) |
|
465 return -1; |
|
466 index = index + (c & 0xff); |
|
467 int db = c2b[index]; |
|
468 if (db == UNMAPPABLE_ENCODING) |
|
469 return -1; |
|
470 int p = c2bPlane[index] & 0xf; |
|
471 if (p == 0) { |
|
472 bb[0] = (byte)(db >> 8); |
|
473 bb[1] = (byte)db; |
|
474 return 2; |
|
475 } else { |
|
476 bb[0] = (byte)SS2; |
|
477 bb[1] = (byte)(0xa0 | p); |
|
478 bb[2] = (byte)(db >> 8); |
|
479 bb[3] = (byte)db; |
|
480 return 4; |
|
481 } |
|
482 } |
|
483 |
|
484 static final char[] c2b; |
|
485 static final char[] c2bIndex; |
|
486 static final char[] c2bSupp; |
|
487 static final char[] c2bSuppIndex; |
|
488 static final byte[] c2bPlane; |
|
489 static { |
|
490 int b1Min = Decoder.b1Min; |
|
491 int b1Max = Decoder.b1Max; |
|
492 int b2Min = Decoder.b2Min; |
|
493 int b2Max = Decoder.b2Max; |
|
494 int dbSegSize = Decoder.dbSegSize; |
|
495 String[] b2c = Decoder.b2c; |
|
496 byte[] b2cIsSupp = Decoder.b2cIsSupp; |
|
497 |
|
498 c2bIndex = EUC_TWMapping.c2bIndex; |
|
499 c2bSuppIndex = EUC_TWMapping.c2bSuppIndex; |
|
500 char[] c2b0 = new char[EUC_TWMapping.C2BSIZE]; |
|
501 char[] c2bSupp0 = new char[EUC_TWMapping.C2BSUPPSIZE]; |
|
502 byte[] c2bPlane0 = new byte[Math.max(EUC_TWMapping.C2BSIZE, |
|
503 EUC_TWMapping.C2BSUPPSIZE)]; |
|
504 |
|
505 Arrays.fill(c2b0, (char)UNMAPPABLE_ENCODING); |
|
506 Arrays.fill(c2bSupp0, (char)UNMAPPABLE_ENCODING); |
|
507 |
|
508 for (int p = 0; p < b2c.length; p++) { |
|
509 String db = b2c[p]; |
|
510 /* |
|
511 adjust the "plane" from 0..7 to 0, 2, 3, 4, 5, 6, 7, 0xf, |
|
512 which helps balance between footprint (to save the plane |
|
513 info in 4 bits) and runtime performance (to require only |
|
514 one operation "0xa0 | plane" to encode the plane byte) |
|
515 */ |
|
516 int plane = p; |
|
517 if (plane == 7) |
|
518 plane = 0xf; |
|
519 else if (plane != 0) |
|
520 plane = p + 1; |
|
521 |
|
522 int off = 0; |
|
523 for (int b1 = b1Min; b1 <= b1Max; b1++) { |
|
524 for (int b2 = b2Min; b2 <= b2Max; b2++) { |
|
525 char c = db.charAt(off); |
|
526 if (c != UNMAPPABLE_DECODING) { |
|
527 if ((b2cIsSupp[off] & (1 << p)) != 0) { |
|
528 int index = c2bSuppIndex[c >> 8] + (c&0xff); |
|
529 c2bSupp0[index] = (char)((b1 << 8) + b2); |
|
530 c2bPlane0[index] |= (byte)(plane << 4); |
|
531 } else { |
|
532 int index = c2bIndex[c >> 8] + (c&0xff); |
|
533 c2b0[index] = (char)((b1 << 8) + b2); |
|
534 c2bPlane0[index] |= (byte)plane; |
|
535 } |
|
536 } |
|
537 off++; |
|
538 } |
|
539 } |
|
540 } |
|
541 c2b = c2b0; |
|
542 c2bSupp = c2bSupp0; |
|
543 c2bPlane = c2bPlane0; |
|
544 } |
|
545 } |
|
546 } |
|