-
Notifications
You must be signed in to change notification settings - Fork 668
/
DoubleByte.java
1107 lines (1017 loc) · 40.7 KB
/
DoubleByte.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.cs;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.util.Arrays;
import sun.nio.cs.Surrogate;
import sun.nio.cs.ArrayDecoder;
import sun.nio.cs.ArrayEncoder;
import static sun.nio.cs.CharsetMapping.*;
/*
* Four types of "DoubleByte" charsets are implemented in this class
* (1)DoubleByte
* The "mostly widely used" multibyte charset, a combination of
* a singlebyte character set (usually the ASCII charset) and a
* doublebyte character set. The codepoint values of singlebyte
* and doublebyte don't overlap. Microsoft's multibyte charsets
* and IBM's "DBCS_ASCII" charsets, such as IBM1381, 942, 943,
* 948, 949 and 950 are such charsets.
*
* (2)DoubleByte_EBCDIC
* IBM EBCDIC Mix multibyte charset. Use SO and SI to shift (switch)
* in and out between the singlebyte character set and doublebyte
* character set.
*
* (3)DoubleByte_SIMPLE_EUC
* It's a "simple" form of EUC encoding scheme, only have the
* singlebyte character set G0 and one doublebyte character set
* G1 are defined, G2 (with SS2) and G3 (with SS3) are not used.
* So it is actually the same as the "typical" type (1) mentioned
* above, except it return "malformed" for the SS2 and SS3 when
* decoding.
*
* (4)DoubleByte ONLY
* A "pure" doublebyte only character set. From implementation
* point of view, this is the type (1) with "decodeSingle" always
* returns unmappable.
*
* For simplicity, all implementations share the same decoding and
* encoding data structure.
*
* Decoding:
*
* char[][] b2c;
* char[] b2cSB;
* int b2Min, b2Max
*
* public char decodeSingle(int b) {
* return b2cSB.[b];
* }
*
* public char decodeDouble(int b1, int b2) {
* if (b2 < b2Min || b2 > b2Max)
* return UNMAPPABLE_DECODING;
* return b2c[b1][b2 - b2Min];
* }
*
* (1)b2Min, b2Max are the corresponding min and max value of the
* low-half of the double-byte.
* (2)The high 8-bit/b1 of the double-byte are used to indexed into
* b2c array.
*
* Encoding:
*
* char[] c2b;
* char[] c2bIndex;
*
* public int encodeChar(char ch) {
* return c2b[c2bIndex[ch >> 8] + (ch & 0xff)];
* }
*
*/
public class DoubleByte {
public static final char[] B2C_UNMAPPABLE;
static {
B2C_UNMAPPABLE = new char[0x100];
Arrays.fill(B2C_UNMAPPABLE, UNMAPPABLE_DECODING);
}
public static class Decoder extends CharsetDecoder
implements DelegatableDecoder, ArrayDecoder
{
final char[][] b2c;
final char[] b2cSB;
final int b2Min;
final int b2Max;
final boolean isASCIICompatible;
// for SimpleEUC override
protected CoderResult crMalformedOrUnderFlow(int b) {
return CoderResult.UNDERFLOW;
}
protected CoderResult crMalformedOrUnmappable(int b1, int b2) {
if (b2c[b1] == B2C_UNMAPPABLE || // isNotLeadingByte(b1)
b2c[b2] != B2C_UNMAPPABLE || // isLeadingByte(b2)
decodeSingle(b2) != UNMAPPABLE_DECODING) { // isSingle(b2)
return CoderResult.malformedForLength(1);
}
return CoderResult.unmappableForLength(2);
}
public Decoder(Charset cs, float avgcpb, float maxcpb,
char[][] b2c, char[] b2cSB,
int b2Min, int b2Max,
boolean isASCIICompatible) {
super(cs, avgcpb, maxcpb);
this.b2c = b2c;
this.b2cSB = b2cSB;
this.b2Min = b2Min;
this.b2Max = b2Max;
this.isASCIICompatible = isASCIICompatible;
}
public Decoder(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max,
boolean isASCIICompatible) {
this(cs, 0.5f, 1.0f, b2c, b2cSB, b2Min, b2Max, isASCIICompatible);
}
public Decoder(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
this(cs, 0.5f, 1.0f, b2c, b2cSB, b2Min, b2Max, false);
}
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl && dp < dl) {
// inline the decodeSingle/Double() for better performance
int inSize = 1;
int b1 = sa[sp] & 0xff;
char c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING) {
if (sl - sp < 2)
return crMalformedOrUnderFlow(b1);
int b2 = sa[sp + 1] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
return crMalformedOrUnmappable(b1, b2);
}
inSize++;
}
da[dp++] = c;
sp += inSize;
}
return (sp >= sl) ? CoderResult.UNDERFLOW
: CoderResult.OVERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining() && dst.hasRemaining()) {
int b1 = src.get() & 0xff;
char c = b2cSB[b1];
int inSize = 1;
if (c == UNMAPPABLE_DECODING) {
if (src.remaining() < 1)
return crMalformedOrUnderFlow(b1);
int b2 = src.get() & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING)
return crMalformedOrUnmappable(b1, b2);
inSize++;
}
dst.put(c);
mark += inSize;
}
return src.hasRemaining()? CoderResult.OVERFLOW
: CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
// Make some protected methods public for use by JISAutoDetect
public CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
if (src.hasArray() && dst.hasArray())
return decodeArrayLoop(src, dst);
else
return decodeBufferLoop(src, dst);
}
@Override
public int decode(byte[] src, int sp, int len, char[] dst) {
int dp = 0;
int sl = sp + len;
char repl = replacement().charAt(0);
while (sp < sl) {
int b1 = src[sp++] & 0xff;
char c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING) {
if (sp < sl) {
int b2 = src[sp++] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
if (crMalformedOrUnmappable(b1, b2).length() == 1) {
sp--;
}
}
}
if (c == UNMAPPABLE_DECODING) {
c = repl;
}
}
dst[dp++] = c;
}
return dp;
}
@Override
public boolean isASCIICompatible() {
return isASCIICompatible;
}
public void implReset() {
super.implReset();
}
public CoderResult implFlush(CharBuffer out) {
return super.implFlush(out);
}
// decode loops are not using decodeSingle/Double() for performance
// reason.
public char decodeSingle(int b) {
return b2cSB[b];
}
public char decodeDouble(int b1, int b2) {
if (b1 < 0 || b1 > b2c.length ||
b2 < b2Min || b2 > b2Max)
return UNMAPPABLE_DECODING;
return b2c[b1][b2 - b2Min];
}
}
// IBM_EBCDIC_DBCS
public static class Decoder_EBCDIC extends Decoder {
private static final int SBCS = 0;
private static final int DBCS = 1;
private static final int SO = 0x0e;
private static final int SI = 0x0f;
private int currentState;
public Decoder_EBCDIC(Charset cs,
char[][] b2c, char[] b2cSB, int b2Min, int b2Max,
boolean isASCIICompatible) {
super(cs, b2c, b2cSB, b2Min, b2Max, isASCIICompatible);
}
public Decoder_EBCDIC(Charset cs,
char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
super(cs, b2c, b2cSB, b2Min, b2Max, false);
}
public void implReset() {
currentState = SBCS;
}
// Check validity of dbcs ebcdic byte pair values
//
// First byte : 0x41 -- 0xFE
// Second byte: 0x41 -- 0xFE
// Doublebyte blank: 0x4040
//
// The validation implementation in "old" DBCS_IBM_EBCDIC and sun.io
// as
// if ((b1 != 0x40 || b2 != 0x40) &&
// (b2 < 0x41 || b2 > 0xfe)) {...}
// is not correct/complete (range check for b1)
//
private static boolean isDoubleByte(int b1, int b2) {
return (0x41 <= b1 && b1 <= 0xfe && 0x41 <= b2 && b2 <= 0xfe)
|| (b1 == 0x40 && b2 == 0x40); // DBCS-HOST SPACE
}
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
byte[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
char[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
// don't check dp/dl together here, it's possible to
// decdoe a SO/SI without space in output buffer.
while (sp < sl) {
int b1 = sa[sp] & 0xff;
int inSize = 1;
if (b1 == SO) { // Shift out
if (currentState != SBCS)
return CoderResult.malformedForLength(1);
else
currentState = DBCS;
} else if (b1 == SI) {
if (currentState != DBCS)
return CoderResult.malformedForLength(1);
else
currentState = SBCS;
} else {
char c = UNMAPPABLE_DECODING;
if (currentState == SBCS) {
c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING)
return CoderResult.unmappableForLength(1);
} else {
if (sl - sp < 2)
return CoderResult.UNDERFLOW;
int b2 = sa[sp + 1] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
if (!isDoubleByte(b1, b2))
return CoderResult.malformedForLength(2);
return CoderResult.unmappableForLength(2);
}
inSize++;
}
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = c;
}
sp += inSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining()) {
int b1 = src.get() & 0xff;
int inSize = 1;
if (b1 == SO) { // Shift out
if (currentState != SBCS)
return CoderResult.malformedForLength(1);
else
currentState = DBCS;
} else if (b1 == SI) {
if (currentState != DBCS)
return CoderResult.malformedForLength(1);
else
currentState = SBCS;
} else {
char c = UNMAPPABLE_DECODING;
if (currentState == SBCS) {
c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING)
return CoderResult.unmappableForLength(1);
} else {
if (src.remaining() < 1)
return CoderResult.UNDERFLOW;
int b2 = src.get()&0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
if (!isDoubleByte(b1, b2))
return CoderResult.malformedForLength(2);
return CoderResult.unmappableForLength(2);
}
inSize++;
}
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
dst.put(c);
}
mark += inSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
@Override
public int decode(byte[] src, int sp, int len, char[] dst) {
int dp = 0;
int sl = sp + len;
currentState = SBCS;
char repl = replacement().charAt(0);
while (sp < sl) {
int b1 = src[sp++] & 0xff;
if (b1 == SO) { // Shift out
if (currentState != SBCS)
dst[dp++] = repl;
else
currentState = DBCS;
} else if (b1 == SI) {
if (currentState != DBCS)
dst[dp++] = repl;
else
currentState = SBCS;
} else {
char c = UNMAPPABLE_DECODING;
if (currentState == SBCS) {
c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING)
c = repl;
} else {
if (sl == sp) {
c = repl;
} else {
int b2 = src[sp++] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
c = repl;
}
}
}
dst[dp++] = c;
}
}
return dp;
}
}
// DBCS_ONLY
public static class Decoder_DBCSONLY extends Decoder {
static final char[] b2cSB_UNMAPPABLE;
static {
b2cSB_UNMAPPABLE = new char[0x100];
Arrays.fill(b2cSB_UNMAPPABLE, UNMAPPABLE_DECODING);
}
// always returns unmappableForLenth(2) for doublebyte_only
@Override
protected CoderResult crMalformedOrUnmappable(int b1, int b2) {
return CoderResult.unmappableForLength(2);
}
public Decoder_DBCSONLY(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max,
boolean isASCIICompatible) {
super(cs, 0.5f, 1.0f, b2c, b2cSB_UNMAPPABLE, b2Min, b2Max, isASCIICompatible);
}
public Decoder_DBCSONLY(Charset cs, char[][] b2c, char[] b2cSB, int b2Min, int b2Max) {
super(cs, 0.5f, 1.0f, b2c, b2cSB_UNMAPPABLE, b2Min, b2Max, false);
}
}
// EUC_SIMPLE
// The only thing we need to "override" is to check SS2/SS3 and
// return "malformed" if found
public static class Decoder_EUC_SIM extends Decoder {
private final int SS2 = 0x8E;
private final int SS3 = 0x8F;
public Decoder_EUC_SIM(Charset cs,
char[][] b2c, char[] b2cSB, int b2Min, int b2Max,
boolean isASCIICompatible) {
super(cs, b2c, b2cSB, b2Min, b2Max, isASCIICompatible);
}
// No support provided for G2/G3 for SimpleEUC
protected CoderResult crMalformedOrUnderFlow(int b) {
if (b == SS2 || b == SS3 )
return CoderResult.malformedForLength(1);
return CoderResult.UNDERFLOW;
}
protected CoderResult crMalformedOrUnmappable(int b1, int b2) {
if (b1 == SS2 || b1 == SS3 )
return CoderResult.malformedForLength(1);
return CoderResult.unmappableForLength(2);
}
@Override
public int decode(byte[] src, int sp, int len, char[] dst) {
int dp = 0;
int sl = sp + len;
char repl = replacement().charAt(0);
while (sp < sl) {
int b1 = src[sp++] & 0xff;
char c = b2cSB[b1];
if (c == UNMAPPABLE_DECODING) {
if (sp < sl) {
int b2 = src[sp++] & 0xff;
if (b2 < b2Min || b2 > b2Max ||
(c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
if (b1 == SS2 || b1 == SS3) {
sp--;
}
c = repl;
}
} else {
c = repl;
}
}
dst[dp++] = c;
}
return dp;
}
}
public static class Encoder extends CharsetEncoder
implements ArrayEncoder
{
protected final int MAX_SINGLEBYTE = 0xff;
private final char[] c2b;
private final char[] c2bIndex;
protected Surrogate.Parser sgp;
final boolean isASCIICompatible;
public Encoder(Charset cs, char[] c2b, char[] c2bIndex) {
this(cs, c2b, c2bIndex, false);
}
public Encoder(Charset cs, char[] c2b, char[] c2bIndex, boolean isASCIICompatible) {
super(cs, 2.0f, 2.0f);
this.c2b = c2b;
this.c2bIndex = c2bIndex;
this.isASCIICompatible = isASCIICompatible;
}
public Encoder(Charset cs, float avg, float max, byte[] repl, char[] c2b, char[] c2bIndex,
boolean isASCIICompatible) {
super(cs, avg, max, repl);
this.c2b = c2b;
this.c2bIndex = c2bIndex;
this.isASCIICompatible = isASCIICompatible;
}
public boolean canEncode(char c) {
return encodeChar(c) != UNMAPPABLE_ENCODING;
}
protected Surrogate.Parser sgp() {
if (sgp == null)
sgp = new Surrogate.Parser();
return sgp;
}
protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
char c = sa[sp];
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isSurrogate(c)) {
if (sgp().parse(c, sa, sp, sl) < 0)
return sgp.error();
return sgp.unmappableResult();
}
return CoderResult.unmappableForLength(1);
}
if (bb > MAX_SINGLEBYTE) { // DoubleByte
if (dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = (byte)(bb >> 8);
da[dp++] = (byte)bb;
} else { // SingleByte
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = (byte)bb;
}
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining()) {
char c = src.get();
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isSurrogate(c)) {
if (sgp().parse(c, src) < 0)
return sgp.error();
return sgp.unmappableResult();
}
return CoderResult.unmappableForLength(1);
}
if (bb > MAX_SINGLEBYTE) { // DoubleByte
if (dst.remaining() < 2)
return CoderResult.OVERFLOW;
dst.put((byte)(bb >> 8));
dst.put((byte)(bb));
} else {
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
dst.put((byte)bb);
}
mark++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
if (src.hasArray() && dst.hasArray())
return encodeArrayLoop(src, dst);
else
return encodeBufferLoop(src, dst);
}
protected byte[] repl = replacement();
protected void implReplaceWith(byte[] newReplacement) {
repl = newReplacement;
}
@Override
public int encode(char[] src, int sp, int len, byte[] dst) {
int dp = 0;
int sl = sp + len;
int dl = dst.length;
while (sp < sl) {
char c = src[sp++];
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isHighSurrogate(c) && sp < sl &&
Character.isLowSurrogate(src[sp])) {
sp++;
}
dst[dp++] = repl[0];
if (repl.length > 1)
dst[dp++] = repl[1];
continue;
} //else
if (bb > MAX_SINGLEBYTE) { // DoubleByte
dst[dp++] = (byte)(bb >> 8);
dst[dp++] = (byte)bb;
} else { // SingleByte
dst[dp++] = (byte)bb;
}
}
return dp;
}
@Override
public int encodeFromLatin1(byte[] src, int sp, int len, byte[] dst) {
int dp = 0;
int sl = sp + len;
while (sp < sl) {
char c = (char)(src[sp++] & 0xff);
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
// no surrogate pair in latin1 string
dst[dp++] = repl[0];
if (repl.length > 1) {
dst[dp++] = repl[1];
}
continue;
} //else
if (bb > MAX_SINGLEBYTE) { // DoubleByte
dst[dp++] = (byte)(bb >> 8);
dst[dp++] = (byte)bb;
} else { // SingleByte
dst[dp++] = (byte)bb;
}
}
return dp;
}
@Override
public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {
int dp = 0;
int sl = sp + len;
while (sp < sl) {
char c = StringUTF16.getChar(src, sp++);
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isHighSurrogate(c) && sp < sl &&
Character.isLowSurrogate(StringUTF16.getChar(src, sp))) {
sp++;
}
dst[dp++] = repl[0];
if (repl.length > 1) {
dst[dp++] = repl[1];
}
continue;
} //else
if (bb > MAX_SINGLEBYTE) { // DoubleByte
dst[dp++] = (byte)(bb >> 8);
dst[dp++] = (byte)bb;
} else { // SingleByte
dst[dp++] = (byte)bb;
}
}
return dp;
}
@Override
public boolean isASCIICompatible() {
return isASCIICompatible;
}
public int encodeChar(char ch) {
return c2b[c2bIndex[ch >> 8] + (ch & 0xff)];
}
// init the c2b and c2bIndex tables from b2c.
public static void initC2B(String[] b2c, String b2cSB, String b2cNR, String c2bNR,
int b2Min, int b2Max,
char[] c2b, char[] c2bIndex)
{
Arrays.fill(c2b, (char)UNMAPPABLE_ENCODING);
int off = 0x100;
char[][] b2c_ca = new char[b2c.length][];
char[] b2cSB_ca = null;
if (b2cSB != null)
b2cSB_ca = b2cSB.toCharArray();
for (int i = 0; i < b2c.length; i++) {
if (b2c[i] == null)
continue;
b2c_ca[i] = b2c[i].toCharArray();
}
if (b2cNR != null) {
int j = 0;
while (j < b2cNR.length()) {
char b = b2cNR.charAt(j++);
char c = b2cNR.charAt(j++);
if (b < 0x100 && b2cSB_ca != null) {
if (b2cSB_ca[b] == c)
b2cSB_ca[b] = UNMAPPABLE_DECODING;
} else {
if (b2c_ca[b >> 8][(b & 0xff) - b2Min] == c)
b2c_ca[b >> 8][(b & 0xff) - b2Min] = UNMAPPABLE_DECODING;
}
}
}
if (b2cSB_ca != null) { // SingleByte
for (int b = 0; b < b2cSB_ca.length; b++) {
char c = b2cSB_ca[b];
if (c == UNMAPPABLE_DECODING)
continue;
int index = c2bIndex[c >> 8];
if (index == 0) {
index = off;
off += 0x100;
c2bIndex[c >> 8] = (char)index;
}
c2b[index + (c & 0xff)] = (char)b;
}
}
for (int b1 = 0; b1 < b2c.length; b1++) { // DoubleByte
char[] db = b2c_ca[b1];
if (db == null)
continue;
for (int b2 = b2Min; b2 <= b2Max; b2++) {
char c = db[b2 - b2Min];
if (c == UNMAPPABLE_DECODING)
continue;
int index = c2bIndex[c >> 8];
if (index == 0) {
index = off;
off += 0x100;
c2bIndex[c >> 8] = (char)index;
}
c2b[index + (c & 0xff)] = (char)((b1 << 8) | b2);
}
}
if (c2bNR != null) {
// add c->b only nr entries
for (int i = 0; i < c2bNR.length(); i += 2) {
char b = c2bNR.charAt(i);
char c = c2bNR.charAt(i + 1);
int index = (c >> 8);
if (c2bIndex[index] == 0) {
c2bIndex[index] = (char)off;
off += 0x100;
}
index = c2bIndex[index] + (c & 0xff);
c2b[index] = b;
}
}
}
}
public static class Encoder_DBCSONLY extends Encoder {
public Encoder_DBCSONLY(Charset cs, byte[] repl,
char[] c2b, char[] c2bIndex,
boolean isASCIICompatible) {
super(cs, 2.0f, 2.0f, repl, c2b, c2bIndex, isASCIICompatible);
}
public int encodeChar(char ch) {
int bb = super.encodeChar(ch);
if (bb <= MAX_SINGLEBYTE)
return UNMAPPABLE_ENCODING;
return bb;
}
}
public static class Encoder_EBCDIC extends Encoder {
static final int SBCS = 0;
static final int DBCS = 1;
static final byte SO = 0x0e;
static final byte SI = 0x0f;
protected int currentState = SBCS;
public Encoder_EBCDIC(Charset cs, char[] c2b, char[] c2bIndex,
boolean isASCIICompatible) {
super(cs, 4.0f, 5.0f, new byte[] {(byte)0x6f}, c2b, c2bIndex, isASCIICompatible);
}
protected void implReset() {
currentState = SBCS;
}
protected CoderResult implFlush(ByteBuffer out) {
if (currentState == DBCS) {
if (out.remaining() < 1)
return CoderResult.OVERFLOW;
out.put(SI);
}
implReset();
return CoderResult.UNDERFLOW;
}
protected CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
char[] sa = src.array();
int sp = src.arrayOffset() + src.position();
int sl = src.arrayOffset() + src.limit();
byte[] da = dst.array();
int dp = dst.arrayOffset() + dst.position();
int dl = dst.arrayOffset() + dst.limit();
try {
while (sp < sl) {
char c = sa[sp];
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isSurrogate(c)) {
if (sgp().parse(c, sa, sp, sl) < 0)
return sgp.error();
return sgp.unmappableResult();
}
return CoderResult.unmappableForLength(1);
}
if (bb > MAX_SINGLEBYTE) { // DoubleByte
if (currentState == SBCS) {
if (dl - dp < 1)
return CoderResult.OVERFLOW;
currentState = DBCS;
da[dp++] = SO;
}
if (dl - dp < 2)
return CoderResult.OVERFLOW;
da[dp++] = (byte)(bb >> 8);
da[dp++] = (byte)bb;
} else { // SingleByte
if (currentState == DBCS) {
if (dl - dp < 1)
return CoderResult.OVERFLOW;
currentState = SBCS;
da[dp++] = SI;
}
if (dl - dp < 1)
return CoderResult.OVERFLOW;
da[dp++] = (byte)bb;
}
sp++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(sp - src.arrayOffset());
dst.position(dp - dst.arrayOffset());
}
}
protected CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining()) {
char c = src.get();
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isSurrogate(c)) {
if (sgp().parse(c, src) < 0)
return sgp.error();
return sgp.unmappableResult();
}
return CoderResult.unmappableForLength(1);
}
if (bb > MAX_SINGLEBYTE) { // DoubleByte
if (currentState == SBCS) {
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
currentState = DBCS;
dst.put(SO);
}
if (dst.remaining() < 2)
return CoderResult.OVERFLOW;
dst.put((byte)(bb >> 8));
dst.put((byte)(bb));
} else { // Single-byte
if (currentState == DBCS) {
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
currentState = SBCS;
dst.put(SI);
}
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
dst.put((byte)bb);
}
mark++;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
@Override
public int encode(char[] src, int sp, int len, byte[] dst) {
int dp = 0;
int sl = sp + len;
while (sp < sl) {
char c = src[sp++];
int bb = encodeChar(c);
if (bb == UNMAPPABLE_ENCODING) {
if (Character.isHighSurrogate(c) && sp < sl &&
Character.isLowSurrogate(src[sp])) {
sp++;
}
dst[dp++] = repl[0];
if (repl.length > 1)
dst[dp++] = repl[1];
continue;
} //else
if (bb > MAX_SINGLEBYTE) { // DoubleByte