2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 1999, 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 |
package sun.io;
|
|
26 |
|
2921
|
27 |
import sun.nio.cs.Surrogate;
|
|
28 |
import sun.nio.cs.ext.DoubleByte;
|
|
29 |
import static sun.nio.cs.CharsetMapping.*;
|
|
30 |
|
2
|
31 |
public abstract class CharToByteDBCS_EBCDIC extends CharToByteConverter
|
|
32 |
{
|
|
33 |
private static final int SBCS = 0;
|
|
34 |
private static final int DBCS = 1;
|
|
35 |
|
|
36 |
private static final byte SO = 0x0e;
|
|
37 |
private static final byte SI = 0x0f;
|
|
38 |
|
|
39 |
private int currentState;
|
|
40 |
private char highHalfZoneCode;
|
|
41 |
private byte[] outputByte = new byte[2];
|
|
42 |
|
2921
|
43 |
private DoubleByte.Encoder enc;
|
2
|
44 |
|
2921
|
45 |
public CharToByteDBCS_EBCDIC(DoubleByte.Encoder enc) {
|
2
|
46 |
super();
|
|
47 |
highHalfZoneCode = 0;
|
|
48 |
currentState = SBCS;
|
2921
|
49 |
this.enc = enc;
|
|
50 |
}
|
|
51 |
|
|
52 |
int encodeChar(char c) {
|
|
53 |
return enc.encodeChar(c);
|
2
|
54 |
}
|
|
55 |
|
|
56 |
/**
|
|
57 |
* flush out any residual data and reset the buffer state
|
|
58 |
*/
|
|
59 |
public int flush(byte [] output, int outStart, int outEnd)
|
|
60 |
throws MalformedInputException, ConversionBufferFullException
|
|
61 |
{
|
|
62 |
int bytesOut = 0;
|
|
63 |
|
|
64 |
if (highHalfZoneCode != 0) {
|
|
65 |
reset();
|
|
66 |
badInputLength = 0;
|
|
67 |
throw new MalformedInputException();
|
|
68 |
}
|
|
69 |
|
|
70 |
if (currentState == DBCS) {
|
|
71 |
if (outStart >= outEnd)
|
|
72 |
throw new ConversionBufferFullException();
|
|
73 |
output[outStart] = SI;
|
|
74 |
bytesOut++;
|
|
75 |
}
|
|
76 |
|
|
77 |
reset();
|
|
78 |
return bytesOut;
|
|
79 |
}
|
|
80 |
|
|
81 |
/**
|
|
82 |
* Character conversion
|
|
83 |
*/
|
|
84 |
public int convert(char[] input, int inOff, int inEnd,
|
|
85 |
byte[] output, int outOff, int outEnd)
|
|
86 |
throws UnknownCharacterException, MalformedInputException,
|
|
87 |
ConversionBufferFullException
|
|
88 |
{
|
|
89 |
char inputChar;
|
|
90 |
int inputSize;
|
|
91 |
|
|
92 |
byteOff = outOff;
|
|
93 |
charOff = inOff;
|
|
94 |
|
|
95 |
while(charOff < inEnd) {
|
|
96 |
|
|
97 |
int index;
|
|
98 |
int theBytes;
|
|
99 |
int spaceNeeded;
|
|
100 |
|
|
101 |
if (highHalfZoneCode == 0) {
|
|
102 |
inputChar = input[charOff];
|
|
103 |
inputSize = 1;
|
|
104 |
} else {
|
|
105 |
inputChar = highHalfZoneCode;
|
|
106 |
inputSize = 0;
|
|
107 |
highHalfZoneCode = 0;
|
|
108 |
}
|
|
109 |
|
|
110 |
// Is this a high surrogate?
|
3714
|
111 |
if (Character.isHighSurrogate(inputChar)) {
|
2
|
112 |
// Is this the last character of the input?
|
|
113 |
if (charOff + inputSize >= inEnd) {
|
|
114 |
highHalfZoneCode = inputChar;
|
|
115 |
charOff += inputSize;
|
|
116 |
break;
|
|
117 |
}
|
|
118 |
|
|
119 |
// Is there a low surrogate following?
|
|
120 |
inputChar = input[charOff + inputSize];
|
3714
|
121 |
if (Character.isLowSurrogate(inputChar)) {
|
2
|
122 |
// We have a valid surrogate pair. Too bad we don't do
|
|
123 |
// surrogates. Is substitution enabled?
|
|
124 |
if (subMode) {
|
|
125 |
if (subBytes.length == 1) {
|
|
126 |
outputByte[0] = 0x00;
|
|
127 |
outputByte[1] = subBytes[0];
|
|
128 |
}
|
|
129 |
else {
|
|
130 |
outputByte[0] = subBytes[0];
|
|
131 |
outputByte[1] = subBytes[1];
|
|
132 |
}
|
|
133 |
inputSize++;
|
|
134 |
} else {
|
|
135 |
badInputLength = 2;
|
|
136 |
throw new UnknownCharacterException();
|
|
137 |
}
|
|
138 |
} else {
|
|
139 |
// We have a malformed surrogate pair
|
|
140 |
badInputLength = 1;
|
|
141 |
throw new MalformedInputException();
|
|
142 |
}
|
|
143 |
}
|
|
144 |
// Is this an unaccompanied low surrogate?
|
3714
|
145 |
else if (Character.isLowSurrogate(inputChar)) {
|
2921
|
146 |
badInputLength = 1;
|
|
147 |
throw new MalformedInputException();
|
|
148 |
} else {
|
2
|
149 |
|
2921
|
150 |
// We have a valid character, get the bytes for it
|
|
151 |
theBytes = encodeChar(inputChar);
|
|
152 |
if (theBytes == UNMAPPABLE_ENCODING) {
|
|
153 |
// if there was no mapping - look for substitution characters
|
|
154 |
if (subMode) {
|
|
155 |
if (subBytes.length == 1) {
|
|
156 |
outputByte[0] = 0x00;
|
|
157 |
outputByte[1] = subBytes[0];
|
|
158 |
} else {
|
|
159 |
outputByte[0] = subBytes[0];
|
|
160 |
outputByte[1] = subBytes[1];
|
|
161 |
}
|
|
162 |
} else {
|
|
163 |
badInputLength = 1;
|
|
164 |
throw new UnknownCharacterException();
|
|
165 |
}
|
|
166 |
} else {
|
|
167 |
outputByte[0] = (byte)((theBytes & 0x0000ff00)>>8);
|
|
168 |
outputByte[1] = (byte)(theBytes & 0x000000ff);
|
|
169 |
}
|
2
|
170 |
}
|
|
171 |
|
|
172 |
//Set the output buffer into the correct state
|
|
173 |
|
|
174 |
if (currentState == DBCS && outputByte[0] == 0x00) {
|
|
175 |
if (byteOff >= outEnd)
|
|
176 |
throw new ConversionBufferFullException();
|
|
177 |
currentState = SBCS;
|
|
178 |
output[byteOff++] = SI;
|
|
179 |
} else
|
|
180 |
if (currentState == SBCS && outputByte[0] != 0x00) {
|
|
181 |
if (byteOff >= outEnd) {
|
|
182 |
throw new ConversionBufferFullException();
|
|
183 |
}
|
|
184 |
currentState = DBCS;
|
|
185 |
output[byteOff++] = SO;
|
|
186 |
}
|
|
187 |
|
|
188 |
if (currentState == DBCS)
|
|
189 |
spaceNeeded = 2;
|
|
190 |
else
|
|
191 |
spaceNeeded = 1;
|
|
192 |
|
|
193 |
if (byteOff + spaceNeeded > outEnd) {
|
|
194 |
throw new ConversionBufferFullException();
|
|
195 |
}
|
|
196 |
|
|
197 |
if (currentState == SBCS)
|
|
198 |
output[byteOff++] = outputByte[1];
|
|
199 |
else {
|
|
200 |
output[byteOff++] = outputByte[0];
|
|
201 |
output[byteOff++] = outputByte[1];
|
|
202 |
}
|
|
203 |
|
|
204 |
charOff += inputSize;
|
|
205 |
}
|
|
206 |
return byteOff - outOff;
|
|
207 |
}
|
|
208 |
|
|
209 |
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Resets converter to its initial state.
|
|
213 |
*/
|
|
214 |
public void reset() {
|
|
215 |
charOff = byteOff = 0;
|
|
216 |
highHalfZoneCode = 0;
|
|
217 |
currentState = SBCS;
|
|
218 |
}
|
|
219 |
|
|
220 |
|
|
221 |
/**
|
|
222 |
* Returns the maximum number of bytes needed to convert a char.
|
|
223 |
*/
|
|
224 |
public int getMaxBytesPerChar() {
|
|
225 |
return 4; //Fixed with bug 4199599 so tests would pass.
|
|
226 |
}
|
|
227 |
|
|
228 |
|
|
229 |
/**
|
|
230 |
* Sets the substitution bytes to use when the converter is in
|
|
231 |
* substitution mode. The given bytes should represent a valid
|
|
232 |
* character in the target character encoding.
|
|
233 |
*/
|
|
234 |
|
|
235 |
public void setSubstitutionBytes( byte[] newSubBytes )
|
|
236 |
throws IllegalArgumentException
|
|
237 |
{
|
|
238 |
if( newSubBytes.length > 2 || newSubBytes.length == 0) {
|
|
239 |
throw new IllegalArgumentException();
|
|
240 |
}
|
|
241 |
|
|
242 |
subBytes = new byte[ newSubBytes.length ];
|
|
243 |
System.arraycopy( newSubBytes, 0, subBytes, 0, newSubBytes.length );
|
|
244 |
|
|
245 |
}
|
|
246 |
|
|
247 |
/**
|
|
248 |
* Returns true if the given character can be converted to the
|
|
249 |
* target character encoding.
|
|
250 |
*/
|
2921
|
251 |
public boolean canConvert(char c) {
|
|
252 |
return encodeChar(c) != UNMAPPABLE_ENCODING;
|
2
|
253 |
}
|
|
254 |
}
|