001: /*
002: * @(#)CharToByteISO2022.java 1.11 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: * @author Tom Zhou
032: */
033:
034: public abstract class CharToByteISO2022 extends CharToByteConverter {
035: private final byte ISO_ESC = 0x1b;
036: private final byte ISO_SI = 0x0f;
037: private final byte ISO_SO = 0x0e;
038: private final byte ISO_SS2_7 = 0x4e;
039: private final byte ISO_SS3_7 = 0x4f;
040: private final byte SS2 = (byte) 0x8e;
041: private final byte P2 = (byte) 0xA2;
042: private final byte P3 = (byte) 0xA3;
043: private final byte MSB = (byte) 0x80;
044: protected final byte maximumDesignatorLength = 3;
045: protected String SODesignator, SS2Designator = null,
046: SS3Designator = null;
047: protected CharToByteConverter codeConverter;
048: private boolean shiftout = false;
049: private boolean SODesDefined = false;
050: private boolean SS2DesDefined = false;
051: private boolean SS3DesDefined = false;
052:
053: public int flush(byte[] output, int outStart, int outEnd)
054: throws MalformedInputException {
055: reset();
056: return 0;
057: }
058:
059: public void reset() {
060: shiftout = false;
061: SODesDefined = false;
062: SS2DesDefined = false;
063: SS3DesDefined = false;
064: byteOff = charOff = 0;
065: }
066:
067: public boolean canConvert(char ch) {
068: if (ch < 0x80)
069: return true;
070: return codeConverter.canConvert(ch);
071: }
072:
073: private int unicodeToNative(char unicode, byte ebyte[]) {
074: int index = 0;
075: byte tmpByte[];
076: byte convByte[] = new byte[codeConverter.getMaxBytesPerChar()];
077: char convChar[] = { unicode };
078: int converted;
079: try {
080: converted = codeConverter.convert(convChar, 0, 1, convByte,
081: 0, codeConverter.getMaxBytesPerChar());
082: } catch (Exception e) {
083: return -1;
084: }
085: if (converted == 2) {
086: if (!SODesDefined) {
087: SODesDefined = true;
088: ebyte[0] = ISO_ESC;
089: tmpByte = SODesignator.getBytes();
090: System.arraycopy(tmpByte, 0, ebyte, 1, tmpByte.length);
091: index = tmpByte.length + 1;
092: }
093: if (!shiftout) {
094: shiftout = true;
095: ebyte[index++] = ISO_SO;
096: }
097: ebyte[index++] = (byte) (convByte[0] & 0x7f);
098: ebyte[index++] = (byte) (convByte[1] & 0x7f);
099: } else {
100: if ((convByte[0] == SS2) && (convByte[1] == P2)) {
101: if (!SS2DesDefined) {
102: SS2DesDefined = true;
103: ebyte[0] = ISO_ESC;
104: tmpByte = SS2Designator.getBytes();
105: System.arraycopy(tmpByte, 0, ebyte, 1,
106: tmpByte.length);
107: index = tmpByte.length + 1;
108: }
109: ebyte[index++] = ISO_ESC;
110: ebyte[index++] = ISO_SS2_7;
111: ebyte[index++] = (byte) (convByte[2] & 0x7f);
112: ebyte[index++] = (byte) (convByte[3] & 0x7f);
113: }
114: if ((convByte[0] == SS2) && (convByte[1] == 0xA3)) {
115: if (!SS3DesDefined) {
116: SS3DesDefined = true;
117: ebyte[0] = ISO_ESC;
118: tmpByte = SS3Designator.getBytes();
119: System.arraycopy(tmpByte, 0, ebyte, 1,
120: tmpByte.length);
121: index = tmpByte.length + 1;
122: }
123: ebyte[index++] = ISO_ESC;
124: ebyte[index++] = ISO_SS3_7;
125: ebyte[index++] = (byte) (convByte[2] & 0x7f);
126: ebyte[index++] = (byte) (convByte[3] & 0x7f);
127: }
128: }
129: return index;
130: }
131:
132: /**
133: * Character conversion
134: */
135: public int convert(char[] input, int inOff, int inEnd,
136: byte[] output, int outOff, int outEnd)
137: throws UnknownCharacterException, MalformedInputException,
138: ConversionBufferFullException {
139: int outputSize;
140: byte[] tmpbuf = new byte[this .getMaxBytesPerChar()];
141: byte[] outputByte;
142: charOff = inOff;
143: byteOff = outOff;
144: while (charOff < inEnd) {
145: outputByte = tmpbuf;
146: if (input[charOff] < 0x80) { // ASCII
147: if (shiftout) {
148: shiftout = false;
149: outputSize = 2;
150: outputByte[0] = ISO_SI;
151: outputByte[1] = (byte) (input[charOff] & 0x7f);
152: } else {
153: outputSize = 1;
154: outputByte[0] = (byte) (input[charOff] & 0x7f);
155: }
156: if (input[charOff] == '\n') {
157: SODesDefined = false;
158: SS2DesDefined = false;
159: SS3DesDefined = false;
160: }
161: } else {
162: outputSize = unicodeToNative(input[charOff], outputByte);
163: }
164: charOff++;
165: if (outputSize == -1) {
166: if (subMode) {
167: if (!SODesDefined) {
168: SODesDefined = !SODesDefined;
169: outputByte[0] = ISO_SO;
170: outputByte[1] = (byte) '?';
171: outputSize = 2;
172: } else {
173: outputByte = subBytes;
174: outputSize = subBytes.length;
175: }
176: } else {
177: badInputLength = 1;
178: throw new UnknownCharacterException();
179: }
180: }
181: if (outEnd - byteOff < outputSize)
182: throw new ConversionBufferFullException();
183: for (int i = 0; i < outputSize; i++)
184: output[byteOff++] = outputByte[i];
185: }
186: return byteOff - outOff;
187: }
188: }
|