jdk/src/share/classes/sun/nio/cs/CharsetMapping.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 688 5ebcfc3cebe4
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 688
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
     4
 *
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
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: 688
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 688
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    10
 *
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    15
 * accompanied this code).
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    16
 *
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 688
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 688
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 688
diff changeset
    23
 * questions.
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    24
 */
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    25
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    26
package sun.nio.cs;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    27
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    28
import java.io.InputStream;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    29
import java.io.InputStreamReader;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    30
import java.io.OutputStream;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    31
import java.io.BufferedReader;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    32
import java.io.IOException;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    33
import java.util.regex.Matcher;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    34
import java.util.regex.Pattern;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    35
import java.util.*;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    36
import java.security.*;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    37
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    38
public class CharsetMapping {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    39
    public final static char UNMAPPABLE_DECODING = '\uFFFD';
688
5ebcfc3cebe4 6710199: SJIS_0213 does not handle "unmappable" encoding operation correctly
sherman
parents: 400
diff changeset
    40
    public final static int  UNMAPPABLE_ENCODING = 0xFFFD;
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    41
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    42
    char[] b2cSB;                //singlebyte b->c
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    43
    char[] b2cDB1;               //dobulebyte b->c /db1
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    44
    char[] b2cDB2;               //dobulebyte b->c /db2
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    45
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    46
    int    b2Min, b2Max;         //min/max(start/end) value of 2nd byte
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    47
    int    b1MinDB1, b1MaxDB1;   //min/Max(start/end) value of 1st byte/db1
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    48
    int    b1MinDB2, b1MaxDB2;   //min/Max(start/end) value of 1st byte/db2
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    49
    int    dbSegSize;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    50
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    51
    char[] c2b;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    52
    char[] c2bIndex;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    53
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    54
    // Supplementary
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    55
    char[] b2cSupp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    56
    char[] c2bSupp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    57
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    58
    // Composite
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    59
    Entry[] b2cComp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    60
    Entry[] c2bComp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    61
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    62
    public char decodeSingle(int b) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    63
        return b2cSB[b];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    64
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    65
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    66
    public char decodeDouble(int b1, int b2) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    67
        if (b2 >= b2Min && b2 < b2Max) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    68
            b2 -= b2Min;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    69
            if (b1 >= b1MinDB1 && b1 <= b1MaxDB1) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    70
                b1 -= b1MinDB1;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    71
                return b2cDB1[b1 * dbSegSize + b2];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    72
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    73
            if (b1 >= b1MinDB2 && b1 <= b1MaxDB2) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    74
                b1 -= b1MinDB2;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    75
                return b2cDB2[b1 * dbSegSize + b2];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    76
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    77
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    78
        return UNMAPPABLE_DECODING;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    79
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    80
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    81
    // for jis0213 all supplementary characters are in 0x2xxxx range,
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    82
    // so only the xxxx part is now stored, should actually store the
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    83
    // codepoint value instead.
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    84
    public char[] decodeSurrogate(int db, char[] cc) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    85
        int end = b2cSupp.length / 2;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    86
        int i = Arrays.binarySearch(b2cSupp, 0, end, (char)db);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    87
        if (i >= 0) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    88
            Character.toChars(b2cSupp[end + i] + 0x20000, cc, 0);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    89
            return cc;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    90
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    91
        return null;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    92
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    93
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    94
    public char[] decodeComposite(Entry comp, char[] cc) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    95
        int i = findBytes(b2cComp, comp);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    96
        if (i >= 0) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    97
            cc[0] = (char)b2cComp[i].cp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    98
            cc[1] = (char)b2cComp[i].cp2;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
    99
            return cc;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   100
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   101
        return null;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   102
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   103
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   104
    public int encodeChar(char ch) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   105
        int index = c2bIndex[ch >> 8];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   106
        if (index == 0xffff)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   107
            return UNMAPPABLE_ENCODING;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   108
        return c2b[index + (ch & 0xff)];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   109
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   110
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   111
    public int encodeSurrogate(char hi, char lo) {
688
5ebcfc3cebe4 6710199: SJIS_0213 does not handle "unmappable" encoding operation correctly
sherman
parents: 400
diff changeset
   112
        int cp = Character.toCodePoint(hi, lo);
5ebcfc3cebe4 6710199: SJIS_0213 does not handle "unmappable" encoding operation correctly
sherman
parents: 400
diff changeset
   113
        if (cp < 0x20000 || cp >= 0x30000)
5ebcfc3cebe4 6710199: SJIS_0213 does not handle "unmappable" encoding operation correctly
sherman
parents: 400
diff changeset
   114
            return UNMAPPABLE_ENCODING;
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   115
        int end = c2bSupp.length / 2;
688
5ebcfc3cebe4 6710199: SJIS_0213 does not handle "unmappable" encoding operation correctly
sherman
parents: 400
diff changeset
   116
        int i = Arrays.binarySearch(c2bSupp, 0, end, (char)cp);
400
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   117
        if (i >= 0)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   118
            return c2bSupp[end + i];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   119
        return UNMAPPABLE_ENCODING;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   120
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   121
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   122
    public boolean isCompositeBase(Entry comp) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   123
        if (comp.cp <= 0x31f7 && comp.cp >= 0xe6) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   124
            return (findCP(c2bComp, comp) >= 0);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   125
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   126
        return false;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   127
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   128
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   129
    public int encodeComposite(Entry comp) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   130
        int i = findComp(c2bComp, comp);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   131
        if (i >= 0)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   132
            return c2bComp[i].bs;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   133
        return UNMAPPABLE_ENCODING;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   134
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   135
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   136
    // init the CharsetMapping object from the .dat binary file
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   137
    public static CharsetMapping get(final InputStream is) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   138
        return AccessController.doPrivileged(new PrivilegedAction<CharsetMapping>() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   139
            public CharsetMapping run() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   140
                return new CharsetMapping().load(is);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   141
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   142
        });
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   143
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   144
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   145
    public static class Entry {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   146
        public int bs;   //byte sequence reps
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   147
        public int cp;   //Unicode codepoint
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   148
        public int cp2;  //CC of composite
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   149
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   150
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   151
    static Comparator<Entry> comparatorBytes =
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   152
        new Comparator<Entry>() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   153
            public int compare(Entry m1, Entry m2) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   154
                return m1.bs - m2.bs;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   155
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   156
            public boolean equals(Object obj) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   157
                return this == obj;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   158
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   159
    };
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   160
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   161
    static Comparator<Entry> comparatorCP =
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   162
        new Comparator<Entry>() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   163
            public int compare(Entry m1, Entry m2) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   164
                return m1.cp - m2.cp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   165
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   166
            public boolean equals(Object obj) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   167
                return this == obj;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   168
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   169
    };
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   170
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   171
    static Comparator<Entry> comparatorComp =
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   172
        new Comparator<Entry>() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   173
            public int compare(Entry m1, Entry m2) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   174
                 int v = m1.cp - m2.cp;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   175
                 if (v == 0)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   176
                   v = m1.cp2 - m2.cp2;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   177
                 return v;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   178
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   179
            public boolean equals(Object obj) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   180
                return this == obj;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   181
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   182
    };
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   183
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   184
    static int findBytes(Entry[] a, Entry k) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   185
        return Arrays.binarySearch(a, 0, a.length, k, comparatorBytes);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   186
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   187
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   188
    static int findCP(Entry[] a, Entry k) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   189
        return Arrays.binarySearch(a, 0, a.length, k, comparatorCP);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   190
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   191
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   192
    static int findComp(Entry[] a, Entry k) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   193
        return Arrays.binarySearch(a, 0, a.length, k, comparatorComp);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   194
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   195
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   196
    /*****************************************************************************/
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   197
    // tags of different charset mapping tables
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   198
    private final static int MAP_SINGLEBYTE      = 0x1; // 0..256  : c
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   199
    private final static int MAP_DOUBLEBYTE1     = 0x2; // min..max: c
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   200
    private final static int MAP_DOUBLEBYTE2     = 0x3; // min..max: c [DB2]
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   201
    private final static int MAP_SUPPLEMENT      = 0x5; //           db,c
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   202
    private final static int MAP_SUPPLEMENT_C2B  = 0x6; //           c,db
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   203
    private final static int MAP_COMPOSITE       = 0x7; //           db,base,cc
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   204
    private final static int MAP_INDEXC2B        = 0x8; // index table of c->bb
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   205
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   206
    private static final boolean readNBytes(InputStream in, byte[] bb, int N)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   207
        throws IOException
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   208
    {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   209
        int off = 0;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   210
        while (N > 0) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   211
            int n = in.read(bb, off, N);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   212
            if (n == -1)
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   213
                return false;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   214
            N = N - n;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   215
            off += n;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   216
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   217
        return true;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   218
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   219
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   220
    int off = 0;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   221
    byte[] bb;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   222
    private char[] readCharArray() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   223
        // first 2 bytes are the number of "chars" stored in this table
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   224
        int size  = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   225
        char [] cc = new char[size];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   226
        for (int i = 0; i < size; i++) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   227
            cc[i] = (char)(((bb[off++]&0xff)<<8) | (bb[off++]&0xff));
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   228
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   229
        return cc;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   230
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   231
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   232
    void readSINGLEBYTE() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   233
        char[] map = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   234
        for (int i = 0; i < map.length; i++) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   235
            char c = map[i];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   236
            if (c != UNMAPPABLE_DECODING) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   237
                c2b[c2bIndex[c >> 8] + (c&0xff)] = (char)i;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   238
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   239
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   240
        b2cSB = map;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   241
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   242
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   243
    void readINDEXC2B() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   244
        char[] map = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   245
        for (int i = map.length - 1; i >= 0; i--) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   246
            if (c2b == null && map[i] != -1) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   247
                c2b = new char[map[i] + 256];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   248
                Arrays.fill(c2b, (char)UNMAPPABLE_ENCODING);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   249
                break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   250
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   251
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   252
        c2bIndex = map;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   253
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   254
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   255
    char[] readDB(int b1Min, int b2Min, int segSize) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   256
        char[] map = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   257
        for (int i = 0; i < map.length; i++) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   258
            char c = map[i];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   259
            if (c != UNMAPPABLE_DECODING) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   260
                int b1 = i / segSize;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   261
                int b2 = i % segSize;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   262
                int b = (b1 + b1Min)* 256 + (b2 + b2Min);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   263
                //System.out.printf("    DB %x\t%x%n", b, c & 0xffff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   264
                c2b[c2bIndex[c >> 8] + (c&0xff)] = (char)(b);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   265
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   266
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   267
        return map;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   268
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   269
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   270
    void readDOUBLEBYTE1() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   271
        b1MinDB1 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   272
        b1MaxDB1 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   273
        b2Min =    ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   274
        b2Max =    ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   275
        dbSegSize = b2Max - b2Min + 1;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   276
        b2cDB1 = readDB(b1MinDB1, b2Min, dbSegSize);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   277
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   278
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   279
    void readDOUBLEBYTE2() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   280
        b1MinDB2 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   281
        b1MaxDB2 = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   282
        b2Min =    ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   283
        b2Max =    ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   284
        dbSegSize = b2Max - b2Min + 1;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   285
        b2cDB2 = readDB(b1MinDB2, b2Min, dbSegSize);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   286
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   287
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   288
    void readCOMPOSITE() {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   289
        char[] map = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   290
        int mLen = map.length/3;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   291
        b2cComp = new Entry[mLen];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   292
        c2bComp = new Entry[mLen];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   293
        for (int i = 0, j= 0; i < mLen; i++) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   294
            Entry m = new Entry();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   295
            m.bs = map[j++];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   296
            m.cp = map[j++];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   297
            m.cp2 = map[j++];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   298
            b2cComp[i] = m;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   299
            c2bComp[i] = m;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   300
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   301
        Arrays.sort(c2bComp, 0, c2bComp.length, comparatorComp);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   302
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   303
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   304
    CharsetMapping load(InputStream in) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   305
        try {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   306
            // The first 4 bytes are the size of the total data followed in
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   307
            // this .dat file.
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   308
            int len = ((in.read()&0xff) << 24) | ((in.read()&0xff) << 16) |
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   309
                      ((in.read()&0xff) << 8) | (in.read()&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   310
            bb = new byte[len];
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   311
            off = 0;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   312
            //System.out.printf("In : Total=%d%n", len);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   313
            // Read in all bytes
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   314
            if (!readNBytes(in, bb, len))
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   315
                throw new RuntimeException("Corrupted data file");
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   316
            in.close();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   317
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   318
            while (off < len) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   319
                int type = ((bb[off++]&0xff)<<8) | (bb[off++]&0xff);
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   320
                switch(type) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   321
                case MAP_INDEXC2B:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   322
                    readINDEXC2B();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   323
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   324
                case MAP_SINGLEBYTE:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   325
                    readSINGLEBYTE();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   326
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   327
                case MAP_DOUBLEBYTE1:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   328
                    readDOUBLEBYTE1();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   329
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   330
                case MAP_DOUBLEBYTE2:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   331
                    readDOUBLEBYTE2();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   332
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   333
                case MAP_SUPPLEMENT:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   334
                    b2cSupp = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   335
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   336
                case MAP_SUPPLEMENT_C2B:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   337
                    c2bSupp = readCharArray();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   338
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   339
                case MAP_COMPOSITE:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   340
                    readCOMPOSITE();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   341
                    break;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   342
                default:
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   343
                    throw new RuntimeException("Corrupted data file");
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   344
                }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   345
            }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   346
            bb = null;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   347
            return this;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   348
        } catch (IOException x) {
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   349
            x.printStackTrace();
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   350
            return null;
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   351
        }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   352
    }
9f3afee24320 6529796: Support JIS X 0213:2004 in existing JDK versions, especially for Windows Vista
sherman
parents:
diff changeset
   353
}