2
|
1 |
/*
|
|
2 |
* Copyright 2000-2001 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.nio.cs;
|
|
27 |
|
|
28 |
import java.nio.CharBuffer;
|
|
29 |
import java.nio.charset.CoderResult;
|
|
30 |
import java.nio.charset.MalformedInputException;
|
|
31 |
import java.nio.charset.UnmappableCharacterException;
|
|
32 |
|
|
33 |
/**
|
|
34 |
* Utility class for dealing with surrogates.
|
|
35 |
*
|
|
36 |
* @author Mark Reinhold
|
|
37 |
*/
|
|
38 |
|
|
39 |
public class Surrogate {
|
|
40 |
|
|
41 |
private Surrogate() { }
|
|
42 |
|
3324
|
43 |
// TODO: Deprecate/remove the following redundant definitions
|
|
44 |
public static final char MIN_HIGH = Character.MIN_HIGH_SURROGATE;
|
|
45 |
public static final char MAX_HIGH = Character.MAX_HIGH_SURROGATE;
|
|
46 |
public static final char MIN_LOW = Character.MIN_LOW_SURROGATE;
|
|
47 |
public static final char MAX_LOW = Character.MAX_LOW_SURROGATE;
|
|
48 |
public static final char MIN = Character.MIN_SURROGATE;
|
|
49 |
public static final char MAX = Character.MAX_SURROGATE;
|
|
50 |
public static final int UCS4_MIN = Character.MIN_SUPPLEMENTARY_CODE_POINT;
|
|
51 |
public static final int UCS4_MAX = Character.MAX_CODE_POINT;
|
2
|
52 |
|
|
53 |
/**
|
|
54 |
* Tells whether or not the given UTF-16 value is a high surrogate.
|
|
55 |
*/
|
|
56 |
public static boolean isHigh(int c) {
|
|
57 |
return (MIN_HIGH <= c) && (c <= MAX_HIGH);
|
|
58 |
}
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Tells whether or not the given UTF-16 value is a low surrogate.
|
|
62 |
*/
|
|
63 |
public static boolean isLow(int c) {
|
|
64 |
return (MIN_LOW <= c) && (c <= MAX_LOW);
|
|
65 |
}
|
|
66 |
|
|
67 |
/**
|
|
68 |
* Tells whether or not the given UTF-16 value is a surrogate character,
|
|
69 |
*/
|
|
70 |
public static boolean is(int c) {
|
|
71 |
return (MIN <= c) && (c <= MAX);
|
|
72 |
}
|
|
73 |
|
|
74 |
/**
|
3324
|
75 |
* Tells whether or not the given UCS-4 character is in the Basic
|
|
76 |
* Multilingual Plane, and can be represented using a single char.
|
|
77 |
*/
|
|
78 |
public static boolean isBMP(int uc) {
|
|
79 |
return (int) (char) uc == uc;
|
|
80 |
}
|
|
81 |
|
|
82 |
/**
|
2
|
83 |
* Tells whether or not the given UCS-4 character must be represented as a
|
|
84 |
* surrogate pair in UTF-16.
|
|
85 |
*/
|
|
86 |
public static boolean neededFor(int uc) {
|
3324
|
87 |
return Character.isSupplementaryCodePoint(uc);
|
2
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* Returns the high UTF-16 surrogate for the given UCS-4 character.
|
|
92 |
*/
|
|
93 |
public static char high(int uc) {
|
3324
|
94 |
assert Character.isSupplementaryCodePoint(uc);
|
|
95 |
return (char)((uc >> 10)
|
|
96 |
+ (Character.MIN_HIGH_SURROGATE
|
|
97 |
- (Character.MIN_SUPPLEMENTARY_CODE_POINT >> 10)));
|
2
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Returns the low UTF-16 surrogate for the given UCS-4 character.
|
|
102 |
*/
|
|
103 |
public static char low(int uc) {
|
3324
|
104 |
assert Character.isSupplementaryCodePoint(uc);
|
|
105 |
return (char)((uc & 0x3ff) + Character.MIN_LOW_SURROGATE);
|
2
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Converts the given surrogate pair into a 32-bit UCS-4 character.
|
|
110 |
*/
|
|
111 |
public static int toUCS4(char c, char d) {
|
3324
|
112 |
assert Character.isHighSurrogate(c) && Character.isLowSurrogate(d);
|
|
113 |
return Character.toCodePoint(c, d);
|
2
|
114 |
}
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Surrogate parsing support. Charset implementations may use instances of
|
|
118 |
* this class to handle the details of parsing UTF-16 surrogate pairs.
|
|
119 |
*/
|
|
120 |
public static class Parser {
|
|
121 |
|
|
122 |
public Parser() { }
|
|
123 |
|
|
124 |
private int character; // UCS-4
|
|
125 |
private CoderResult error = CoderResult.UNDERFLOW;
|
|
126 |
private boolean isPair;
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Returns the UCS-4 character previously parsed.
|
|
130 |
*/
|
|
131 |
public int character() {
|
|
132 |
assert (error == null);
|
|
133 |
return character;
|
|
134 |
}
|
|
135 |
|
|
136 |
/**
|
|
137 |
* Tells whether or not the previously-parsed UCS-4 character was
|
|
138 |
* originally represented by a surrogate pair.
|
|
139 |
*/
|
|
140 |
public boolean isPair() {
|
|
141 |
assert (error == null);
|
|
142 |
return isPair;
|
|
143 |
}
|
|
144 |
|
|
145 |
/**
|
|
146 |
* Returns the number of UTF-16 characters consumed by the previous
|
|
147 |
* parse.
|
|
148 |
*/
|
|
149 |
public int increment() {
|
|
150 |
assert (error == null);
|
|
151 |
return isPair ? 2 : 1;
|
|
152 |
}
|
|
153 |
|
|
154 |
/**
|
|
155 |
* If the previous parse operation detected an error, return the object
|
|
156 |
* describing that error.
|
|
157 |
*/
|
|
158 |
public CoderResult error() {
|
|
159 |
assert (error != null);
|
|
160 |
return error;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Returns an unmappable-input result object, with the appropriate
|
|
165 |
* input length, for the previously-parsed character.
|
|
166 |
*/
|
|
167 |
public CoderResult unmappableResult() {
|
|
168 |
assert (error == null);
|
|
169 |
return CoderResult.unmappableForLength(isPair ? 2 : 1);
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Parses a UCS-4 character from the given source buffer, handling
|
|
174 |
* surrogates.
|
|
175 |
*
|
|
176 |
* @param c The first character
|
|
177 |
* @param in The source buffer, from which one more character
|
|
178 |
* will be consumed if c is a high surrogate
|
|
179 |
*
|
|
180 |
* @returns Either a parsed UCS-4 character, in which case the isPair()
|
|
181 |
* and increment() methods will return meaningful values, or
|
|
182 |
* -1, in which case error() will return a descriptive result
|
|
183 |
* object
|
|
184 |
*/
|
|
185 |
public int parse(char c, CharBuffer in) {
|
3324
|
186 |
if (Character.isHighSurrogate(c)) {
|
2
|
187 |
if (!in.hasRemaining()) {
|
|
188 |
error = CoderResult.UNDERFLOW;
|
|
189 |
return -1;
|
|
190 |
}
|
|
191 |
char d = in.get();
|
3324
|
192 |
if (Character.isLowSurrogate(d)) {
|
|
193 |
character = Character.toCodePoint(c, d);
|
2
|
194 |
isPair = true;
|
|
195 |
error = null;
|
|
196 |
return character;
|
|
197 |
}
|
|
198 |
error = CoderResult.malformedForLength(1);
|
|
199 |
return -1;
|
|
200 |
}
|
3324
|
201 |
if (Character.isLowSurrogate(c)) {
|
2
|
202 |
error = CoderResult.malformedForLength(1);
|
|
203 |
return -1;
|
|
204 |
}
|
|
205 |
character = c;
|
|
206 |
isPair = false;
|
|
207 |
error = null;
|
|
208 |
return character;
|
|
209 |
}
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Parses a UCS-4 character from the given source buffer, handling
|
|
213 |
* surrogates.
|
|
214 |
*
|
|
215 |
* @param c The first character
|
|
216 |
* @param ia The input array, from which one more character
|
|
217 |
* will be consumed if c is a high surrogate
|
|
218 |
* @param ip The input index
|
|
219 |
* @param il The input limit
|
|
220 |
*
|
|
221 |
* @returns Either a parsed UCS-4 character, in which case the isPair()
|
|
222 |
* and increment() methods will return meaningful values, or
|
|
223 |
* -1, in which case error() will return a descriptive result
|
|
224 |
* object
|
|
225 |
*/
|
|
226 |
public int parse(char c, char[] ia, int ip, int il) {
|
|
227 |
assert (ia[ip] == c);
|
3324
|
228 |
if (Character.isHighSurrogate(c)) {
|
2
|
229 |
if (il - ip < 2) {
|
|
230 |
error = CoderResult.UNDERFLOW;
|
|
231 |
return -1;
|
|
232 |
}
|
|
233 |
char d = ia[ip + 1];
|
3324
|
234 |
if (Character.isLowSurrogate(d)) {
|
|
235 |
character = Character.toCodePoint(c, d);
|
2
|
236 |
isPair = true;
|
|
237 |
error = null;
|
|
238 |
return character;
|
|
239 |
}
|
|
240 |
error = CoderResult.malformedForLength(1);
|
|
241 |
return -1;
|
|
242 |
}
|
3324
|
243 |
if (Character.isLowSurrogate(c)) {
|
2
|
244 |
error = CoderResult.malformedForLength(1);
|
|
245 |
return -1;
|
|
246 |
}
|
|
247 |
character = c;
|
|
248 |
isPair = false;
|
|
249 |
error = null;
|
|
250 |
return character;
|
|
251 |
}
|
|
252 |
|
|
253 |
}
|
|
254 |
|
|
255 |
/**
|
|
256 |
* Surrogate generation support. Charset implementations may use instances
|
|
257 |
* of this class to handle the details of generating UTF-16 surrogate
|
|
258 |
* pairs.
|
|
259 |
*/
|
|
260 |
public static class Generator {
|
|
261 |
|
|
262 |
public Generator() { }
|
|
263 |
|
|
264 |
private CoderResult error = CoderResult.OVERFLOW;
|
|
265 |
|
|
266 |
/**
|
|
267 |
* If the previous generation operation detected an error, return the
|
|
268 |
* object describing that error.
|
|
269 |
*/
|
|
270 |
public CoderResult error() {
|
|
271 |
assert error != null;
|
|
272 |
return error;
|
|
273 |
}
|
|
274 |
|
|
275 |
/**
|
|
276 |
* Generates one or two UTF-16 characters to represent the given UCS-4
|
|
277 |
* character.
|
|
278 |
*
|
|
279 |
* @param uc The UCS-4 character
|
|
280 |
* @param len The number of input bytes from which the UCS-4 value
|
|
281 |
* was constructed (used when creating result objects)
|
|
282 |
* @param dst The destination buffer, to which one or two UTF-16
|
|
283 |
* characters will be written
|
|
284 |
*
|
|
285 |
* @returns Either a positive count of the number of UTF-16 characters
|
|
286 |
* written to the destination buffer, or -1, in which case
|
|
287 |
* error() will return a descriptive result object
|
|
288 |
*/
|
|
289 |
public int generate(int uc, int len, CharBuffer dst) {
|
3324
|
290 |
if (Surrogate.isBMP(uc)) {
|
2
|
291 |
if (Surrogate.is(uc)) {
|
|
292 |
error = CoderResult.malformedForLength(len);
|
|
293 |
return -1;
|
|
294 |
}
|
|
295 |
if (dst.remaining() < 1) {
|
|
296 |
error = CoderResult.OVERFLOW;
|
|
297 |
return -1;
|
|
298 |
}
|
|
299 |
dst.put((char)uc);
|
|
300 |
error = null;
|
|
301 |
return 1;
|
3324
|
302 |
} else if (Character.isSupplementaryCodePoint(uc)) {
|
2
|
303 |
if (dst.remaining() < 2) {
|
|
304 |
error = CoderResult.OVERFLOW;
|
|
305 |
return -1;
|
|
306 |
}
|
|
307 |
dst.put(Surrogate.high(uc));
|
|
308 |
dst.put(Surrogate.low(uc));
|
|
309 |
error = null;
|
|
310 |
return 2;
|
3324
|
311 |
} else {
|
|
312 |
error = CoderResult.unmappableForLength(len);
|
|
313 |
return -1;
|
2
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
/**
|
|
318 |
* Generates one or two UTF-16 characters to represent the given UCS-4
|
|
319 |
* character.
|
|
320 |
*
|
|
321 |
* @param uc The UCS-4 character
|
|
322 |
* @param len The number of input bytes from which the UCS-4 value
|
|
323 |
* was constructed (used when creating result objects)
|
|
324 |
* @param da The destination array, to which one or two UTF-16
|
|
325 |
* characters will be written
|
|
326 |
* @param dp The destination position
|
|
327 |
* @param dl The destination limit
|
|
328 |
*
|
|
329 |
* @returns Either a positive count of the number of UTF-16 characters
|
|
330 |
* written to the destination buffer, or -1, in which case
|
|
331 |
* error() will return a descriptive result object
|
|
332 |
*/
|
|
333 |
public int generate(int uc, int len, char[] da, int dp, int dl) {
|
3324
|
334 |
if (Surrogate.isBMP(uc)) {
|
2
|
335 |
if (Surrogate.is(uc)) {
|
|
336 |
error = CoderResult.malformedForLength(len);
|
|
337 |
return -1;
|
|
338 |
}
|
|
339 |
if (dl - dp < 1) {
|
|
340 |
error = CoderResult.OVERFLOW;
|
|
341 |
return -1;
|
|
342 |
}
|
|
343 |
da[dp] = (char)uc;
|
|
344 |
error = null;
|
|
345 |
return 1;
|
3324
|
346 |
} else if (Character.isSupplementaryCodePoint(uc)) {
|
2
|
347 |
if (dl - dp < 2) {
|
|
348 |
error = CoderResult.OVERFLOW;
|
|
349 |
return -1;
|
|
350 |
}
|
|
351 |
da[dp] = Surrogate.high(uc);
|
|
352 |
da[dp + 1] = Surrogate.low(uc);
|
|
353 |
error = null;
|
|
354 |
return 2;
|
3324
|
355 |
} else {
|
|
356 |
error = CoderResult.unmappableForLength(len);
|
|
357 |
return -1;
|
2
|
358 |
}
|
|
359 |
}
|
|
360 |
}
|
|
361 |
|
|
362 |
}
|