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