001: /*
002: * @(#)ByteToCharSingleByte.java 1.20 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:
028: package sun.io;
029:
030: /**
031: * A table driven conversion from byte to char for single byte character sets.
032: * The needed data tables will reside in a character set specific subclass.
033: *
034: * @author Lloyd Honomichl
035: * @author Asmus Freytag
036: * @version 8/28/96
037: */
038: public abstract class ByteToCharSingleByte extends ByteToCharConverter {
039:
040: /**
041: * Mapping table. Values supplied by subclass
042: */
043: protected String byteToCharTable;
044:
045: public String getByteToCharTable() {
046: return byteToCharTable;
047: }
048:
049: public int flush(char[] output, int outStart, int outEnd) {
050: byteOff = charOff = 0;
051: return 0;
052: }
053:
054: /**
055: * Converts bytes to characters according to the selected character
056: * encoding.
057: * Maintains internal state, so that conversions that result in
058: * exceptions can be restarted by calling convert again, with
059: * appropriately modified parameters.
060: * Call reset before converting input that is not a continuation of
061: * the previous call.
062: * @return the number of characters written to output.
063: * @param input byte array containing text in character set
064: * @param inStart offset in input array
065: * @param inEnd offset of last byte to be converted
066: * @param output character array to receive conversion result
067: * @param outStart starting offset
068: * @param outEnd offset of last character to be written to
069: * @throw MalformedInputException for any sequence of bytes that is
070: * illegal for the input character set, including any partial multi-byte
071: * sequence which occurs at the end of an input buffer.
072: * @throw UnsupportedCharacterException for any sequence of bytes that
073: * contain a character not supported in the current conversion.
074: * @throw BufferFullException whenever the output buffer is full
075: * before the input is exhausted.
076: * @see #reset
077: */
078: public int convert(byte[] input, int inOff, int inEnd,
079: char[] output, int outOff, int outEnd)
080: throws UnknownCharacterException, MalformedInputException,
081: ConversionBufferFullException {
082: char outputChar;
083: int byteIndex;
084:
085: charOff = outOff;
086: byteOff = inOff;
087:
088: // Loop until we hit the end of the input
089: while (byteOff < inEnd) {
090:
091: byteIndex = input[byteOff];
092:
093: /* old source
094: *outputChar = byteToCharTable[input[byteOff] + 128];
095: */
096: // Lookup the output character
097: outputChar = getUnicode(byteIndex);
098:
099: // Is the output unmappable?
100: if (outputChar == '\uFFFD') {
101: if (subMode) {
102: outputChar = subChars[0];
103: } else {
104: badInputLength = 1;
105: throw new UnknownCharacterException();
106: }
107: }
108:
109: // If we don't have room for the output, throw an exception
110: if (charOff >= outEnd)
111: throw new ConversionBufferFullException();
112:
113: // Put the character in the output buffer
114: output[charOff] = outputChar;
115: charOff++;
116: byteOff++;
117: }
118:
119: // Return the length written to the output buffer
120: return charOff - outOff;
121: }
122:
123: protected char getUnicode(int byteIndex) {
124: int n = byteIndex + 128;
125: if (n >= byteToCharTable.length() || n < 0)
126: return '\uFFFD';
127: return byteToCharTable.charAt(n);
128: }
129:
130: /**
131: * Resets the converter.
132: * Call this method to reset the converter to its initial state
133: */
134: public void reset() {
135: byteOff = charOff = 0;
136: }
137:
138: }
|