001: /*
002: * @(#)ByteToCharDBCS_ASCII.java 1.10 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package sun.io;
028:
029: public abstract class ByteToCharDBCS_ASCII extends ByteToCharConverter {
030: private boolean savedBytePresent;
031: private byte savedByte;
032: protected String singleByteToChar;
033: protected boolean leadByte[];
034: protected short index1[];
035: protected String index2;
036: protected int mask1;
037: protected int mask2;
038: protected int shift;
039:
040: public ByteToCharDBCS_ASCII() {
041: super ();
042: savedBytePresent = false;
043: }
044:
045: public int flush(char[] output, int outStart, int outEnd)
046: throws MalformedInputException {
047: if (savedBytePresent) {
048: reset();
049: badInputLength = 0;
050: throw new MalformedInputException();
051: }
052: reset();
053: return 0;
054: }
055:
056: /**
057: * Character conversion
058: */
059: public int convert(byte[] input, int inOff, int inEnd,
060: char[] output, int outOff, int outEnd)
061: throws UnknownCharacterException, MalformedInputException,
062: ConversionBufferFullException {
063: int inputSize;
064: char outputChar = '\uFFFD';
065: charOff = outOff;
066: byteOff = inOff;
067: while (byteOff < inEnd) {
068: int byte1, byte2;
069: int v;
070: if (!savedBytePresent) {
071: byte1 = input[byteOff];
072: inputSize = 1;
073: } else {
074: byte1 = savedByte;
075: savedBytePresent = false;
076: inputSize = 0;
077: }
078: if (byte1 < 0)
079: byte1 += 256;
080: if (!leadByte[byte1]) {
081: outputChar = singleByteToChar.charAt(byte1);
082: } else {
083: if (byteOff + inputSize >= inEnd) {
084: savedByte = (byte) byte1;
085: savedBytePresent = true;
086: byteOff += inputSize;
087: break;
088: }
089: byte2 = input[byteOff + inputSize];
090: if (byte2 < 0)
091: byte2 += 256;
092: inputSize++;
093: // Lookup in the two level index
094: v = byte1 * 256 + byte2;
095: outputChar = index2
096: .charAt(index1[((v & mask1) >> shift)]
097: + (v & mask2));
098: }
099: if (outputChar == '\uFFFD') {
100: if (subMode)
101: outputChar = subChars[0];
102: else {
103: badInputLength = inputSize;
104: throw new UnknownCharacterException();
105: }
106: }
107: if (charOff >= outEnd)
108: throw new ConversionBufferFullException();
109: output[charOff++] = outputChar;
110: byteOff += inputSize;
111: }
112: return charOff - outOff;
113: }
114:
115: /**
116: * Resets the converter.
117: */
118: public void reset() {
119: charOff = byteOff = 0;
120: savedBytePresent = false;
121: }
122: }
|