author | ohair |
Tue, 28 Dec 2010 15:53:50 -0800 | |
changeset 7668 | d4a77089c587 |
parent 7542 | 514d58003c20 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7668 | 2 |
* Copyright (c) 2005, 2010, 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.ByteBuffer; |
|
29 |
import java.nio.CharBuffer; |
|
30 |
import java.nio.charset.Charset; |
|
31 |
import java.nio.charset.CoderResult; |
|
32 |
import java.nio.charset.CharsetDecoder; |
|
33 |
import java.nio.charset.CharsetEncoder; |
|
34 |
||
35 |
class UTF_32Coder { |
|
36 |
protected static final int BOM_BIG = 0xFEFF; |
|
37 |
protected static final int BOM_LITTLE = 0xFFFE0000; |
|
38 |
protected static final int NONE = 0; |
|
39 |
protected static final int BIG = 1; |
|
40 |
protected static final int LITTLE = 2; |
|
41 |
||
42 |
protected static class Decoder extends CharsetDecoder { |
|
43 |
private int currentBO; |
|
44 |
private int expectedBO; |
|
45 |
||
46 |
protected Decoder(Charset cs, int bo) { |
|
47 |
super(cs, 0.25f, 1.0f); |
|
48 |
this.expectedBO = bo; |
|
49 |
this.currentBO = NONE; |
|
50 |
} |
|
51 |
||
52 |
private int getCP(ByteBuffer src) { |
|
53 |
return (currentBO==BIG) |
|
54 |
?(((src.get() & 0xff) << 24) | |
|
55 |
((src.get() & 0xff) << 16) | |
|
56 |
((src.get() & 0xff) << 8) | |
|
57 |
(src.get() & 0xff)) |
|
58 |
:((src.get() & 0xff) | |
|
59 |
((src.get() & 0xff) << 8) | |
|
60 |
((src.get() & 0xff) << 16) | |
|
61 |
((src.get() & 0xff) << 24)); |
|
62 |
} |
|
63 |
||
64 |
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { |
|
65 |
if (src.remaining() < 4) |
|
66 |
return CoderResult.UNDERFLOW; |
|
67 |
int mark = src.position(); |
|
68 |
int cp; |
|
69 |
try { |
|
70 |
if (currentBO == NONE) { |
|
71 |
cp = ((src.get() & 0xff) << 24) | |
|
72 |
((src.get() & 0xff) << 16) | |
|
73 |
((src.get() & 0xff) << 8) | |
|
74 |
(src.get() & 0xff); |
|
75 |
if (cp == BOM_BIG && expectedBO != LITTLE) { |
|
76 |
currentBO = BIG; |
|
77 |
mark += 4; |
|
78 |
} else if (cp == BOM_LITTLE && expectedBO != BIG) { |
|
79 |
currentBO = LITTLE; |
|
80 |
mark += 4; |
|
81 |
} else { |
|
82 |
if (expectedBO == NONE) |
|
83 |
currentBO = BIG; |
|
84 |
else |
|
85 |
currentBO = expectedBO; |
|
86 |
src.position(mark); |
|
87 |
} |
|
88 |
} |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
89 |
while (src.remaining() >= 4) { |
2 | 90 |
cp = getCP(src); |
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
91 |
if (Character.isBmpCodePoint(cp)) { |
2 | 92 |
if (!dst.hasRemaining()) |
93 |
return CoderResult.OVERFLOW; |
|
94 |
mark += 4; |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
95 |
dst.put((char) cp); |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
96 |
} else if (Character.isValidCodePoint(cp)) { |
2 | 97 |
if (dst.remaining() < 2) |
98 |
return CoderResult.OVERFLOW; |
|
99 |
mark += 4; |
|
5991
288afdbbca28
6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents:
5986
diff
changeset
|
100 |
dst.put(Character.highSurrogate(cp)); |
288afdbbca28
6933322: Add methods highSurrogate(), lowSurrogate() to class Character
martin
parents:
5986
diff
changeset
|
101 |
dst.put(Character.lowSurrogate(cp)); |
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
102 |
} else { |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
103 |
return CoderResult.malformedForLength(4); |
2 | 104 |
} |
105 |
} |
|
106 |
return CoderResult.UNDERFLOW; |
|
107 |
} finally { |
|
108 |
src.position(mark); |
|
109 |
} |
|
110 |
} |
|
111 |
protected void implReset() { |
|
112 |
currentBO = NONE; |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
protected static class Encoder extends CharsetEncoder { |
|
117 |
private boolean doBOM = false; |
|
118 |
private boolean doneBOM = true; |
|
119 |
private int byteOrder; |
|
120 |
||
121 |
protected void put(int cp, ByteBuffer dst) { |
|
122 |
if (byteOrder==BIG) { |
|
123 |
dst.put((byte)(cp >> 24)); |
|
124 |
dst.put((byte)(cp >> 16)); |
|
125 |
dst.put((byte)(cp >> 8)); |
|
126 |
dst.put((byte)cp); |
|
127 |
} else { |
|
128 |
dst.put((byte)cp); |
|
129 |
dst.put((byte)(cp >> 8)); |
|
130 |
dst.put((byte)(cp >> 16)); |
|
131 |
dst.put((byte)(cp >> 24)); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
protected Encoder(Charset cs, int byteOrder, boolean doBOM) { |
|
136 |
super(cs, 4.0f, |
|
137 |
doBOM?8.0f:4.0f, |
|
138 |
(byteOrder==BIG)?new byte[]{(byte)0, (byte)0, (byte)0xff, (byte)0xfd} |
|
139 |
:new byte[]{(byte)0xfd, (byte)0xff, (byte)0, (byte)0}); |
|
140 |
this.byteOrder = byteOrder; |
|
141 |
this.doBOM = doBOM; |
|
142 |
this.doneBOM = !doBOM; |
|
143 |
} |
|
144 |
||
145 |
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { |
|
146 |
int mark = src.position(); |
|
7542
514d58003c20
6415373: (cs) UnicodeEncoder emits BOM when there are no bytes to encode
sherman
parents:
5991
diff
changeset
|
147 |
if (!doneBOM && src.hasRemaining()) { |
2 | 148 |
if (dst.remaining() < 4) |
149 |
return CoderResult.OVERFLOW; |
|
150 |
put(BOM_BIG, dst); |
|
151 |
doneBOM = true; |
|
152 |
} |
|
153 |
try { |
|
154 |
while (src.hasRemaining()) { |
|
155 |
char c = src.get(); |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
156 |
if (!Character.isSurrogate(c)) { |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
157 |
if (dst.remaining() < 4) |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
158 |
return CoderResult.OVERFLOW; |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
159 |
mark++; |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
160 |
put(c, dst); |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
161 |
} else if (Character.isHighSurrogate(c)) { |
2 | 162 |
if (!src.hasRemaining()) |
163 |
return CoderResult.UNDERFLOW; |
|
164 |
char low = src.get(); |
|
3714 | 165 |
if (Character.isLowSurrogate(low)) { |
2 | 166 |
if (dst.remaining() < 4) |
167 |
return CoderResult.OVERFLOW; |
|
168 |
mark += 2; |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
169 |
put(Character.toCodePoint(c, low), dst); |
2 | 170 |
} else { |
171 |
return CoderResult.malformedForLength(1); |
|
172 |
} |
|
5986
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
173 |
} else { |
04eb44085c00
6934265: Add public method Character.isBmpCodePoint
martin
parents:
5506
diff
changeset
|
174 |
// assert Character.isLowSurrogate(c); |
2 | 175 |
return CoderResult.malformedForLength(1); |
176 |
} |
|
177 |
} |
|
178 |
return CoderResult.UNDERFLOW; |
|
179 |
} finally { |
|
180 |
src.position(mark); |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
protected void implReset() { |
|
185 |
doneBOM = !doBOM; |
|
186 |
} |
|
187 |
||
188 |
} |
|
189 |
} |