author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 2913 | 39a9cc073b84 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2004, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.io; |
|
27 |
||
28 |
import sun.nio.cs.ext.EUC_TW; |
|
29 |
||
30 |
/* |
|
31 |
* @author Limin Shi |
|
32 |
*/ |
|
33 |
public class ByteToCharEUC_TW extends ByteToCharConverter |
|
34 |
{ |
|
35 |
private final byte G0 = 0; |
|
36 |
private final byte G1 = 1; |
|
37 |
private final byte G2 = 2; |
|
38 |
private final byte G3 = 3; |
|
39 |
private final byte G4 = 4; |
|
40 |
private final byte MSB = (byte) 0x80; |
|
41 |
private final byte SS2 = (byte) 0x8E; |
|
42 |
||
43 |
private byte firstByte = 0, state = G0; |
|
44 |
private int cnsPlane = 0; |
|
45 |
||
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
46 |
private EUC_TW.Decoder dec = (EUC_TW.Decoder)(new EUC_TW().newDecoder()); |
2 | 47 |
|
48 |
public ByteToCharEUC_TW() { |
|
49 |
} |
|
50 |
||
51 |
public int flush(char[] output, int outStart, int outEnd) |
|
52 |
throws MalformedInputException |
|
53 |
{ |
|
54 |
if (state != G0) { |
|
55 |
state = G0; |
|
56 |
firstByte = 0; |
|
57 |
badInputLength = 0; |
|
58 |
throw new MalformedInputException(); |
|
59 |
} |
|
60 |
reset(); |
|
61 |
return 0; |
|
62 |
} |
|
63 |
||
64 |
public void reset() { |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
65 |
dec.reset(); |
2 | 66 |
state = G0; |
67 |
firstByte = 0; |
|
68 |
byteOff = charOff = 0; |
|
69 |
} |
|
70 |
||
71 |
/** |
|
72 |
* Character conversion |
|
73 |
*/ |
|
74 |
public int convert(byte[] input, int inOff, int inEnd, |
|
75 |
char[] output, int outOff, int outEnd) |
|
76 |
throws UnknownCharacterException, MalformedInputException, |
|
77 |
ConversionBufferFullException |
|
78 |
{ |
|
79 |
int inputSize = 0; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
80 |
char[] c1 = new char[1]; |
2 | 81 |
|
82 |
byteOff = inOff; |
|
83 |
charOff = outOff; |
|
84 |
||
85 |
cnsPlane = 3; |
|
86 |
while (byteOff < inEnd) { |
|
87 |
if (charOff >= outEnd) |
|
88 |
throw new ConversionBufferFullException(); |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
89 |
char[] outputChar = null; |
2 | 90 |
switch (state) { |
91 |
case G0: |
|
92 |
if ( (input[byteOff] & MSB) == 0) { // ASCII |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
93 |
outputChar = c1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
94 |
outputChar[0] = (char) input[byteOff]; |
2 | 95 |
} else if (input[byteOff] == SS2) { // Codeset 2 |
96 |
state = G2; |
|
97 |
} else { // Codeset 1 |
|
98 |
firstByte = input[byteOff]; |
|
99 |
state = G1; |
|
100 |
} |
|
101 |
break; |
|
102 |
case G1: |
|
103 |
inputSize = 2; |
|
104 |
if ( (input[byteOff] & MSB) != 0) { // 2nd byte |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
105 |
cnsPlane = 0; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
106 |
outputChar = dec.toUnicode(firstByte & 0xff, |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
107 |
input[byteOff] & 0xff, |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
108 |
cnsPlane); |
2 | 109 |
} else { // Error |
110 |
badInputLength = 1; |
|
111 |
throw new MalformedInputException(); |
|
112 |
} |
|
113 |
firstByte = 0; |
|
114 |
state = G0; |
|
115 |
break; |
|
116 |
case G2: |
|
117 |
cnsPlane = (input[byteOff] & (byte)0x0f); |
|
118 |
// Adjust String array index for plan 15 |
|
119 |
cnsPlane = (cnsPlane == 15)? 8 : cnsPlane; |
|
120 |
||
121 |
if (cnsPlane < 15) { |
|
122 |
state = G3; |
|
123 |
} else { |
|
124 |
badInputLength = 2; |
|
125 |
throw new MalformedInputException(); |
|
126 |
} |
|
127 |
||
128 |
break; |
|
129 |
case G3: |
|
130 |
if ( (input[byteOff] & MSB) != 0) { // 1st byte |
|
131 |
firstByte = input[byteOff]; |
|
132 |
state = G4; |
|
133 |
} else { // Error |
|
134 |
state = G0; |
|
135 |
badInputLength = 2; |
|
136 |
throw new MalformedInputException(); |
|
137 |
} |
|
138 |
break; |
|
139 |
case G4: |
|
140 |
if ( (input[byteOff] & MSB) != 0) { // 2nd byte |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
141 |
outputChar = dec.toUnicode(firstByte & 0xff, |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
142 |
input[byteOff] & 0xff, |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
143 |
cnsPlane - 1); |
2 | 144 |
} else { // Error |
145 |
badInputLength = 3; |
|
146 |
throw new MalformedInputException(); |
|
147 |
} |
|
148 |
firstByte = 0; |
|
149 |
state = G0; |
|
150 |
break; |
|
151 |
} |
|
152 |
byteOff++; |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
153 |
if (state == G0) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
154 |
if (outputChar == null) { |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
155 |
if (subMode) { // substitution enabled |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
156 |
outputChar = c1; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
157 |
outputChar[0] = subChars[0]; |
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
158 |
} else { |
2 | 159 |
badInputLength = inputSize; |
160 |
throw new UnknownCharacterException(); |
|
161 |
} |
|
162 |
} |
|
2913
39a9cc073b84
6831794: charset EUC_TW is 12.6% of the total size of charsets.jar
sherman
parents:
2
diff
changeset
|
163 |
output[charOff++] = outputChar[0]; |
2 | 164 |
} |
165 |
} |
|
166 |
return charOff - outOff; |
|
167 |
} |
|
168 |
||
169 |
||
170 |
/** |
|
171 |
* Return the character set ID |
|
172 |
*/ |
|
173 |
public String getCharacterEncoding() { |
|
174 |
return "EUC_TW"; |
|
175 |
} |
|
176 |
} |