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/A
0N/Apackage sun.io;
0N/Aimport sun.nio.cs.ext.GB18030;
0N/A
0N/Apublic class CharToByteGB18030 extends CharToByteConverter
0N/A{
0N/A
0N/A private char highHalfZoneCode;
0N/A boolean flushed = true;
0N/A
0N/A private final static int GB18030_SINGLE_BYTE = 1;
0N/A private final static int GB18030_DOUBLE_BYTE = 2;
0N/A private final static int GB18030_FOUR_BYTE = 3;
0N/A private static short[] index1;
0N/A private static String[] index2;
0N/A private int currentState;
0N/A
0N/A public CharToByteGB18030() {
0N/A GB18030 nioCoder = new GB18030();
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A subBytes = new byte[1];
0N/A subBytes[0] = (byte)'?';
0N/A index1 = nioCoder.getEncoderIndex1();
0N/A index2 = nioCoder.getEncoderIndex2();
0N/A }
0N/A
0N/A public int flush(byte[] output, int outStart, int outEnd)
0N/A throws MalformedInputException
0N/A {
0N/A if (highHalfZoneCode != 0) {
0N/A highHalfZoneCode = 0;
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
0N/A }
0N/A reset();
0N/A flushed = true;
0N/A return 0;
0N/A }
0N/A
0N/A public void reset() {
0N/A byteOff = charOff = 0;
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A }
0N/A
0N/A public boolean canConvert(char c) {
0N/A // converts all but unpaired surrogates
0N/A // and illegal chars, U+FFFE & U+FFFF
0N/A
0N/A if ((c >= 0xd800 && c <=0xdfff) || (c >= 0xfffe))
0N/A return false;
0N/A else
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Character conversion
0N/A */
0N/A public int convert(char[] input, int inOff, int inEnd,
0N/A byte[] output, int outOff, int outEnd)
0N/A throws UnknownCharacterException, MalformedInputException,
0N/A ConversionBufferFullException
0N/A {
0N/A int linearDiffValue = 0;
0N/A int hiByte = 0 , loByte = 0; // low and high order bytes
0N/A char inputChar; // Input character to be converted
0N/A charOff = inOff;
0N/A byteOff = outOff;
0N/A int inputSize; // Size of the input
0N/A int outputSize; // Size of the output
0N/A
0N/A flushed = false;
0N/A
0N/A if (highHalfZoneCode != 0) {
0N/A if (input[inOff] >= 0xDC00 && input[inOff] <= 0xDFFF) {
0N/A
0N/A // This is legal UTF16 sequence, so shunt in the high
0N/A // surrogate for conversion by convert() loop.
0N/A
0N/A char[] newBuf = new char[inEnd - inOff + 1];
0N/A newBuf[0] = highHalfZoneCode;
0N/A System.arraycopy(input, inOff, newBuf, 1, inEnd - inOff);
0N/A charOff -= 1;
0N/A input = newBuf;
0N/A inOff = 0;
0N/A inEnd = newBuf.length;
0N/A highHalfZoneCode = 0;
0N/A } else {
0N/A // This is illegal UTF16 sequence.
0N/A badInputLength = 0;
0N/A throw new MalformedInputException();
0N/A }
0N/A }
0N/A
0N/A // Main encode loop
0N/A
0N/A while (charOff < inEnd) {
0N/A inputChar = input[charOff++];
0N/A
0N/A if(inputChar >= '\uD800' && inputChar <= '\uDBFF') {
0N/A // Is this the last character of the input?
0N/A if (charOff + 1 > inEnd) {
0N/A highHalfZoneCode = inputChar;
0N/A break;
0N/A }
0N/A
0N/A char previousChar = inputChar;
0N/A inputChar = input[charOff];
0N/A
0N/A // Is there a low surrogate following?
0N/A if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
0N/A inputSize = 2;
0N/A charOff++;
0N/A linearDiffValue = ( previousChar - 0xD800) * 0x400 +
0N/A ( inputChar - 0xDC00) + 0x2E248;
0N/A
0N/A currentState = GB18030_FOUR_BYTE;
0N/A } else {
0N/A // We have a malformed surrogate pair
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A }
0N/A // Is this an unaccompanied low surrogate?
0N/A else if (inputChar >= '\uDC00' && inputChar <= '\uDFFF') {
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A
0N/A // Not part of a surrogate
0N/A else if (inputChar >= 0x0000 && inputChar <= 0x007F) {
0N/A if (byteOff >= outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A currentState = GB18030_SINGLE_BYTE;
0N/A output[byteOff++] = (byte) inputChar;
0N/A }
0N/A else if (inputChar <= 0xA4C6 || inputChar >= 0xE000) {
0N/A int outByteVal = getGB18030(index1, index2, inputChar);
0N/A
0N/A if (outByteVal == 0xFFFD ) {
0N/A if (subMode) {
0N/A if (byteOff >= outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A } else {
0N/A output[byteOff++] = subBytes[0];
0N/A continue;
0N/A }
0N/A } else {
0N/A badInputLength = 1;
0N/A throw new UnknownCharacterException();
0N/A }
0N/A }
0N/A
0N/A hiByte = (outByteVal & 0xFF00) >> 8;
0N/A loByte = (outByteVal & 0xFF);
0N/A
0N/A linearDiffValue = (hiByte - 0x20) * 256 + loByte;
0N/A
0N/A if (inputChar >= 0xE000 && inputChar < 0xF900)
0N/A linearDiffValue += 0x82BD;
0N/A else if (inputChar >= 0xF900)
0N/A linearDiffValue += 0x93A9;
0N/A
0N/A if (hiByte > 0x80)
0N/A currentState = GB18030_DOUBLE_BYTE;
0N/A else
0N/A currentState = GB18030_FOUR_BYTE;
0N/A }
0N/A else if (inputChar >= 0xA4C7 && inputChar <= 0xD7FF) {
0N/A linearDiffValue = inputChar - 0x5543;
0N/A currentState = GB18030_FOUR_BYTE;
0N/A }
0N/A else {
0N/A badInputLength = 1;
0N/A throw new MalformedInputException();
0N/A }
0N/A
0N/A if (currentState == GB18030_SINGLE_BYTE)
0N/A continue;
0N/A
0N/A if (currentState == GB18030_DOUBLE_BYTE) {
0N/A if (byteOff + 2 > outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A output[byteOff++] = (byte)hiByte;
0N/A output[byteOff++] = (byte)loByte;
0N/A }
0N/A else { // Four Byte encoding
0N/A if (byteOff + 4 > outEnd) {
0N/A throw new ConversionBufferFullException();
0N/A }
0N/A
0N/A byte b1, b2, b3, b4;
0N/A
0N/A b4 = (byte)((linearDiffValue % 10) + 0x30);
0N/A linearDiffValue /= 10;
0N/A b3 = (byte)((linearDiffValue % 126) + 0x81);
0N/A linearDiffValue /= 126;
0N/A b2 = (byte)((linearDiffValue % 10) + 0x30);
0N/A b1 = (byte)((linearDiffValue / 10) + 0x81);
0N/A output[byteOff++] = b1;
0N/A output[byteOff++] = b2;
0N/A output[byteOff++] = b3;
0N/A output[byteOff++] = b4;
0N/A }
0N/A }
0N/A // Return number of bytes written to the output buffer.
0N/A return byteOff - outOff;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * returns the maximum number of bytes needed to convert a char
0N/A */
0N/A public int getMaxBytesPerChar() {
0N/A return 4;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the character set ID
0N/A */
0N/A public String getCharacterEncoding() {
0N/A return "GB18030";
0N/A }
0N/A
0N/A private int getGB18030(short[] outerIndex, String[] innerIndex, char ch) {
0N/A int offset = outerIndex[((ch & 0xff00) >> 8 )] << 8;
0N/A
0N/A return innerIndex[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
0N/A }
0N/A
0N/A}