jdk/src/share/classes/sun/io/ByteToCharEUC_JP_Solaris.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.nio.cs.ext.JIS_X_0208_Solaris_Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.nio.cs.ext.JIS_X_0212_Solaris_Decoder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Limin Shi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author Ian Little
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * EUC_JP variant converter for Solaris with vendor defined chars
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * added (4765370)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class ByteToCharEUC_JP_Solaris extends ByteToCharEUC_JP {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private byte savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    ByteToCharJIS0212_Solaris bcJIS0212 = new ByteToCharJIS0212_Solaris();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    short[] j0208Index1 = JIS_X_0208_Solaris_Decoder.getIndex1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    String[] j0208Index2 = JIS_X_0208_Solaris_Decoder.getIndex2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    ByteToCharJIS0212_Solaris j0212Decoder = new ByteToCharJIS0212_Solaris();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public ByteToCharEUC_JP_Solaris() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        start = 0xA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        end = 0xFE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public int flush(char[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (savedSecond != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Resets the converter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Call this method to reset the converter to its initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return "eucJP-open";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected char convSingleByte(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (b < 0 || b > 0x7F)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return bcJIS0201.getUnicode(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected char getUnicode(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (byte1 == 0x8E) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return bcJIS0201.getUnicode(byte2 - 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Fix for bug 4121358 - similar fix for bug 4117820 put
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // into ByteToCharDoubleByte.getUnicode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (((byte1 < 0) || (byte1 > j0208Index1.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            || ((byte2 < start) || (byte2 > end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        char result = super.getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (result != '\uFFFD') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            int n = (j0208Index1[byte1 - 0x80] & 0xf) * (end - start + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                + (byte2 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return j0208Index2[j0208Index1[byte1 - 0x80] >> 4].charAt(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected char decode0212(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return j0212Decoder.getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Converts sequences of bytes to characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Conversions that result in Exceptions can be restarted by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * convert again, with appropriately modified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the characters written to output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @param input byte array containing text in Double/single Byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param inStart offset in input array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param inEnd offset of last byte to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param output character array to receive conversion result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param outStart starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param outEnd offset of last byte to be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @throw UnsupportedCharacterException for any bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * that cannot be converted to the external character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                       char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws UnknownCharacterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        char    outputChar = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int     inputSize = 0;          // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        // Record beginning offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        charOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        byteOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // Loop until we hit the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        while (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            int byte1, byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (savedByte == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                byte1 = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                byte1 = savedByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                savedByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            outputChar = convSingleByte(byte1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (outputChar == REPLACE_CHAR) {   // Multibyte char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if ((byte1 & 0xff) == 0x8F) {   // JIS0212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    if (byteOff + inputSize + 1 >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        // split in the middle of a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        // save the first 2 bytes for next time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        savedByte = (byte) byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        if (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            savedSecond = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            byteOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if (savedSecond != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        byte1 = savedSecond & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        byte1 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    byte2 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    outputChar = bcJIS0212.getUnicode(byte1-0x80, byte2-0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                } else { // JIS0208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (byteOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        // split in the middle of a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        // save the first byte for next time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        savedByte = (byte) byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    byte1 &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    byte2 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    outputChar = getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            if (outputChar == REPLACE_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (subMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    outputChar = subChars[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    badInputLength = inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (charOff >= outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            output[charOff++] = outputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}