2
|
1 |
/*
|
|
2 |
* Copyright 1996-2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package sun.io;
|
|
27 |
|
|
28 |
/**
|
|
29 |
* @author Limin Shi
|
|
30 |
*/
|
|
31 |
|
|
32 |
public class ByteToCharEUC_JP extends ByteToCharJIS0208 {
|
|
33 |
private byte savedSecond = 0;
|
|
34 |
|
|
35 |
ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201();
|
|
36 |
ByteToCharJIS0212 bcJIS0212 = new ByteToCharJIS0212();
|
|
37 |
|
|
38 |
public ByteToCharEUC_JP() {
|
|
39 |
super();
|
|
40 |
start = 0xA1;
|
|
41 |
end = 0xFE;
|
|
42 |
savedSecond = 0;
|
|
43 |
}
|
|
44 |
|
|
45 |
public int flush(char[] output, int outStart, int outEnd)
|
|
46 |
throws MalformedInputException
|
|
47 |
{
|
|
48 |
if (savedSecond != 0) {
|
|
49 |
reset();
|
|
50 |
throw new MalformedInputException();
|
|
51 |
}
|
|
52 |
reset();
|
|
53 |
return 0;
|
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Resets the converter.
|
|
58 |
* Call this method to reset the converter to its initial state
|
|
59 |
*/
|
|
60 |
public void reset() {
|
|
61 |
super.reset();
|
|
62 |
savedSecond = 0;
|
|
63 |
}
|
|
64 |
|
|
65 |
public String getCharacterEncoding() {
|
|
66 |
return "EUC_JP";
|
|
67 |
}
|
|
68 |
|
|
69 |
protected char convSingleByte(int b) {
|
|
70 |
if (b < 0 || b > 0x7F)
|
|
71 |
return REPLACE_CHAR;
|
|
72 |
return bcJIS0201.getUnicode(b);
|
|
73 |
}
|
|
74 |
|
|
75 |
protected char getUnicode(int byte1, int byte2) {
|
|
76 |
if (byte1 == 0x8E) {
|
|
77 |
return bcJIS0201.getUnicode(byte2 - 256);
|
|
78 |
}
|
|
79 |
// Fix for bug 4121358 - similar fix for bug 4117820 put
|
|
80 |
// into ByteToCharDoubleByte.getUnicode()
|
|
81 |
if (((byte1 < 0) || (byte1 > index1.length))
|
|
82 |
|| ((byte2 < start) || (byte2 > end)))
|
|
83 |
return REPLACE_CHAR;
|
|
84 |
|
|
85 |
int n = (index1[byte1 - 0x80] & 0xf) * (end - start + 1)
|
|
86 |
+ (byte2 - start);
|
|
87 |
return index2[index1[byte1 - 0x80] >> 4].charAt(n);
|
|
88 |
}
|
|
89 |
|
|
90 |
protected char decode0212(int byte1, int byte2) {
|
|
91 |
return bcJIS0212.getUnicode(byte1, byte2);
|
|
92 |
}
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Converts sequences of bytes to characters.
|
|
96 |
* Conversions that result in Exceptions can be restarted by calling
|
|
97 |
* convert again, with appropriately modified parameters.
|
|
98 |
* @return the characters written to output.
|
|
99 |
* @param input byte array containing text in Double/single Byte
|
|
100 |
* @param inStart offset in input array
|
|
101 |
* @param inEnd offset of last byte to be converted
|
|
102 |
* @param output character array to receive conversion result
|
|
103 |
* @param outStart starting offset
|
|
104 |
* @param outEnd offset of last byte to be written to
|
|
105 |
* @throw UnsupportedCharacterException for any bytes
|
|
106 |
* that cannot be converted to the external character set.
|
|
107 |
*/
|
|
108 |
public int convert(byte[] input, int inOff, int inEnd,
|
|
109 |
char[] output, int outOff, int outEnd)
|
|
110 |
throws UnknownCharacterException,
|
|
111 |
ConversionBufferFullException
|
|
112 |
{
|
|
113 |
char outputChar = REPLACE_CHAR;
|
|
114 |
int inputSize = 0; // Size of input
|
|
115 |
|
|
116 |
// Record beginning offsets
|
|
117 |
charOff = outOff;
|
|
118 |
byteOff = inOff;
|
|
119 |
|
|
120 |
// Loop until we hit the end of the input
|
|
121 |
while (byteOff < inEnd) {
|
|
122 |
int byte1, byte2;
|
|
123 |
|
|
124 |
if (savedByte == 0) {
|
|
125 |
byte1 = input[byteOff];
|
|
126 |
inputSize = 1;
|
|
127 |
} else {
|
|
128 |
byte1 = savedByte;
|
|
129 |
savedByte = 0;
|
|
130 |
inputSize = 0;
|
|
131 |
}
|
|
132 |
|
|
133 |
outputChar = convSingleByte(byte1);
|
|
134 |
|
|
135 |
if (outputChar == REPLACE_CHAR) { // Multibyte char
|
|
136 |
if ((byte1 & 0xff) == 0x8F) { // JIS0212
|
|
137 |
if (byteOff + inputSize + 1 >= inEnd) {
|
|
138 |
// split in the middle of a character
|
|
139 |
// save the first 2 bytes for next time around
|
|
140 |
savedByte = (byte) byte1;
|
|
141 |
byteOff += inputSize;
|
|
142 |
if (byteOff < inEnd) {
|
|
143 |
savedSecond = input[byteOff];
|
|
144 |
byteOff++;
|
|
145 |
}
|
|
146 |
break;
|
|
147 |
}
|
|
148 |
if (savedSecond != 0) {
|
|
149 |
byte1 = savedSecond & 0xff;
|
|
150 |
savedSecond = 0;
|
|
151 |
} else {
|
|
152 |
byte1 = input[byteOff + inputSize] & 0xff;
|
|
153 |
inputSize++;
|
|
154 |
}
|
|
155 |
byte2 = input[byteOff + inputSize] & 0xff;
|
|
156 |
inputSize++;
|
|
157 |
outputChar = decode0212(byte1-0x80, byte2-0x80);
|
|
158 |
} else { // JIS0208
|
|
159 |
if (byteOff + inputSize >= inEnd) {
|
|
160 |
// split in the middle of a character
|
|
161 |
// save the first byte for next time around
|
|
162 |
savedByte = (byte) byte1;
|
|
163 |
byteOff += inputSize;
|
|
164 |
break;
|
|
165 |
}
|
|
166 |
byte1 &= 0xff;
|
|
167 |
byte2 = input[byteOff + inputSize] & 0xff;
|
|
168 |
inputSize++;
|
|
169 |
outputChar = getUnicode(byte1, byte2);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
if (outputChar == REPLACE_CHAR) {
|
|
174 |
if (subMode)
|
|
175 |
outputChar = subChars[0];
|
|
176 |
else {
|
|
177 |
badInputLength = inputSize;
|
|
178 |
throw new UnknownCharacterException();
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
if (charOff >= outEnd)
|
|
183 |
throw new ConversionBufferFullException();
|
|
184 |
|
|
185 |
output[charOff++] = outputChar;
|
|
186 |
byteOff += inputSize;
|
|
187 |
}
|
|
188 |
|
|
189 |
return charOff - outOff;
|
|
190 |
}
|
|
191 |
|
|
192 |
}
|