author | mgronlun |
Mon, 09 Sep 2019 13:26:35 +0200 | |
branch | JEP-349-branch |
changeset 58049 | 10ecdb5d3574 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2009, 2010, 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 |
||
29119
f680ba340556
8073893: Enable charsets build system to configure euc_tw into java.base module/sun.nio.cs
sherman
parents:
25859
diff
changeset
|
26 |
package $PACKAGE$; |
2 | 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; |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
37 |
import static sun.nio.cs.CharsetMapping.*; |
2 | 38 |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
39 |
public class EUC_TW extends Charset implements HistoricallyNamedCharset |
2 | 40 |
{ |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
41 |
private static final int SS2 = 0x8E; |
2 | 42 |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
43 |
/* |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
44 |
(1) EUC_TW |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
45 |
Second byte of EUC_TW for cs2 is in range of |
2 | 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; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
58 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
59 |
(2) Mapping |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
60 |
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
|
61 |
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
|
62 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
63 |
Decoding: |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
64 |
(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
|
65 |
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
|
66 |
(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
|
67 |
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
|
68 |
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
|
69 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
70 |
Encoding: |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
71 |
(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
|
72 |
char[]c2b/char[]c2bIndex |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
73 |
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
|
74 |
(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
|
75 |
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
|
76 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
77 |
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
|
78 |
is generated by tool. |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
79 |
*/ |
2 | 80 |
|
81 |
public EUC_TW() { |
|
29119
f680ba340556
8073893: Enable charsets build system to configure euc_tw into java.base module/sun.nio.cs
sherman
parents:
25859
diff
changeset
|
82 |
super("x-EUC-TW", $ALIASES$); |
2 | 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 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
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
|
110 |
return decode(b1, b2, p, c1, c2); |
2 | 111 |
} |
112 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
113 |
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
|
114 |
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
|
115 |
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
|
116 |
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
|
117 |
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
|
118 |
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
|
119 |
static final byte[] b2cIsSupp; |
2 | 120 |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
121 |
// 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
|
122 |
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
|
123 |
static { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
cnspToIndex[0xaf] = 7; |
2 | 128 |
} |
129 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
130 |
//static final BitSet b2cIsSupp; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
131 |
static { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
132 |
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
|
133 |
// 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
|
134 |
// directly on b2cIsSupp |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
135 |
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
|
136 |
int off = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
137 |
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
|
138 |
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
|
139 |
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
|
140 |
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
|
141 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
142 |
b2cIsSupp = flag; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
143 |
} |
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 |
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
|
146 |
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
|
147 |
} |
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 |
static char[] decode(int b1, int b2, int p, char[] c1, char[] c2) |
2 | 150 |
{ |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
151 |
if (b1 < b1Min || b1 > b1Max || b2 < b2Min || b2 > b2Max) |
2 | 152 |
return null; |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
if (c == UNMAPPABLE_DECODING) |
2 | 156 |
return null; |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
157 |
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
|
158 |
c1[0] = c; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
159 |
return c1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
160 |
} else { |
5991
288afdbbca28
6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents:
5986
diff
changeset
|
161 |
c2[0] = Character.highSurrogate(0x20000 + c); |
288afdbbca28
6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents:
5986
diff
changeset
|
162 |
c2[1] = Character.lowSurrogate(0x20000 + c); |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
163 |
return c2; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
164 |
} |
2 | 165 |
} |
166 |
||
167 |
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
|
168 |
CharBuffer dst) |
2 | 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) { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
179 |
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
|
180 |
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
|
181 |
if ( sl - sp < 4) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
182 |
return CoderResult.UNDERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
183 |
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
|
184 |
if (cnsPlane < 0) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
185 |
return CoderResult.malformedForLength(2); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
186 |
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
|
187 |
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
|
188 |
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
|
189 |
if (cc == null) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
190 |
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
|
191 |
return CoderResult.malformedForLength(4); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
192 |
return CoderResult.unmappableForLength(4); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
193 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
194 |
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
|
195 |
return CoderResult.OVERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
196 |
if (cc.length == 1) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
197 |
da[dp++] = cc[0]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
198 |
} else { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
199 |
da[dp++] = cc[0]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
200 |
da[dp++] = cc[1]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
201 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
202 |
sp += 4; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
203 |
} else if (byte1 < 0x80) { // ASCII G0 |
2 | 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; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
211 |
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
|
212 |
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
|
213 |
if (cc == null) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
214 |
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
|
215 |
return CoderResult.malformedForLength(1); |
2 | 216 |
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
|
217 |
} |
2 | 218 |
if (dl - dp < 1) |
219 |
return CoderResult.OVERFLOW; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
220 |
da[dp++] = cc[0]; |
2 | 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()) { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
if ( src.remaining() < 3) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
240 |
return CoderResult.UNDERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
241 |
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
|
242 |
if (cnsPlane < 0) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
243 |
return CoderResult.malformedForLength(2); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
244 |
byte1 = src.get() & 0xff; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
245 |
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
|
246 |
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
|
247 |
if (cc == null) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
248 |
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
|
249 |
return CoderResult.malformedForLength(4); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
250 |
return CoderResult.unmappableForLength(4); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
251 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
252 |
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
|
253 |
return CoderResult.OVERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
254 |
if (cc.length == 1) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
255 |
dst.put(cc[0]); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
256 |
} else { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
257 |
dst.put(cc[0]); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
258 |
dst.put(cc[1]); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
259 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
260 |
mark += 4; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
261 |
} else if (byte1 < 0x80) { // ASCII G0 |
2 | 262 |
if (!dst.hasRemaining()) |
263 |
return CoderResult.OVERFLOW; |
|
264 |
dst.put((char) byte1); |
|
265 |
mark++; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
266 |
} else { // Codeset 1 G1 |
2 | 267 |
if (!src.hasRemaining()) |
268 |
return CoderResult.UNDERFLOW; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
269 |
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
|
270 |
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
|
271 |
if (cc == null) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
272 |
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
|
273 |
return CoderResult.malformedForLength(1); |
2 | 274 |
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
|
275 |
} |
2 | 276 |
if (!dst.hasRemaining()) |
277 |
return CoderResult.OVERFLOW; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
278 |
dst.put(cc[0]); |
2 | 279 |
mark +=2; |
280 |
} |
|
281 |
} |
|
282 |
return CoderResult.UNDERFLOW; |
|
283 |
} finally { |
|
284 |
src.position(mark); |
|
285 |
} |
|
286 |
} |
|
287 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
288 |
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) |
2 | 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 { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
298 |
private byte[] bb = new byte[4]; |
2 | 299 |
|
300 |
public Encoder(Charset cs) { |
|
301 |
super(cs, 4.0f, 4.0f); |
|
302 |
} |
|
303 |
||
304 |
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
|
305 |
return (c <= '\u007f' || toEUC(c, bb) != -1); |
2 | 306 |
} |
307 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
308 |
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
|
309 |
int i = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
310 |
while (i < cs.length()) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
311 |
char c = cs.charAt(i++); |
3714 | 312 |
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
|
313 |
if (i == cs.length()) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
314 |
return false; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
315 |
char low = cs.charAt(i++); |
3714 | 316 |
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
|
317 |
return false; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
318 |
} else if (!canEncode(c)) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
319 |
return false; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
320 |
} |
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 |
return true; |
2 | 323 |
} |
324 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
325 |
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
|
326 |
return encode(hi, low, bb); |
2 | 327 |
} |
328 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
329 |
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
|
330 |
return encode(c, bb); |
2 | 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(); |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
339 |
|
2 | 340 |
byte[] da = dst.array(); |
341 |
int dp = dst.arrayOffset() + dst.position(); |
|
342 |
int dl = dst.arrayOffset() + dst.limit(); |
|
343 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
344 |
int inSize; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
345 |
int outSize; |
2 | 346 |
|
347 |
try { |
|
348 |
while (sp < sl) { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
349 |
char c = sa[sp]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
350 |
inSize = 1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
351 |
if (c < 0x80) { // ASCII |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
352 |
bb[0] = (byte)c; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
353 |
outSize = 1; |
2 | 354 |
} else { |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
355 |
outSize = toEUC(c, bb); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
356 |
if (outSize == -1) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
357 |
// 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
|
358 |
// 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
|
359 |
// 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
|
360 |
// 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
|
361 |
// 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
|
362 |
// is something worth doing. |
3714 | 363 |
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
|
364 |
if ((sp + 1) == sl) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
365 |
return CoderResult.UNDERFLOW; |
3714 | 366 |
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
|
367 |
return CoderResult.malformedForLength(1); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
368 |
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
|
369 |
inSize = 2; |
3714 | 370 |
} 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
|
371 |
return CoderResult.malformedForLength(1); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
372 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
373 |
} |
2 | 374 |
} |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
375 |
if (outSize == -1) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
376 |
return CoderResult.unmappableForLength(inSize); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
377 |
if ( dl - dp < outSize) |
2 | 378 |
return CoderResult.OVERFLOW; |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
379 |
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
|
380 |
da[dp++] = bb[i]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
381 |
sp += inSize; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
382 |
} |
2 | 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 |
{ |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
393 |
int outSize; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
394 |
int inSize; |
2 | 395 |
int mark = src.position(); |
396 |
||
397 |
try { |
|
398 |
while (src.hasRemaining()) { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
399 |
inSize = 1; |
2 | 400 |
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
|
401 |
if (c < 0x80) { // ASCII |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
402 |
outSize = 1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
403 |
bb[0] = (byte)c; |
2 | 404 |
} else { |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
405 |
outSize = toEUC(c, bb); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
406 |
if (outSize == -1) { |
3714 | 407 |
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
|
408 |
if (!src.hasRemaining()) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
409 |
return CoderResult.UNDERFLOW; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
410 |
char c2 = src.get(); |
3714 | 411 |
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
|
412 |
return CoderResult.malformedForLength(1); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
413 |
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
|
414 |
inSize = 2; |
3714 | 415 |
} 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
|
416 |
return CoderResult.malformedForLength(1); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
417 |
} |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
418 |
} |
2 | 419 |
} |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
420 |
if (outSize == -1) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
421 |
return CoderResult.unmappableForLength(inSize); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
422 |
if (dst.remaining() < outSize) |
2 | 423 |
return CoderResult.OVERFLOW; |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
424 |
for (int i = 0; i < outSize; i++) |
5785
5dfabe612d10
6959197: When building with JAVAC_MAX_WARNINGS=true, the build fails in sun/nio/cs due to the use of -Werror
andrew
parents:
5506
diff
changeset
|
425 |
dst.put(bb[i]); |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
426 |
mark += inSize; |
2 | 427 |
} |
428 |
return CoderResult.UNDERFLOW; |
|
429 |
} finally { |
|
430 |
src.position(mark); |
|
431 |
} |
|
432 |
} |
|
433 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
434 |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) |
2 | 435 |
{ |
436 |
if (src.hasArray() && dst.hasArray()) |
|
437 |
return encodeArrayLoop(src, dst); |
|
438 |
else |
|
439 |
return encodeBufferLoop(src, dst); |
|
440 |
} |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
441 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
442 |
static int encode(char hi, char low, byte[] bb) { |
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5785
diff
changeset
|
443 |
int c = Character.toCodePoint(hi, low); |
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
444 |
if ((c & 0xf0000) != 0x20000) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
445 |
return -1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
446 |
c -= 0x20000; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
447 |
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
|
448 |
if (index == UNMAPPABLE_ENCODING) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
449 |
return -1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
450 |
index = index + (c & 0xff); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
451 |
int db = c2bSupp[index]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
452 |
if (db == UNMAPPABLE_ENCODING) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
453 |
return -1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
454 |
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
|
455 |
bb[0] = (byte)SS2; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
456 |
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
|
457 |
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
|
458 |
bb[3] = (byte)db; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
459 |
return 4; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
460 |
} |
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 |
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
|
463 |
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
|
464 |
if (index == UNMAPPABLE_ENCODING) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
465 |
return -1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
466 |
index = index + (c & 0xff); |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
467 |
int db = c2b[index]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
468 |
if (db == UNMAPPABLE_ENCODING) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
469 |
return -1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
470 |
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
|
471 |
if (p == 0) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
472 |
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
|
473 |
bb[1] = (byte)db; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
474 |
return 2; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
475 |
} else { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
476 |
bb[0] = (byte)SS2; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
477 |
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
|
478 |
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
|
479 |
bb[3] = (byte)db; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
480 |
return 4; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
481 |
} |
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 |
static final char[] c2b; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
485 |
static final char[] c2bIndex; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
486 |
static final char[] c2bSupp; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
487 |
static final char[] c2bSuppIndex; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
488 |
static final byte[] c2bPlane; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
489 |
static { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
490 |
int b1Min = Decoder.b1Min; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
491 |
int b1Max = Decoder.b1Max; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
492 |
int b2Min = Decoder.b2Min; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
493 |
int b2Max = Decoder.b2Max; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
494 |
int dbSegSize = Decoder.dbSegSize; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
495 |
String[] b2c = Decoder.b2c; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
496 |
byte[] b2cIsSupp = Decoder.b2cIsSupp; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
497 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
498 |
c2bIndex = EUC_TWMapping.c2bIndex; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
499 |
c2bSuppIndex = EUC_TWMapping.c2bSuppIndex; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
500 |
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
|
501 |
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
|
502 |
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
|
503 |
EUC_TWMapping.C2BSUPPSIZE)]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
504 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
505 |
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
|
506 |
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
|
507 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
508 |
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
|
509 |
String db = b2c[p]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
510 |
/* |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
511 |
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
|
512 |
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
|
513 |
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
|
514 |
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
|
515 |
*/ |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
516 |
int plane = p; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
517 |
if (plane == 7) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
518 |
plane = 0xf; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
519 |
else if (plane != 0) |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
520 |
plane = p + 1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
521 |
|
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
522 |
int off = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
523 |
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
|
524 |
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
|
525 |
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
|
526 |
if (c != UNMAPPABLE_DECODING) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
527 |
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
|
528 |
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
|
529 |
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
|
530 |
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
|
531 |
} else { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
532 |
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
|
533 |
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
|
534 |
c2bPlane0[index] |= (byte)plane; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
535 |
} |
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 |
off++; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
538 |
} |
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 |
c2b = c2b0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
542 |
c2bSupp = c2bSupp0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
543 |
c2bPlane = c2bPlane0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
544 |
} |
2 | 545 |
} |
546 |
} |