0N/A/*
2362N/A * Copyright (c) 2001, 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/A/*
0N/A */
0N/A
0N/Apackage sun.io;
0N/A
0N/Aimport sun.nio.cs.ext.GB18030;
0N/A
0N/Apublic class ByteToCharGB18030 extends ByteToCharGB18030DB {
0N/A
0N/A private static final int GB18030_SINGLE_BYTE = 1;
0N/A private static final int GB18030_DOUBLE_BYTE = 2;
0N/A private static final int GB18030_FOUR_BYTE = 3;
0N/A private static short[] decoderIndex1;
0N/A private static String[] decoderIndex2;
0N/A
0N/A private int currentState;
0N/A private int savedSize;
0N/A private byte[] savedBytes;
0N/A
0N/A public ByteToCharGB18030() {
0N/A super();
0N/A GB18030 nioCoder = new GB18030();
0N/A savedBytes = new byte[3];
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A decoderIndex1 = nioCoder.getDecoderIndex1();
0N/A decoderIndex2 = nioCoder.getDecoderIndex2();
0N/A savedSize = 0;
0N/A }
0N/A
0N/A public short[] getOuter() {
0N/A return(index1);
0N/A }
0N/A
0N/A public String[] getInner() {
0N/A return(index2);
0N/A }
0N/A
0N/A public short[] getDBIndex1() {
0N/A return(super.index1);
0N/A }
0N/A
0N/A public String[] getDBIndex2() {
0N/A return(super.index2);
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 = GB18030_DOUBLE_BYTE;
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 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 // Filter illegal bytes when they are detected in saved
0N/A // partial input from a previous conversion attempt.
0N/A if (((savedBytes[0] & 0xFF) < 0x81 || savedBytes[0] > 0xFE) ||
0N/A (savedSize > 1 &&
0N/A (savedBytes[1] & 0xFF) < 0x30 ) ||
0N/A (savedSize > 2 &&
0N/A ((savedBytes[2] & 0xFF) < 0x81 ||
0N/A (savedBytes[2] & 0xFF) > 0xFE ))) {
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
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 = 0 , byte2 = 0, byte3 = 0, byte4 = 0;
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){ // US-ASCII range
0N/A outputChar = (char)byte1;
0N/A currentState = GB18030_SINGLE_BYTE;
0N/A }
0N/A
0N/A else if (byte1 < 0x81 || byte1 > 0xfe) {
0N/A if (subMode)
0N/A outputChar = subChars[0];
0N/A else {
0N/A badInputLength = 1;
0N/A throw new UnknownCharacterException();
0N/A }
0N/A }
0N/A else {
0N/A // Either 2 or 4 byte sequence follows
0N/A // If an underrun is detected save for later
0N/A // replay.
0N/A
0N/A if (readOff + inputSize > inEnd) {
0N/A savedBytes[0]=(byte)byte1;
0N/A savedSize = 1;
0N/A break;
0N/A }
0N/A
0N/A byte2 = input[readOff++] & 0xFF;
0N/A inputSize = 2;
0N/A
0N/A if (byte2 < 0x30) {
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A else if (byte2 >= 0x30 && byte2 <= 0x39) {
0N/A currentState = GB18030_FOUR_BYTE;
0N/A inputSize = 4;
0N/A
0N/A if (readOff + 2 > inEnd) {
0N/A if (readOff + 1 > inEnd) {
0N/A savedBytes[0] = (byte)byte1;
0N/A savedBytes[1] = (byte)byte2;
0N/A savedSize = 2;
0N/A }
0N/A else {
0N/A savedBytes[0] = (byte)byte1;
0N/A savedBytes[1] = (byte)byte2;
0N/A savedBytes[2] = input[readOff++];
0N/A savedSize = 3;
0N/A }
0N/A break;
0N/A }
0N/A byte3 = input[readOff++] & 0xFF;
0N/A if (byte3 < 0x81 || byte3 > 0xfe) {
0N/A badInputLength = 3;
0N/A throw new MalformedInputException();
0N/A }
0N/A
0N/A byte4 = input[readOff++] & 0xFF;
0N/A
0N/A if (byte4 < 0x30 || byte4 > 0x39) {
0N/A badInputLength = 4;
0N/A throw new MalformedInputException();
0N/A }
0N/A }
0N/A else if (byte2 == 0x7f || byte2 == 0xff ||
0N/A (byte2 < 0x40 )) {
0N/A badInputLength = 2;
0N/A throw new MalformedInputException();
0N/A }
0N/A else
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A }
0N/A
0N/A switch (currentState){
0N/A case GB18030_SINGLE_BYTE:
0N/A output[charOff++] = (char)(byte1);
0N/A break;
0N/A case GB18030_DOUBLE_BYTE:
0N/A output[charOff++] = super.getUnicode(byte1, byte2);
0N/A break;
0N/A case GB18030_FOUR_BYTE:
0N/A int offset = (((byte1 - 0x81) * 10 +
0N/A (byte2 - 0x30)) * 126 +
0N/A byte3 - 0x81) * 10 + byte4 - 0x30;
0N/A int hiByte = (offset >>8) & 0xFF;
0N/A int lowByte = (offset & 0xFF);
0N/A
0N/A // Mixture of table lookups and algorithmic calculation
0N/A // of character values.
0N/A
0N/A // BMP Ranges
0N/A
0N/A if (offset <= 0x4A62)
0N/A output[charOff++] = getChar(offset);
0N/A else if (offset > 0x4A62 && offset <= 0x82BC)
0N/A output[charOff++] = (char) (offset + 0x5543);
0N/A else if (offset >= 0x82BD && offset <= 0x830D)
0N/A output[charOff++] = getChar(offset);
0N/A else if (offset >= 0x830D && offset <= 0x93A8)
0N/A output[charOff++] = (char) (offset + 0x6557);
0N/A else if (offset >= 0x93A9 && offset <= 0x99FB)
0N/A output[charOff++] = getChar(offset);
0N/A // Supplemental UCS planes handled via surrogates
0N/A else if (offset >= 0x2E248 && offset < 0x12E248) {
0N/A if (offset >= 0x12E248) {
0N/A if (subMode)
0N/A return subChars[0];
0N/A else {
0N/A badInputLength = 4;
0N/A throw new UnknownCharacterException();
0N/A }
0N/A }
0N/A
0N/A if (charOff +2 > outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A offset -= 0x1e248;
0N/A char highSurr = (char) ((offset - 0x10000) / 0x400 + 0xD800);
0N/A char lowSurr = (char) ((offset - 0x10000) % 0x400 + 0xDC00);
0N/A output[charOff++] = highSurr;
0N/A output[charOff++] = lowSurr;
0N/A }
0N/A else {
0N/A badInputLength = 4;
0N/A throw new MalformedInputException();
0N/A }
0N/A break;
0N/A }
0N/A byteOff += inputSize;
0N/A }
0N/A
0N/A byteOff += savedSize;
0N/A return charOff - outOff;
0N/A }
0N/A
0N/A public void reset() {
0N/A byteOff = charOff = 0;
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A savedSize = 0;
0N/A }
0N/A
0N/A public String getCharacterEncoding() {
0N/A return "GB18030";
0N/A }
0N/A
0N/A private char getChar(int offset) throws UnknownCharacterException {
0N/A int byte1 = (offset >>8) & 0xFF;
0N/A int byte2 = (offset & 0xFF);
0N/A int start = 0, end = 0xFF;
0N/A
0N/A if (((byte1 < 0) || (byte1 > getOuter().length))
0N/A || ((byte2 < start) || (byte2 > end))) {
0N/A if (subMode)
0N/A return subChars[0];
0N/A else {
0N/A badInputLength = 1;
0N/A throw new UnknownCharacterException();
0N/A }
0N/A }
0N/A
0N/A int n = (decoderIndex1[byte1] & 0xf) * (end - start + 1) + (byte2 - start);
0N/A return decoderIndex2[decoderIndex1[byte1] >> 4].charAt(n);
0N/A }
0N/A}