jdk/src/share/classes/sun/io/ByteToCharEUC_JP.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 1996-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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Limin Shi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class ByteToCharEUC_JP extends ByteToCharJIS0208 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private byte savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    ByteToCharJIS0212 bcJIS0212 = new ByteToCharJIS0212();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    public ByteToCharEUC_JP() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        start = 0xA1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        end = 0xFE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public int flush(char[] output, int outStart, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        throws MalformedInputException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (savedSecond != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Resets the converter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Call this method to reset the converter to its initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return "EUC_JP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected char convSingleByte(int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (b < 0 || b > 0x7F)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return bcJIS0201.getUnicode(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected char getUnicode(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (byte1 == 0x8E) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            return bcJIS0201.getUnicode(byte2 - 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Fix for bug 4121358 - similar fix for bug 4117820 put
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // into ByteToCharDoubleByte.getUnicode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (((byte1 < 0) || (byte1 > index1.length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            || ((byte2 < start) || (byte2 > end)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        int n = (index1[byte1 - 0x80] & 0xf) * (end - start + 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                + (byte2 - start);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return index2[index1[byte1 - 0x80] >> 4].charAt(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    protected char decode0212(int byte1, int byte2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        return bcJIS0212.getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Converts sequences of bytes to characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Conversions that result in Exceptions can be restarted by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * convert again, with appropriately modified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @return the characters written to output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param input byte array containing text in Double/single Byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param inStart offset in input array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param inEnd offset of last byte to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param output character array to receive conversion result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param outStart starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @param outEnd offset of last byte to be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @throw UnsupportedCharacterException for any bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * that cannot be converted to the external character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int convert(byte[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                       char[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        throws UnknownCharacterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        char    outputChar = REPLACE_CHAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int     inputSize = 0;          // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        // Record beginning offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        charOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        byteOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        // Loop until we hit the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        while (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            int byte1, byte2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (savedByte == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                byte1 = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                byte1 = savedByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                savedByte = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                inputSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            outputChar = convSingleByte(byte1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (outputChar == REPLACE_CHAR) {   // Multibyte char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if ((byte1 & 0xff) == 0x8F) {   // JIS0212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    if (byteOff + inputSize + 1 >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        // split in the middle of a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        // save the first 2 bytes for next time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        savedByte = (byte) byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        if (byteOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                            savedSecond = input[byteOff];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            byteOff++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    if (savedSecond != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        byte1 = savedSecond & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        savedSecond = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        byte1 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    byte2 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    outputChar = decode0212(byte1-0x80, byte2-0x80);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } else { // JIS0208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (byteOff + inputSize >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        // split in the middle of a character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        // save the first byte for next time around
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        savedByte = (byte) byte1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    byte1 &= 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    byte2 = input[byteOff + inputSize] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    inputSize++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    outputChar = getUnicode(byte1, byte2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            if (outputChar == REPLACE_CHAR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (subMode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    outputChar = subChars[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    badInputLength = inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (charOff >= outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            output[charOff++] = outputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            byteOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return charOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}