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