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