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