1 /* |
|
2 * Copyright (c) 2000, Oracle and/or its affiliates. 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. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package sun.io; |
|
27 |
|
28 /** |
|
29 * Class for converting bytes to characters for the EUC-JP encoding in |
|
30 * linux. This converter supports the JIS0201 and the JIS0208 encoding and |
|
31 * omits support for the JIS212 encoding. |
|
32 * |
|
33 * @author Naveen Sanjeeva |
|
34 */ |
|
35 |
|
36 public class ByteToCharEUC_JP_LINUX extends ByteToCharJIS0208 { |
|
37 private byte savedSecond = 0; |
|
38 ByteToCharJIS0201 bcJIS0201 = new ByteToCharJIS0201(); |
|
39 |
|
40 public ByteToCharEUC_JP_LINUX() { |
|
41 super(); |
|
42 start = 0xA1; |
|
43 end = 0xFE; |
|
44 savedSecond = 0; |
|
45 } |
|
46 |
|
47 public int flush(char[] output, int outStart, int outEnd) |
|
48 throws MalformedInputException |
|
49 { |
|
50 if (savedSecond != 0) { |
|
51 reset(); |
|
52 throw new MalformedInputException(); |
|
53 } |
|
54 reset(); |
|
55 return 0; |
|
56 } |
|
57 |
|
58 /** |
|
59 * Resets the converter. |
|
60 * Call this method to reset the converter to its initial state |
|
61 */ |
|
62 public void reset() { |
|
63 super.reset(); |
|
64 savedSecond = 0; |
|
65 } |
|
66 |
|
67 public String getCharacterEncoding() { |
|
68 return "EUC_JP_LINUX"; |
|
69 } |
|
70 |
|
71 protected char convSingleByte(int b) { |
|
72 if (b < 0 || b > 0x7F) |
|
73 return REPLACE_CHAR; |
|
74 return bcJIS0201.getUnicode(b); |
|
75 } |
|
76 |
|
77 protected char getUnicode(int byte1, int byte2) { |
|
78 if (byte1 == 0x8E) { |
|
79 return bcJIS0201.getUnicode(byte2 - 256); |
|
80 } |
|
81 // Fix for bug 4121358 - similar fix for bug 4117820 put |
|
82 // into ByteToCharDoubleByte.getUnicode() |
|
83 if (((byte1 < 0) || (byte1 > index1.length)) |
|
84 || ((byte2 < start) || (byte2 > end))) |
|
85 return REPLACE_CHAR; |
|
86 |
|
87 int n = (index1[byte1 - 0x80] & 0xf) * (end - start + 1) |
|
88 + (byte2 - start); |
|
89 return index2[index1[byte1 - 0x80] >> 4].charAt(n); |
|
90 } |
|
91 |
|
92 /** |
|
93 * Converts sequences of bytes to characters. |
|
94 * Conversions that result in Exceptions can be restarted by calling |
|
95 * convert again, with appropriately modified parameters. |
|
96 * @return the characters written to output. |
|
97 * @param input byte array containing text in Double/single Byte |
|
98 * @param inStart offset in input array |
|
99 * @param inEnd offset of last byte to be converted |
|
100 * @param output character array to receive conversion result |
|
101 * @param outStart starting offset |
|
102 * @param outEnd offset of last byte to be written to |
|
103 * @throw UnsupportedCharacterException for any bytes |
|
104 * that cannot be converted to the external character set. |
|
105 */ |
|
106 public int convert(byte[] input, int inOff, int inEnd, |
|
107 char[] output, int outOff, int outEnd) |
|
108 throws UnknownCharacterException, |
|
109 ConversionBufferFullException |
|
110 { |
|
111 char outputChar = REPLACE_CHAR; |
|
112 int inputSize = 0; // Size of input |
|
113 |
|
114 // Record beginning offsets |
|
115 charOff = outOff; |
|
116 byteOff = inOff; |
|
117 |
|
118 // Loop until we hit the end of the input |
|
119 while (byteOff < inEnd) { |
|
120 int byte1, byte2; |
|
121 |
|
122 if (savedByte == 0) { |
|
123 byte1 = input[byteOff]; |
|
124 inputSize = 1; |
|
125 } else { |
|
126 byte1 = savedByte; |
|
127 savedByte = 0; |
|
128 inputSize = 0; |
|
129 } |
|
130 |
|
131 outputChar = convSingleByte(byte1); |
|
132 |
|
133 if (outputChar == REPLACE_CHAR) { // Multibyte char |
|
134 if ((byte1 & 0xff) != 0x8F) { // JIS0208 |
|
135 if (byteOff + inputSize >= inEnd) { |
|
136 // split in the middle of a character |
|
137 // save the first byte for next time around |
|
138 savedByte = (byte) byte1; |
|
139 byteOff += inputSize; |
|
140 break; |
|
141 } |
|
142 byte1 &= 0xff; |
|
143 byte2 = input[byteOff + inputSize] & 0xff; |
|
144 inputSize++; |
|
145 outputChar = getUnicode(byte1, byte2); |
|
146 } else if ((byte1 & 0xff) == 0x8F) { // JIS0212 |
|
147 // Handling of 3-byte sequences for this converter involves |
|
148 // just ignoring the relevant bytes and returning a |
|
149 // suitable substitute char if one exists. |
|
150 if (byteOff + inputSize + 1 >= inEnd) { |
|
151 // split in the middle of a character |
|
152 // save the first 2 bytes for next time around |
|
153 savedByte = (byte) byte1; |
|
154 byteOff += inputSize; |
|
155 if (byteOff < inEnd) { |
|
156 savedSecond = input[byteOff]; |
|
157 byteOff++; |
|
158 } |
|
159 break; |
|
160 } |
|
161 |
|
162 // Skip over the saved bytes if any |
|
163 if (savedSecond != 0) { |
|
164 savedSecond = 0; |
|
165 } else { |
|
166 inputSize++; |
|
167 } |
|
168 inputSize++; |
|
169 } |
|
170 } |
|
171 |
|
172 if (outputChar == REPLACE_CHAR) { |
|
173 if (subMode) |
|
174 outputChar = subChars[0]; |
|
175 else { |
|
176 badInputLength = inputSize; |
|
177 throw new UnknownCharacterException(); |
|
178 } |
|
179 } |
|
180 |
|
181 if (charOff >= outEnd) |
|
182 throw new ConversionBufferFullException(); |
|
183 |
|
184 output[charOff++] = outputChar; |
|
185 byteOff += inputSize; |
|
186 } |
|
187 |
|
188 return charOff - outOff; |
|
189 } |
|
190 } |
|