0N/A/*
3261N/A * Copyright (c) 1996, 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/Apublic class ByteToCharISO2022JP extends ByteToCharJIS0208 {
0N/A
0N/A private static final int ASCII = 0; // ESC ( B
0N/A private static final int JISX0201_1976 = 1; // ESC ( J
0N/A private static final int JISX0208_1978 = 2; // ESC $ @
0N/A private static final int JISX0208_1983 = 3; // ESC $ B
0N/A private static final int JISX0201_1976_KANA = 4; // ESC ( I
0N/A private static final int SHIFTOUT = 5; // SO (0x0e)
0N/A
0N/A private int currentState;
0N/A private int savedSize;
0N/A private byte[] savedBytes;
0N/A
0N/A public ByteToCharISO2022JP() {
0N/A super();
0N/A savedBytes = new byte[2];
0N/A currentState = ASCII;
0N/A savedSize = 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 (savedSize != 0) {
0N/A savedSize = 0;
0N/A currentState = ASCII;
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
0N/A }
0N/A byteOff = charOff = 0;
0N/A return 0;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Character conversion
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, MalformedInputException,
0N/A ConversionBufferFullException
0N/A {
0N/A int previousState = ASCII;
0N/A int inputSize = 0;
0N/A char outputChar = '\uFFFD';
0N/A // readOff keeps the actual buffer's pointer.
0N/A // byteOff keeps original buffer's pointer.
0N/A int readOff = byteOff = inOff;
0N/A
0N/A if (savedSize != 0) {
0N/A if (savedBytes[0] == 0x1b) { // ESC
0N/A if ((savedSize == 2 &&
0N/A (savedBytes[1] == 0x28 &&
0N/A input[0] != 'B' &&
0N/A input[0] != 'J' &&
0N/A input[0] != 'I') &&
0N/A (savedBytes[1] == 0x24 &&
0N/A input[0] != '@' &&
0N/A input[0] != 'B')) ||
0N/A ((savedSize == 1) &&
0N/A (input[0] != 0x28 &&
0N/A input[0] != 0x24))) {
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
0N/A }
0N/A if ((inEnd - inOff) == 1 && savedSize == 1 &&
0N/A savedBytes[0] == 0x1b) {
0N/A savedSize = 2;
0N/A savedBytes[1] = input[0];
0N/A byteOff++;
0N/A return 0;
0N/A }
0N/A }
0N/A byte[] newBuf = new byte[inEnd - inOff + savedSize];
0N/A for (int i = 0; i < savedSize; i++) {
0N/A newBuf[i] = savedBytes[i];
0N/A }
0N/A System.arraycopy(input, inOff, newBuf, savedSize, inEnd - inOff);
0N/A byteOff -= savedSize;
0N/A input = newBuf;
0N/A inOff = 0;
0N/A inEnd = newBuf.length;
0N/A savedSize = 0;
0N/A }
0N/A
0N/A charOff = outOff;
0N/A readOff = inOff;
0N/A
0N/A while(readOff < inEnd) {
0N/A int byte1, byte2, byte3;
0N/A boolean noOutput = false;
0N/A
0N/A // Is there room in the output buffer for the result?
0N/A if (charOff >= outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A
0N/A // Get the input byte
0N/A byte1 = input[readOff] & 0xFF;
0N/A inputSize = 1;
0N/A
0N/A if ((byte1 & (byte)0x80) != 0){
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A
0N/A // Is this a escape sequence?
0N/A while (byte1 == 0x1b || byte1 == 0x0e || byte1 == 0x0f) {
0N/A if (byte1 == 0x1b){ // ESC
0N/A if (readOff + inputSize + 1 >= inEnd) {
0N/A if (readOff + inputSize >= inEnd) {
0N/A savedSize = 1;
0N/A savedBytes[0] = (byte)byte1;
0N/A } else {
0N/A savedSize = 2;
0N/A savedBytes[0] = (byte)byte1;
2472N/A savedBytes[1] = input[readOff + inputSize];
0N/A inputSize++;
0N/A }
0N/A break;
0N/A }
0N/A byte2 = input[readOff + inputSize] & 0xFF;
0N/A inputSize++;
0N/A if ((byte2 & (byte)0x80) != 0){
0N/A badInputLength = 2;
0N/A throw new MalformedInputException();
0N/A }
0N/A if (byte2 == 0x28){
0N/A byte3 = input[readOff + inputSize] & 0xFF;
0N/A inputSize++;
0N/A if (byte3 == 'B'){
0N/A currentState = ASCII;
0N/A } else if (byte3 == 'J'){
0N/A currentState = JISX0201_1976;
0N/A } else if (byte3 == 'I'){
0N/A currentState = JISX0201_1976_KANA;
0N/A } else {
0N/A // illegal ESC sequence
0N/A badInputLength = 3;
0N/A throw new MalformedInputException();
0N/A }
0N/A } else if (byte2 == '$'){
0N/A byte3 = input[readOff + inputSize] & 0xFF;
0N/A inputSize++;
0N/A if ((byte3 & (byte)0x80) != 0){
0N/A badInputLength = 3;
0N/A throw new MalformedInputException();
0N/A }
0N/A if (byte3 == '@'){
0N/A currentState = JISX0208_1978;
0N/A } else if (byte3 == 'B'){
0N/A currentState = JISX0208_1983;
0N/A } else {
0N/A // illegal ESC sequence
0N/A badInputLength = 3;
0N/A throw new MalformedInputException();
0N/A }
0N/A } else {
0N/A // illegal ESC sequence
0N/A badInputLength = 2;
0N/A throw new MalformedInputException();
0N/A }
0N/A if (readOff + inputSize >= inEnd) {
0N/A noOutput = true;
0N/A break;
0N/A } else {
0N/A byte1 = input[readOff + inputSize];
0N/A inputSize++;
0N/A }
0N/A } else if (byte1 == 0x0e){ // shift out for one byte kana
0N/A previousState = currentState;
0N/A currentState = SHIFTOUT;
0N/A if (readOff + inputSize >= inEnd) {
0N/A noOutput = true;
0N/A break;
0N/A }
0N/A byte1 = input[readOff + inputSize];
0N/A inputSize++;
0N/A if ((byte1 & (byte)0x80) != 0){
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A } else if (byte1 == 0x0f){ // shift in for previous mode
0N/A currentState = previousState;
0N/A if (readOff + inputSize >= inEnd) {
0N/A noOutput = true;
0N/A break;
0N/A }
0N/A byte1 = input[readOff + inputSize];
0N/A inputSize++;
0N/A if ((byte1 & (byte)0x80) != 0){
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A }
0N/A }
0N/A if (noOutput || savedSize != 0) {
0N/A byteOff += inputSize;
0N/A break;
0N/A }
0N/A noOutput = false;
0N/A switch (currentState){
0N/A case ASCII:
0N/A outputChar = (char)(byte1 & 0xff);
0N/A break;
0N/A case JISX0201_1976:
0N/A switch (byte1) {
0N/A case 0x5c:
0N/A outputChar = '\u00a5';
0N/A break;
0N/A case 0x7e:
0N/A outputChar = '\u203e';
0N/A break;
0N/A default:
0N/A outputChar = (char)byte1;
0N/A break;
0N/A }
0N/A break;
0N/A case JISX0208_1978:
0N/A case JISX0208_1983:
0N/A if (readOff + inputSize >= inEnd) {
0N/A savedSize = 1;
0N/A savedBytes[0] = (byte)byte1;
0N/A break;
0N/A }
0N/A byte2 = input[readOff + inputSize] & 0xff;
0N/A inputSize++;
0N/A if ((byte2 & (byte)0x80) != 0){
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A // jisx0208Chars table convert FULLWIDTH_REVERSE_SOLIDUS
0N/A // 0x2140 to REVERSE_SOLIDUS (BACKSLASH) 0x5c.
0N/A // This behavior causes problem because
0N/A // 0x5c is special escape character for java.
0N/A if (byte1 == 0x21 && byte2 == 0x40) {
0N/A outputChar = '\uFF3C';
0N/A } else {
0N/A try {
0N/A outputChar = getUnicode(byte1, byte2);
0N/A } catch (ArrayIndexOutOfBoundsException e) {
0N/A outputChar = '\uFFFD';
0N/A }
0N/A }
0N/A break;
0N/A case JISX0201_1976_KANA:
0N/A case SHIFTOUT:
0N/A if (byte1 > 0x60) {
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A outputChar = (char)(byte1 + 0xff40);
0N/A break;
0N/A }
0N/A
0N/A if (savedSize != 0) {
0N/A byteOff += inputSize;
0N/A break;
0N/A }
0N/A
0N/A if (outputChar == '\uFFFD') {
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 readOff += inputSize;
0N/A byteOff += inputSize;
0N/A output[charOff++] = outputChar;
0N/A }
0N/A
0N/A return charOff - outOff;
0N/A }
0N/A
0N/A public void reset() {
0N/A byteOff = charOff = 0;
0N/A currentState = ASCII;
0N/A savedSize = 0;
0N/A }
0N/A
0N/A public String getCharacterEncoding() {
0N/A return "ISO2022JP";
0N/A }
0N/A}