001: /*
002: * %W% %E%
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: import java.io.UnsupportedEncodingException;
030:
031: public class ByteToCharJISAutoDetect extends ByteToCharConverter {
032: private String convName = null;
033: private ByteToCharConverter detectedConv = null;
034: private ByteToCharConverter defaultConv = null;
035:
036: public ByteToCharJISAutoDetect() {
037: super ();
038: try {
039: defaultConv = ByteToCharConverter.getConverter("8859_1");
040: } catch (UnsupportedEncodingException e) {
041: defaultConv = new ByteToCharISO8859_1();
042: }
043: defaultConv.subChars = subChars;
044: defaultConv.subMode = subMode;
045: }
046:
047: public int flush(char[] output, int outStart, int outEnd)
048: throws MalformedInputException,
049: ConversionBufferFullException {
050: badInputLength = 0;
051: if (detectedConv != null)
052: return detectedConv.flush(output, outStart, outEnd);
053: else
054: return defaultConv.flush(output, outStart, outEnd);
055: }
056:
057: /**
058: * Character conversion
059: */
060: public int convert(byte[] input, int inOff, int inEnd,
061: char[] output, int outOff, int outEnd)
062: throws UnknownCharacterException, MalformedInputException,
063: ConversionBufferFullException {
064: int start_pos = inOff;
065: int end_pos = inOff + inEnd;
066: int num;
067: String candidatedConvName = null;
068: ByteToCharConverter anticipatedConv = null;
069: try {
070: if (detectedConv == null) {
071: for (int cnt = start_pos; cnt < end_pos; cnt++) {
072: int itmp = input[cnt] & 0xff;
073: if (itmp == 0x1b) {
074: convName = "ISO2022JP";
075: break;
076: } else if ((0x81 <= itmp) && (itmp <= 0x9f)) {
077: convName = "SJIS";
078: break;
079: } else if (((0xa1 <= itmp) && (itmp <= 0xdf))
080: || ((0xfd <= itmp) && (itmp <= 0xfe))) {
081: convName = "EUC_JP";
082: break;
083: } else if ((0xe0 <= itmp) && (itmp <= 0xea)) {
084: if (cnt + 1 < end_pos) {
085: cnt++;
086: itmp = input[cnt] & 0xff;
087: if ((0x40 <= itmp) && (itmp <= 0x7e)) {
088: convName = "SJIS";
089: break;
090: } else {
091: if (candidatedConvName == null)
092: candidatedConvName = "SJIS";
093: }
094: } else {
095: throw new MalformedInputException();
096: }
097: //convName = "SJIS";
098: break;
099: } else if ((0xeb <= itmp) && (itmp <= 0xfc)) {
100: if (cnt + 1 < end_pos) {
101: cnt++;
102: itmp = input[cnt] & 0xff;
103: if ((0x40 <= itmp) && (itmp <= 0x7e)) {
104: convName = "SJIS";
105: break;
106: } else {
107: if (candidatedConvName == null)
108: candidatedConvName = "EUC_JP";
109: }
110: } else {
111: throw new MalformedInputException();
112: }
113: break;
114: }
115: }
116: if (convName != null) {
117: try {
118: detectedConv = ByteToCharConverter
119: .getConverter(convName);
120: detectedConv.subChars = subChars;
121: detectedConv.subMode = subMode;
122: } catch (UnsupportedEncodingException e) {
123: detectedConv = null;
124: convName = null;
125: }
126: } else if (candidatedConvName != null) {
127: try {
128: anticipatedConv = ByteToCharConverter
129: .getConverter(candidatedConvName);
130: anticipatedConv.subChars = subChars;
131: anticipatedConv.subMode = subMode;
132: } catch (UnsupportedEncodingException e) {
133: anticipatedConv = null;
134: candidatedConvName = null;
135: }
136: }
137: }
138: } catch (Exception e) {
139: // If we fail to detect the converter needed for any reason,
140: // use the default converter.
141: detectedConv = defaultConv;
142: }
143: if (detectedConv != null) {
144: try {
145: num = detectedConv.convert(input, inOff, inEnd, output,
146: outOff, outEnd);
147: charOff = detectedConv.nextCharIndex();
148: byteOff = detectedConv.nextByteIndex();
149: badInputLength = detectedConv.badInputLength;
150: } catch (UnknownCharacterException e) {
151: charOff = detectedConv.nextCharIndex();
152: byteOff = detectedConv.nextByteIndex();
153: badInputLength = detectedConv.badInputLength;
154: throw e;
155: } catch (MalformedInputException e) {
156: charOff = detectedConv.nextCharIndex();
157: byteOff = detectedConv.nextByteIndex();
158: badInputLength = detectedConv.badInputLength;
159: throw e;
160: } catch (ConversionBufferFullException e) {
161: charOff = detectedConv.nextCharIndex();
162: byteOff = detectedConv.nextByteIndex();
163: badInputLength = detectedConv.badInputLength;
164: throw e;
165: }
166: } else if (anticipatedConv != null) {
167: try {
168: num = anticipatedConv.convert(input, inOff, inEnd,
169: output, outOff, outEnd);
170: charOff = anticipatedConv.nextCharIndex();
171: byteOff = anticipatedConv.nextByteIndex();
172: badInputLength = anticipatedConv.badInputLength;
173: } catch (UnknownCharacterException e) {
174: charOff = anticipatedConv.nextCharIndex();
175: byteOff = anticipatedConv.nextByteIndex();
176: badInputLength = anticipatedConv.badInputLength;
177: throw e;
178: } catch (MalformedInputException e) {
179: charOff = anticipatedConv.nextCharIndex();
180: byteOff = anticipatedConv.nextByteIndex();
181: badInputLength = anticipatedConv.badInputLength;
182: throw e;
183: } catch (ConversionBufferFullException e) {
184: charOff = anticipatedConv.nextCharIndex();
185: byteOff = anticipatedConv.nextByteIndex();
186: badInputLength = anticipatedConv.badInputLength;
187: throw e;
188: }
189: } else {
190: try {
191: num = defaultConv.convert(input, inOff, inEnd, output,
192: outOff, outEnd);
193: charOff = defaultConv.nextCharIndex();
194: byteOff = defaultConv.nextByteIndex();
195: badInputLength = defaultConv.badInputLength;
196: } catch (UnknownCharacterException e) {
197: charOff = defaultConv.nextCharIndex();
198: byteOff = defaultConv.nextByteIndex();
199: badInputLength = defaultConv.badInputLength;
200: throw e;
201: } catch (MalformedInputException e) {
202: charOff = defaultConv.nextCharIndex();
203: byteOff = defaultConv.nextByteIndex();
204: badInputLength = defaultConv.badInputLength;
205: throw e;
206: } catch (ConversionBufferFullException e) {
207: charOff = defaultConv.nextCharIndex();
208: byteOff = defaultConv.nextByteIndex();
209: badInputLength = defaultConv.badInputLength;
210: throw e;
211: }
212: }
213: return num;
214: }
215:
216: public void reset() {
217: if (detectedConv != null) {
218: detectedConv.reset();
219: detectedConv = null;
220: convName = null;
221: } else
222: defaultConv.reset();
223: }
224:
225: public String getCharacterEncoding() {
226: return "JISAutoDetect";
227: }
228: }
|