Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/sun/nio/cs/HKSCS.java
67862 views
1
/*
2
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.nio.cs;
27
28
import java.nio.ByteBuffer;
29
import java.nio.CharBuffer;
30
import java.nio.charset.Charset;
31
import java.nio.charset.CharsetDecoder;
32
import java.nio.charset.CharsetEncoder;
33
import java.nio.charset.CoderResult;
34
import java.util.Arrays;
35
import sun.nio.cs.DoubleByte;
36
import sun.nio.cs.Surrogate;
37
import static sun.nio.cs.CharsetMapping.*;
38
39
public class HKSCS {
40
41
public static class Decoder extends DoubleByte.Decoder {
42
static int b2Min = 0x40;
43
static int b2Max = 0xfe;
44
45
private final char[][] b2cBmp;
46
private final char[][] b2cSupp;
47
private final DoubleByte.Decoder big5Dec;
48
49
protected Decoder(Charset cs,
50
DoubleByte.Decoder big5Dec,
51
char[][] b2cBmp, char[][] b2cSupp)
52
{
53
// super(cs, 0.5f, 1.0f);
54
// need to extends DoubleByte.Decoder so the
55
// sun.io can use it. this implementation
56
super(cs, 0.5f, 1.0f, null, null, 0, 0, true);
57
this.big5Dec = big5Dec;
58
this.b2cBmp = b2cBmp;
59
this.b2cSupp = b2cSupp;
60
}
61
62
public char decodeSingle(int b) {
63
return big5Dec.decodeSingle(b);
64
}
65
66
public char decodeBig5(int b1, int b2) {
67
return big5Dec.decodeDouble(b1, b2);
68
}
69
70
public char decodeDouble(int b1, int b2) {
71
return b2cBmp[b1][b2 - b2Min];
72
}
73
74
public char decodeDoubleEx(int b1, int b2) {
75
/* if the b2cSupp is null, the subclass need
76
to override the methold
77
if (b2cSupp == null)
78
return UNMAPPABLE_DECODING;
79
*/
80
return b2cSupp[b1][b2 - b2Min];
81
}
82
83
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
84
byte[] sa = src.array();
85
int sp = src.arrayOffset() + src.position();
86
int sl = src.arrayOffset() + src.limit();
87
88
char[] da = dst.array();
89
int dp = dst.arrayOffset() + dst.position();
90
int dl = dst.arrayOffset() + dst.limit();
91
92
try {
93
while (sp < sl) {
94
int b1 = sa[sp] & 0xff;
95
char c = decodeSingle(b1);
96
int inSize = 1, outSize = 1;
97
if (c == UNMAPPABLE_DECODING) {
98
if (sl - sp < 2)
99
return CoderResult.UNDERFLOW;
100
int b2 = sa[sp + 1] & 0xff;
101
inSize++;
102
if (b2 < b2Min || b2 > b2Max)
103
return CoderResult.unmappableForLength(2);
104
c = decodeDouble(b1, b2); //bmp
105
if (c == UNMAPPABLE_DECODING) {
106
c = decodeDoubleEx(b1, b2); //supp
107
if (c == UNMAPPABLE_DECODING) {
108
c = decodeBig5(b1, b2); //big5
109
if (c == UNMAPPABLE_DECODING)
110
return CoderResult.unmappableForLength(2);
111
} else {
112
// supplementary character in u+2xxxx area
113
outSize = 2;
114
}
115
}
116
}
117
if (dl - dp < outSize)
118
return CoderResult.OVERFLOW;
119
if (outSize == 2) {
120
// supplementary characters
121
da[dp++] = Surrogate.high(0x20000 + c);
122
da[dp++] = Surrogate.low(0x20000 + c);
123
} else {
124
da[dp++] = c;
125
}
126
sp += inSize;
127
}
128
return CoderResult.UNDERFLOW;
129
} finally {
130
src.position(sp - src.arrayOffset());
131
dst.position(dp - dst.arrayOffset());
132
}
133
}
134
135
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
136
int mark = src.position();
137
try {
138
while (src.hasRemaining()) {
139
int b1 = src.get() & 0xff;
140
int inSize = 1, outSize = 1;
141
char c = decodeSingle(b1);
142
if (c == UNMAPPABLE_DECODING) {
143
if (src.remaining() < 1)
144
return CoderResult.UNDERFLOW;
145
int b2 = src.get() & 0xff;
146
inSize++;
147
if (b2 < b2Min || b2 > b2Max)
148
return CoderResult.unmappableForLength(2);
149
c = decodeDouble(b1, b2); //bmp
150
if (c == UNMAPPABLE_DECODING) {
151
c = decodeDoubleEx(b1, b2); //supp
152
if (c == UNMAPPABLE_DECODING) {
153
c = decodeBig5(b1, b2); //big5
154
if (c == UNMAPPABLE_DECODING)
155
return CoderResult.unmappableForLength(2);
156
} else {
157
outSize = 2;
158
}
159
}
160
}
161
if (dst.remaining() < outSize)
162
return CoderResult.OVERFLOW;
163
if (outSize == 2) {
164
dst.put(Surrogate.high(0x20000 + c));
165
dst.put(Surrogate.low(0x20000 + c));
166
} else {
167
dst.put(c);
168
}
169
mark += inSize;
170
}
171
return CoderResult.UNDERFLOW;
172
} finally {
173
src.position(mark);
174
}
175
}
176
177
public int decode(byte[] src, int sp, int len, char[] dst) {
178
int dp = 0;
179
int sl = sp + len;
180
char repl = replacement().charAt(0);
181
while (sp < sl) {
182
int b1 = src[sp++] & 0xff;
183
char c = decodeSingle(b1);
184
if (c == UNMAPPABLE_DECODING) {
185
if (sl == sp) {
186
c = repl;
187
} else {
188
int b2 = src[sp++] & 0xff;
189
if (b2 < b2Min || b2 > b2Max) {
190
c = repl;
191
} else if ((c = decodeDouble(b1, b2)) == UNMAPPABLE_DECODING) {
192
c = decodeDoubleEx(b1, b2); //supp
193
if (c == UNMAPPABLE_DECODING) {
194
c = decodeBig5(b1, b2); //big5
195
if (c == UNMAPPABLE_DECODING)
196
c = repl;
197
} else {
198
// supplementary character in u+2xxxx area
199
dst[dp++] = Surrogate.high(0x20000 + c);
200
dst[dp++] = Surrogate.low(0x20000 + c);
201
continue;
202
}
203
}
204
}
205
}
206
dst[dp++] = c;
207
}
208
return dp;
209
}
210
211
public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
212
if (src.hasArray() && dst.hasArray())
213
return decodeArrayLoop(src, dst);
214
else
215
return decodeBufferLoop(src, dst);
216
}
217
218
public static void initb2c(char[][]b2c, String[] b2cStr)
219
{
220
for (int i = 0; i < b2cStr.length; i++) {
221
if (b2cStr[i] == null)
222
b2c[i] = DoubleByte.B2C_UNMAPPABLE;
223
else
224
b2c[i] = b2cStr[i].toCharArray();
225
}
226
}
227
228
}
229
230
public static class Encoder extends DoubleByte.Encoder {
231
private final DoubleByte.Encoder big5Enc;
232
private final char[][] c2bBmp;
233
private final char[][] c2bSupp;
234
235
protected Encoder(Charset cs,
236
DoubleByte.Encoder big5Enc,
237
char[][] c2bBmp,
238
char[][] c2bSupp)
239
{
240
super(cs, null, null, true);
241
this.big5Enc = big5Enc;
242
this.c2bBmp = c2bBmp;
243
this.c2bSupp = c2bSupp;
244
}
245
246
public int encodeBig5(char ch) {
247
return big5Enc.encodeChar(ch);
248
}
249
250
public int encodeChar(char ch) {
251
int bb = c2bBmp[ch >> 8][ch & 0xff];
252
if (bb == UNMAPPABLE_ENCODING)
253
return encodeBig5(ch);
254
return bb;
255
}
256
257
public int encodeSupp(int cp) {
258
if ((cp & 0xf0000) != 0x20000)
259
return UNMAPPABLE_ENCODING;
260
return c2bSupp[(cp >> 8) & 0xff][cp & 0xff];
261
}
262
263
public boolean canEncode(char c) {
264
return encodeChar(c) != UNMAPPABLE_ENCODING;
265
}
266
267
protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
268
char[] sa = src.array();
269
int sp = src.arrayOffset() + src.position();
270
int sl = src.arrayOffset() + src.limit();
271
272
byte[] da = dst.array();
273
int dp = dst.arrayOffset() + dst.position();
274
int dl = dst.arrayOffset() + dst.limit();
275
276
try {
277
while (sp < sl) {
278
char c = sa[sp];
279
int inSize = 1;
280
int bb = encodeChar(c);
281
if (bb == UNMAPPABLE_ENCODING) {
282
if (Character.isSurrogate(c)) {
283
int cp;
284
if ((cp = sgp().parse(c, sa, sp, sl)) < 0)
285
return sgp.error();
286
bb = encodeSupp(cp);
287
if (bb == UNMAPPABLE_ENCODING)
288
return CoderResult.unmappableForLength(2);
289
inSize = 2;
290
} else {
291
return CoderResult.unmappableForLength(1);
292
}
293
}
294
if (bb > MAX_SINGLEBYTE) { // DoubleByte
295
if (dl - dp < 2)
296
return CoderResult.OVERFLOW;
297
da[dp++] = (byte)(bb >> 8);
298
da[dp++] = (byte)bb;
299
} else { // SingleByte
300
if (dl - dp < 1)
301
return CoderResult.OVERFLOW;
302
da[dp++] = (byte)bb;
303
}
304
sp += inSize;
305
}
306
return CoderResult.UNDERFLOW;
307
} finally {
308
src.position(sp - src.arrayOffset());
309
dst.position(dp - dst.arrayOffset());
310
}
311
}
312
313
protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
314
int mark = src.position();
315
try {
316
while (src.hasRemaining()) {
317
int inSize = 1;
318
char c = src.get();
319
int bb = encodeChar(c);
320
if (bb == UNMAPPABLE_ENCODING) {
321
if (Character.isSurrogate(c)) {
322
int cp;
323
if ((cp = sgp().parse(c, src)) < 0)
324
return sgp.error();
325
bb = encodeSupp(cp);
326
if (bb == UNMAPPABLE_ENCODING)
327
return CoderResult.unmappableForLength(2);
328
inSize = 2;
329
} else {
330
return CoderResult.unmappableForLength(1);
331
}
332
}
333
if (bb > MAX_SINGLEBYTE) { // DoubleByte
334
if (dst.remaining() < 2)
335
return CoderResult.OVERFLOW;
336
dst.put((byte)(bb >> 8));
337
dst.put((byte)(bb));
338
} else {
339
if (dst.remaining() < 1)
340
return CoderResult.OVERFLOW;
341
dst.put((byte)bb);
342
}
343
mark += inSize;
344
}
345
return CoderResult.UNDERFLOW;
346
} finally {
347
src.position(mark);
348
}
349
}
350
351
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
352
if (src.hasArray() && dst.hasArray())
353
return encodeArrayLoop(src, dst);
354
else
355
return encodeBufferLoop(src, dst);
356
}
357
358
private byte[] repl = replacement();
359
protected void implReplaceWith(byte[] newReplacement) {
360
repl = newReplacement;
361
}
362
363
public int encode(char[] src, int sp, int len, byte[] dst) {
364
int dp = 0;
365
int sl = sp + len;
366
while (sp < sl) {
367
char c = src[sp++];
368
int bb = encodeChar(c);
369
if (bb == UNMAPPABLE_ENCODING) {
370
if (!Character.isHighSurrogate(c) || sp == sl ||
371
!Character.isLowSurrogate(src[sp]) ||
372
(bb = encodeSupp(Character.toCodePoint(c, src[sp++])))
373
== UNMAPPABLE_ENCODING) {
374
dst[dp++] = repl[0];
375
if (repl.length > 1)
376
dst[dp++] = repl[1];
377
continue;
378
}
379
}
380
if (bb > MAX_SINGLEBYTE) { // DoubleByte
381
dst[dp++] = (byte)(bb >> 8);
382
dst[dp++] = (byte)bb;
383
} else { // SingleByte
384
dst[dp++] = (byte)bb;
385
}
386
}
387
return dp;
388
}
389
390
public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {
391
int dp = 0;
392
int sl = sp + len;
393
int dl = dst.length;
394
while (sp < sl) {
395
char c = StringUTF16.getChar(src, sp++);
396
int bb = encodeChar(c);
397
if (bb == UNMAPPABLE_ENCODING) {
398
if (!Character.isHighSurrogate(c) || sp == sl ||
399
!Character.isLowSurrogate(StringUTF16.getChar(src,sp)) ||
400
(bb = encodeSupp(Character.toCodePoint(c, StringUTF16.getChar(src, sp++))))
401
== UNMAPPABLE_ENCODING) {
402
dst[dp++] = repl[0];
403
if (repl.length > 1)
404
dst[dp++] = repl[1];
405
continue;
406
}
407
}
408
if (bb > MAX_SINGLEBYTE) { // DoubleByte
409
dst[dp++] = (byte)(bb >> 8);
410
dst[dp++] = (byte)bb;
411
} else { // SingleByte
412
dst[dp++] = (byte)bb;
413
}
414
}
415
return dp;
416
}
417
418
static char[] C2B_UNMAPPABLE = new char[0x100];
419
static {
420
Arrays.fill(C2B_UNMAPPABLE, (char)UNMAPPABLE_ENCODING);
421
}
422
423
public static void initc2b(char[][] c2b, String[] b2cStr, String pua) {
424
// init c2b/c2bSupp from b2cStr and supp
425
int b2Min = 0x40;
426
Arrays.fill(c2b, C2B_UNMAPPABLE);
427
for (int b1 = 0; b1 < 0x100; b1++) {
428
String s = b2cStr[b1];
429
if (s == null)
430
continue;
431
for (int i = 0; i < s.length(); i++) {
432
char c = s.charAt(i);
433
if (c == UNMAPPABLE_DECODING)
434
continue;
435
int hi = c >> 8;
436
if (c2b[hi] == C2B_UNMAPPABLE) {
437
c2b[hi] = new char[0x100];
438
Arrays.fill(c2b[hi], (char)UNMAPPABLE_ENCODING);
439
}
440
c2b[hi][c & 0xff] = (char)((b1 << 8) | (i + b2Min));
441
}
442
}
443
if (pua != null) { // add the compatibility pua entries
444
char c = '\ue000'; //first pua character
445
for (int i = 0; i < pua.length(); i++) {
446
char bb = pua.charAt(i);
447
if (bb != UNMAPPABLE_DECODING) {
448
int hi = c >> 8;
449
if (c2b[hi] == C2B_UNMAPPABLE) {
450
c2b[hi] = new char[0x100];
451
Arrays.fill(c2b[hi], (char)UNMAPPABLE_ENCODING);
452
}
453
c2b[hi][c & 0xff] = bb;
454
}
455
c++;
456
}
457
}
458
}
459
}
460
}
461
462