36 import sun.nio.cs.Surrogate;
37 import static sun.nio.cs.CharsetMapping.*;
38
39 public class HKSCS {
40
41 public static class Decoder extends DoubleByte.Decoder {
42 static int b2Min = 0x40;
43 static int b2Max = 0xfe;
44
45 private char[][] b2cBmp;
46 private char[][] b2cSupp;
47 private DoubleByte.Decoder big5Dec;
48
49 protected Decoder(Charset cs,
50 DoubleByte.Decoder big5Dec,
51 char[][] b2cBmp, char[][] b2cSupp)
52 {
53 // super(cs, 0.5f, 1.0f);
54 // need to extends DoubleByte.Decoder so the
55 // sun.io can use it. this implementation
56 super(cs, 0.5f, 1.0f, null, null, 0, 0);
57 this.big5Dec = big5Dec;
58 this.b2cBmp = b2cBmp;
59 this.b2cSupp = b2cSupp;
60 }
61
62 public char decodeSingle(int b) {
63 return big5Dec.decodeSingle(b);
64 }
65
66 public char decodeBig5(int b1, int b2) {
67 return big5Dec.decodeDouble(b1, b2);
68 }
69
70 public char decodeDouble(int b1, int b2) {
71 return b2cBmp[b1][b2 - b2Min];
72 }
73
74 public char decodeDoubleEx(int b1, int b2) {
75 /* if the b2cSupp is null, the subclass need
76 to override the methold
222 for (int i = 0; i < b2cStr.length; i++) {
223 if (b2cStr[i] == null)
224 b2c[i] = DoubleByte.B2C_UNMAPPABLE;
225 else
226 b2c[i] = b2cStr[i].toCharArray();
227 }
228 }
229
230 }
231
232 public static class Encoder extends DoubleByte.Encoder {
233 private DoubleByte.Encoder big5Enc;
234 private char[][] c2bBmp;
235 private char[][] c2bSupp;
236
237 protected Encoder(Charset cs,
238 DoubleByte.Encoder big5Enc,
239 char[][] c2bBmp,
240 char[][] c2bSupp)
241 {
242 super(cs, null, null);
243 this.big5Enc = big5Enc;
244 this.c2bBmp = c2bBmp;
245 this.c2bSupp = c2bSupp;
246 }
247
248 public int encodeBig5(char ch) {
249 return big5Enc.encodeChar(ch);
250 }
251
252 public int encodeChar(char ch) {
253 int bb = c2bBmp[ch >> 8][ch & 0xff];
254 if (bb == UNMAPPABLE_ENCODING)
255 return encodeBig5(ch);
256 return bb;
257 }
258
259 public int encodeSupp(int cp) {
260 if ((cp & 0xf0000) != 0x20000)
261 return UNMAPPABLE_ENCODING;
262 return c2bSupp[(cp >> 8) & 0xff][cp & 0xff];
372 if (!Character.isHighSurrogate(c) || sp == sl ||
373 !Character.isLowSurrogate(src[sp]) ||
374 (bb = encodeSupp(Character.toCodePoint(c, src[sp++])))
375 == UNMAPPABLE_ENCODING) {
376 dst[dp++] = repl[0];
377 if (repl.length > 1)
378 dst[dp++] = repl[1];
379 continue;
380 }
381 }
382 if (bb > MAX_SINGLEBYTE) { // DoubleByte
383 dst[dp++] = (byte)(bb >> 8);
384 dst[dp++] = (byte)bb;
385 } else { // SingleByte
386 dst[dp++] = (byte)bb;
387 }
388 }
389 return dp;
390 }
391
392
393 static char[] C2B_UNMAPPABLE = new char[0x100];
394 static {
395 Arrays.fill(C2B_UNMAPPABLE, (char)UNMAPPABLE_ENCODING);
396 }
397
398 public static void initc2b(char[][] c2b, String[] b2cStr, String pua) {
399 // init c2b/c2bSupp from b2cStr and supp
400 int b2Min = 0x40;
401 Arrays.fill(c2b, C2B_UNMAPPABLE);
402 for (int b1 = 0; b1 < 0x100; b1++) {
403 String s = b2cStr[b1];
404 if (s == null)
405 continue;
406 for (int i = 0; i < s.length(); i++) {
407 char c = s.charAt(i);
408 int hi = c >> 8;
409 if (c2b[hi] == C2B_UNMAPPABLE) {
410 c2b[hi] = new char[0x100];
411 Arrays.fill(c2b[hi], (char)UNMAPPABLE_ENCODING);
|
36 import sun.nio.cs.Surrogate;
37 import static sun.nio.cs.CharsetMapping.*;
38
39 public class HKSCS {
40
41 public static class Decoder extends DoubleByte.Decoder {
42 static int b2Min = 0x40;
43 static int b2Max = 0xfe;
44
45 private char[][] b2cBmp;
46 private char[][] b2cSupp;
47 private DoubleByte.Decoder big5Dec;
48
49 protected Decoder(Charset cs,
50 DoubleByte.Decoder big5Dec,
51 char[][] b2cBmp, char[][] b2cSupp)
52 {
53 // super(cs, 0.5f, 1.0f);
54 // need to extends DoubleByte.Decoder so the
55 // sun.io can use it. this implementation
56 super(cs, 0.5f, 1.0f, null, null, 0, 0, true);
57 this.big5Dec = big5Dec;
58 this.b2cBmp = b2cBmp;
59 this.b2cSupp = b2cSupp;
60 }
61
62 public char decodeSingle(int b) {
63 return big5Dec.decodeSingle(b);
64 }
65
66 public char decodeBig5(int b1, int b2) {
67 return big5Dec.decodeDouble(b1, b2);
68 }
69
70 public char decodeDouble(int b1, int b2) {
71 return b2cBmp[b1][b2 - b2Min];
72 }
73
74 public char decodeDoubleEx(int b1, int b2) {
75 /* if the b2cSupp is null, the subclass need
76 to override the methold
222 for (int i = 0; i < b2cStr.length; i++) {
223 if (b2cStr[i] == null)
224 b2c[i] = DoubleByte.B2C_UNMAPPABLE;
225 else
226 b2c[i] = b2cStr[i].toCharArray();
227 }
228 }
229
230 }
231
232 public static class Encoder extends DoubleByte.Encoder {
233 private DoubleByte.Encoder big5Enc;
234 private char[][] c2bBmp;
235 private char[][] c2bSupp;
236
237 protected Encoder(Charset cs,
238 DoubleByte.Encoder big5Enc,
239 char[][] c2bBmp,
240 char[][] c2bSupp)
241 {
242 super(cs, null, null, true);
243 this.big5Enc = big5Enc;
244 this.c2bBmp = c2bBmp;
245 this.c2bSupp = c2bSupp;
246 }
247
248 public int encodeBig5(char ch) {
249 return big5Enc.encodeChar(ch);
250 }
251
252 public int encodeChar(char ch) {
253 int bb = c2bBmp[ch >> 8][ch & 0xff];
254 if (bb == UNMAPPABLE_ENCODING)
255 return encodeBig5(ch);
256 return bb;
257 }
258
259 public int encodeSupp(int cp) {
260 if ((cp & 0xf0000) != 0x20000)
261 return UNMAPPABLE_ENCODING;
262 return c2bSupp[(cp >> 8) & 0xff][cp & 0xff];
372 if (!Character.isHighSurrogate(c) || sp == sl ||
373 !Character.isLowSurrogate(src[sp]) ||
374 (bb = encodeSupp(Character.toCodePoint(c, src[sp++])))
375 == UNMAPPABLE_ENCODING) {
376 dst[dp++] = repl[0];
377 if (repl.length > 1)
378 dst[dp++] = repl[1];
379 continue;
380 }
381 }
382 if (bb > MAX_SINGLEBYTE) { // DoubleByte
383 dst[dp++] = (byte)(bb >> 8);
384 dst[dp++] = (byte)bb;
385 } else { // SingleByte
386 dst[dp++] = (byte)bb;
387 }
388 }
389 return dp;
390 }
391
392 public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {
393 int dp = 0;
394 int sl = sp + len;
395 int dl = dst.length;
396 while (sp < sl) {
397 char c = StringUTF16.getChar(src, sp++);
398 int bb = encodeChar(c);
399 if (bb == UNMAPPABLE_ENCODING) {
400 if (!Character.isHighSurrogate(c) || sp == sl ||
401 !Character.isLowSurrogate(StringUTF16.getChar(src,sp)) ||
402 (bb = encodeSupp(Character.toCodePoint(c, StringUTF16.getChar(src, sp++))))
403 == UNMAPPABLE_ENCODING) {
404 dst[dp++] = repl[0];
405 if (repl.length > 1)
406 dst[dp++] = repl[1];
407 continue;
408 }
409 }
410 if (bb > MAX_SINGLEBYTE) { // DoubleByte
411 dst[dp++] = (byte)(bb >> 8);
412 dst[dp++] = (byte)bb;
413 } else { // SingleByte
414 dst[dp++] = (byte)bb;
415 }
416 }
417 return dp;
418 }
419
420 static char[] C2B_UNMAPPABLE = new char[0x100];
421 static {
422 Arrays.fill(C2B_UNMAPPABLE, (char)UNMAPPABLE_ENCODING);
423 }
424
425 public static void initc2b(char[][] c2b, String[] b2cStr, String pua) {
426 // init c2b/c2bSupp from b2cStr and supp
427 int b2Min = 0x40;
428 Arrays.fill(c2b, C2B_UNMAPPABLE);
429 for (int b1 = 0; b1 < 0x100; b1++) {
430 String s = b2cStr[b1];
431 if (s == null)
432 continue;
433 for (int i = 0; i < s.length(); i++) {
434 char c = s.charAt(i);
435 int hi = c >> 8;
436 if (c2b[hi] == C2B_UNMAPPABLE) {
437 c2b[hi] = new char[0x100];
438 Arrays.fill(c2b[hi], (char)UNMAPPABLE_ENCODING);
|