jdk/src/share/classes/sun/io/CharToByteEUC_JP.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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 CharToByteEUC_JP extends CharToByteJIS0208 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    CharToByteJIS0201 cbJIS0201 = new CharToByteJIS0201();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    CharToByteJIS0212 cbJIS0212 = new CharToByteJIS0212();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public String getCharacterEncoding() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        return "EUC_JP";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    protected int convSingleByte(char inputChar, byte[] outputByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        byte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (inputChar == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            outputByte[0] = (byte)0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if ((b = cbJIS0201.getNative(inputChar)) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (b > 0 && b < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            outputByte[0] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        outputByte[0] = (byte)0x8E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        outputByte[1] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected int getNative(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int offset = index1[((ch & 0xff00) >> 8 )] << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int r = index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (r != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return r + 0x8080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        r = cbJIS0212.getNative(ch);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (r == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return r + 0x8F8080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Converts characters to sequences of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Conversions that result in Exceptions can be restarted by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * convert again, with appropriately modified parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @return the characters written to output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param input char array containing text in Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param inStart offset in input array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param inEnd offset of last byte to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param output byte array to receive conversion result
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param outStart starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param outEnd offset of last byte to be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @throw UnsupportedCharacterException for any character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * that cannot be converted to the external character set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public int convert(char[] input, int inOff, int inEnd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                       byte[] output, int outOff, int outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throws MalformedInputException, UnknownCharacterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
               ConversionBufferFullException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        char    inputChar;                 // Input character to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        byte[]  outputByte;                // Output byte written to output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        int     inputSize = 0;             // Size of input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        int     outputSize = 0;            // Size of output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        byte[]  tmpbuf = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // Record beginning offsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        charOff = inOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        byteOff = outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (highHalfZoneCode != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            inputChar = highHalfZoneCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            highHalfZoneCode = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (input[inOff] >= 0xdc00 && input[inOff] <= 0xdfff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                // This is legal UTF16 sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                // This is illegal UTF16 sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                badInputLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // Loop until we hit the end of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        while(charOff < inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            inputSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            outputByte = tmpbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            inputChar = input[charOff]; // Get the input character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Is this a high surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if(inputChar >= '\uD800' && inputChar <= '\uDBFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // Is this the last character of the input?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (charOff + 1 >= inEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    highHalfZoneCode = inputChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                // Is there a low surrogate following?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                inputChar = input[charOff + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    // We have a valid surrogate pair.  Too bad we don't do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    // surrogates.  Is substitution enabled?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                    if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        outputByte = subBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        outputSize = subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        inputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        badInputLength = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    // We have a malformed surrogate pair
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Is this an unaccompanied low surrogate?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            else if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new MalformedInputException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                outputSize = convSingleByte(inputChar, outputByte);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (outputSize == 0) { // DoubleByte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    int ncode = getNative(inputChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    if (ncode != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        if ((ncode & 0xFF0000) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            outputByte[0] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                            outputByte[1] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                            outputSize = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            outputByte[0] = (byte) 0x8F;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                            outputByte[1] = (byte) ((ncode & 0xff00) >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                            outputByte[2] = (byte) (ncode & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                            outputSize = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        if (subMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                            outputByte = subBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            outputSize = subBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                            badInputLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                            throw new UnknownCharacterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // If we don't have room for the output, throw an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            if (byteOff + outputSize > outEnd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throw new ConversionBufferFullException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // Put the byte in the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            for (int i = 0; i < outputSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                output[byteOff++] = outputByte[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            charOff += inputSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // Return the length written to the output buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return byteOff - outOff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * the maximum number of bytes needed to hold a converted char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @returns the maximum number of bytes needed for a converted char
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public int getMaxBytesPerChar() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}