0N/A/*
3261N/A * Copyright (c) 1997, 2010, 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 * An algorithmic conversion from ISO 2022 to Unicode
0N/A *
0N/A * @author Tom Zhou
0N/A */
0N/Apublic abstract class ByteToCharISO2022 extends ByteToCharConverter
0N/A{
0N/A // Value to be filled by subclass
0N/A protected String SODesignator[];
0N/A protected String SS2Designator[] = null;
0N/A protected String SS3Designator[] = null;
0N/A
0N/A protected ByteToCharConverter SOConverter[];
0N/A protected ByteToCharConverter SS2Converter[] = null;
0N/A protected ByteToCharConverter SS3Converter[] = null;
0N/A
0N/A private static final byte ISO_ESC = 0x1b;
0N/A private static final byte ISO_SI = 0x0f;
0N/A private static final byte ISO_SO = 0x0e;
0N/A private static final byte ISO_SS2_7 = 0x4e;
0N/A private static final byte ISO_SS3_7 = 0x4f;
0N/A private static final byte MSB = (byte)0x80;
0N/A private static final char REPLACE_CHAR = '\uFFFD';
0N/A private static final byte maximumDesignatorLength = 3;
0N/A
0N/A private static final byte SOFlag = 0;
0N/A private static final byte SS2Flag = 1;
0N/A private static final byte SS3Flag = 2;
0N/A private static final byte G0 = 0;
0N/A private static final byte G1 = 1;
0N/A
0N/A private ByteToCharConverter tmpConverter[];
0N/A
0N/A private int curSODes, curSS2Des, curSS3Des;
0N/A private boolean shiftout;
0N/A
0N/A private byte remainByte[] = new byte[10];
0N/A private int remainIndex = -1;
0N/A private byte state, firstByte;
0N/A
0N/A public void reset()
0N/A {
0N/A int i = 0;
0N/A
0N/A shiftout = false;
0N/A state = G0;
0N/A firstByte = 0;
0N/A
0N/A curSODes = 0;
0N/A curSS2Des = 0;
0N/A curSS3Des = 0;
0N/A
0N/A charOff = byteOff = 0;
0N/A remainIndex = -1;
0N/A
0N/A for(i = 0; i < remainByte.length; i++)
0N/A remainByte[i] = 0;
0N/A }
0N/A
0N/A public int flush(char[] output, int outStart, int outEnd)
0N/A throws MalformedInputException
0N/A {
0N/A int i;
0N/A if (state != G0) {
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
0N/A }
0N/A reset();
0N/A return 0;
0N/A }
0N/A
0N/A private byte[] savetyGetSrc(byte[] input, int inOff, int inEnd, int nbytes)
0N/A {
0N/A int i;
0N/A byte tmp[];
0N/A
0N/A if(inOff <= (inEnd-nbytes+1))
0N/A tmp = new byte[nbytes];
0N/A else
0N/A tmp = new byte[inEnd-inOff];
0N/A
0N/A for(i = 0; i < tmp.length; i++)
0N/A tmp[i] = input[inOff+i];
0N/A return tmp;
0N/A }
0N/A
0N/A private char getUnicode(byte byte1, byte byte2, byte shiftFlag)
0N/A {
0N/A byte1 |= MSB;
0N/A byte2 |= MSB;
0N/A
0N/A byte[] tmpByte = {byte1,byte2};
0N/A char[] tmpChar = new char[1];
0N/A int i = 0,
0N/A tmpIndex = 0;
0N/A
0N/A switch(shiftFlag) {
0N/A case SOFlag:
0N/A tmpIndex = curSODes;
2472N/A tmpConverter = SOConverter;
0N/A break;
0N/A case SS2Flag:
0N/A tmpIndex = curSS2Des;
2472N/A tmpConverter = SS2Converter;
0N/A break;
0N/A case SS3Flag:
0N/A tmpIndex = curSS3Des;
2472N/A tmpConverter = SS3Converter;
0N/A break;
0N/A }
0N/A
0N/A for(i = 0; i < tmpConverter.length; i++) {
0N/A if(tmpIndex == i) {
0N/A try {
0N/A tmpConverter[i].convert(tmpByte, 0, 2, tmpChar, 0, 1);
0N/A } catch (Exception e) {}
0N/A return tmpChar[0];
0N/A }
0N/A }
0N/A return REPLACE_CHAR;
0N/A }
0N/A
0N/A public final int convert(byte[] input, int inOff, int inEnd,
0N/A char[] output, int outOff, int outEnd)
0N/A throws ConversionBufferFullException,
0N/A MalformedInputException
0N/A {
0N/A int i;
0N/A int DesignatorLength = 0;
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 // If we don't have room for the output, throw an exception
0N/A if (charOff >= outEnd)
0N/A throw new ConversionBufferFullException();
0N/A if(remainIndex < 0) {
0N/A remainByte[0] = input[byteOff];
0N/A remainIndex = 0;
0N/A byteOff++;
0N/A }
0N/A switch (remainByte[0]) {
0N/A case ISO_SO:
0N/A shiftout = true;
0N/A if(remainIndex > 0)
0N/A System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
0N/A remainIndex--;
0N/A break;
0N/A case ISO_SI:
0N/A shiftout = false;
0N/A if(remainIndex > 0)
0N/A System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
0N/A remainIndex--;
0N/A break;
0N/A case ISO_ESC:
0N/A byte tmp[] = savetyGetSrc(input, byteOff, inEnd,
0N/A (maximumDesignatorLength-remainIndex));
0N/A System.arraycopy(tmp, 0, remainByte, remainIndex+1, tmp.length);
0N/A remainIndex += tmp.length;
0N/A byteOff += tmp.length;
0N/A if(tmp.length<(maximumDesignatorLength-remainIndex))
0N/A break;
0N/A String tmpString = new String(remainByte, 1, remainIndex);
0N/A for (i = 0; i < SODesignator.length; i++) {
0N/A if(tmpString.indexOf(SODesignator[i]) == 0) {
0N/A curSODes = i;
0N/A DesignatorLength = SODesignator[i].length();
0N/A break;
0N/A }
0N/A }
0N/A
0N/A if (DesignatorLength == 0 ) { // Designator not recognized
0N/A badInputLength = tmp.length;
0N/A throw new MalformedInputException();
0N/A }
0N/A
0N/A if (i == SODesignator.length) {
0N/A for (i = 0; i < SS2Designator.length; i++) {
0N/A if(tmpString.indexOf(SS2Designator[i]) == 0) {
0N/A curSS2Des = i;
0N/A DesignatorLength = SS2Designator[i].length();
0N/A break;
0N/A }
0N/A }
0N/A if(i == SS2Designator.length) {
0N/A for(i = 0; i < SS3Designator.length; i++) {
0N/A if (tmpString.indexOf(SS3Designator[i]) == 0) {
0N/A curSS3Des = i;
0N/A DesignatorLength = SS3Designator[i].length();
0N/A break;
0N/A }
0N/A }
0N/A if (i == SS3Designator.length) {
0N/A switch(remainByte[1]) {
0N/A case ISO_SS2_7:
0N/A output[charOff] = getUnicode(remainByte[2],
0N/A remainByte[3],
0N/A SS2Flag);
0N/A charOff++;
0N/A DesignatorLength = 3;
0N/A break;
0N/A case ISO_SS3_7:
0N/A output[charOff] = getUnicode(remainByte[2],
0N/A remainByte[3],
0N/A SS3Flag);
0N/A charOff++;
0N/A DesignatorLength = 3;
0N/A break;
0N/A default:
0N/A DesignatorLength = 0;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A if (remainIndex > DesignatorLength) {
0N/A for(i = 0; i < remainIndex-DesignatorLength; i++)
0N/A remainByte[i] = remainByte[DesignatorLength+1+i];
0N/A remainIndex = i-1;
0N/A } else {
0N/A remainIndex = -1;
0N/A }
0N/A break;
0N/A default:
0N/A if (!shiftout) {
0N/A output[charOff] = (char)remainByte[0];
0N/A charOff++;
0N/A } else {
0N/A switch (state) {
0N/A case G0:
0N/A firstByte = remainByte[0];
0N/A state = G1;
0N/A break;
0N/A case G1:
0N/A output[charOff] = getUnicode(firstByte, remainByte[0],
0N/A SOFlag);
0N/A charOff++;
0N/A state = G0;
0N/A break;
0N/A }
0N/A }
0N/A if (remainIndex > 0)
0N/A System.arraycopy(remainByte, 1, remainByte, 0, remainIndex);
0N/A remainIndex--;
0N/A }
0N/A }
0N/A return charOff - outOff;
0N/A }
0N/A}