0N/A/*
2362N/A * Copyright (c) 1996, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.io;
0N/A
0N/A/**
0N/A * @author Limin Shi
0N/A */
0N/A
0N/Apublic class ByteToCharEUC_JP extends ByteToCharJIS0208 {
0N/A private byte savedSecond = 0;
0N/A
0N/A ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201();
0N/A ByteToCharJIS0212 bcJIS0212 = new ByteToCharJIS0212();
0N/A
0N/A public ByteToCharEUC_JP() {
0N/A super();
0N/A start = 0xA1;
0N/A end = 0xFE;
0N/A savedSecond = 0;
0N/A }
0N/A
0N/A public int flush(char[] output, int outStart, int outEnd)
0N/A throws MalformedInputException
0N/A {
0N/A if (savedSecond != 0) {
0N/A reset();
0N/A throw new MalformedInputException();
0N/A }
0N/A reset();
0N/A return 0;
0N/A }
0N/A
0N/A /**
0N/A * Resets the converter.
0N/A * Call this method to reset the converter to its initial state
0N/A */
0N/A public void reset() {
0N/A super.reset();
0N/A savedSecond = 0;
0N/A }
0N/A
0N/A public String getCharacterEncoding() {
0N/A return "EUC_JP";
0N/A }
0N/A
0N/A protected char convSingleByte(int b) {
0N/A if (b < 0 || b > 0x7F)
0N/A return REPLACE_CHAR;
0N/A return bcJIS0201.getUnicode(b);
0N/A }
0N/A
0N/A protected char getUnicode(int byte1, int byte2) {
0N/A if (byte1 == 0x8E) {
0N/A return bcJIS0201.getUnicode(byte2 - 256);
0N/A }
0N/A // Fix for bug 4121358 - similar fix for bug 4117820 put
0N/A // into ByteToCharDoubleByte.getUnicode()
0N/A if (((byte1 < 0) || (byte1 > index1.length))
0N/A || ((byte2 < start) || (byte2 > end)))
0N/A return REPLACE_CHAR;
0N/A
0N/A int n = (index1[byte1 - 0x80] & 0xf) * (end - start + 1)
0N/A + (byte2 - start);
0N/A return index2[index1[byte1 - 0x80] >> 4].charAt(n);
0N/A }
0N/A
0N/A protected char decode0212(int byte1, int byte2) {
0N/A return bcJIS0212.getUnicode(byte1, byte2);
0N/A }
0N/A
0N/A /**
0N/A * Converts sequences of bytes to characters.
0N/A * Conversions that result in Exceptions can be restarted by calling
0N/A * convert again, with appropriately modified parameters.
0N/A * @return the characters written to output.
0N/A * @param input byte array containing text in Double/single Byte
0N/A * @param inStart offset in input array
0N/A * @param inEnd offset of last byte to be converted
0N/A * @param output character array to receive conversion result
0N/A * @param outStart starting offset
0N/A * @param outEnd offset of last byte to be written to
0N/A * @throw UnsupportedCharacterException for any bytes
0N/A * that cannot be converted to the external character set.
0N/A */
0N/A public int convert(byte[] input, int inOff, int inEnd,
0N/A char[] output, int outOff, int outEnd)
0N/A throws UnknownCharacterException,
0N/A ConversionBufferFullException
0N/A {
0N/A char outputChar = REPLACE_CHAR;
0N/A int inputSize = 0; // Size of input
0N/A
0N/A // Record beginning offsets
0N/A charOff = outOff;
0N/A byteOff = inOff;
0N/A
0N/A // Loop until we hit the end of the input
0N/A while (byteOff < inEnd) {
0N/A int byte1, byte2;
0N/A
0N/A if (savedByte == 0) {
0N/A byte1 = input[byteOff];
0N/A inputSize = 1;
0N/A } else {
0N/A byte1 = savedByte;
0N/A savedByte = 0;
0N/A inputSize = 0;
0N/A }
0N/A
0N/A outputChar = convSingleByte(byte1);
0N/A
0N/A if (outputChar == REPLACE_CHAR) { // Multibyte char
0N/A if ((byte1 & 0xff) == 0x8F) { // JIS0212
0N/A if (byteOff + inputSize + 1 >= inEnd) {
0N/A // split in the middle of a character
0N/A // save the first 2 bytes for next time around
0N/A savedByte = (byte) byte1;
0N/A byteOff += inputSize;
0N/A if (byteOff < inEnd) {
0N/A savedSecond = input[byteOff];
0N/A byteOff++;
0N/A }
0N/A break;
0N/A }
0N/A if (savedSecond != 0) {
0N/A byte1 = savedSecond & 0xff;
0N/A savedSecond = 0;
0N/A } else {
0N/A byte1 = input[byteOff + inputSize] & 0xff;
0N/A inputSize++;
0N/A }
0N/A byte2 = input[byteOff + inputSize] & 0xff;
0N/A inputSize++;
0N/A outputChar = decode0212(byte1-0x80, byte2-0x80);
0N/A } else { // JIS0208
0N/A if (byteOff + inputSize >= inEnd) {
0N/A // split in the middle of a character
0N/A // save the first byte for next time around
0N/A savedByte = (byte) byte1;
0N/A byteOff += inputSize;
0N/A break;
0N/A }
0N/A byte1 &= 0xff;
0N/A byte2 = input[byteOff + inputSize] & 0xff;
0N/A inputSize++;
0N/A outputChar = getUnicode(byte1, byte2);
0N/A }
0N/A }
0N/A
0N/A if (outputChar == REPLACE_CHAR) {
0N/A if (subMode)
0N/A outputChar = subChars[0];
0N/A else {
0N/A badInputLength = inputSize;
0N/A throw new UnknownCharacterException();
0N/A }
0N/A }
0N/A
0N/A if (charOff >= outEnd)
0N/A throw new ConversionBufferFullException();
0N/A
0N/A output[charOff++] = outputChar;
0N/A byteOff += inputSize;
0N/A }
0N/A
0N/A return charOff - outOff;
0N/A }
0N/A
0N/A}