author | aleonard |
Thu, 10 Oct 2019 10:28:55 +0100 | |
changeset 58561 | 3968bf3673c5 |
parent 51447 | 8dfed4387312 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58561
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 java.lang; |
|
27 |
||
28 |
import java.io.UnsupportedEncodingException; |
|
29 |
import java.lang.ref.SoftReference; |
|
30 |
import java.nio.ByteBuffer; |
|
31 |
import java.nio.CharBuffer; |
|
32 |
import java.nio.charset.Charset; |
|
33 |
import java.nio.charset.CharsetDecoder; |
|
34 |
import java.nio.charset.CharsetEncoder; |
|
35 |
import java.nio.charset.CharacterCodingException; |
|
36 |
import java.nio.charset.CoderResult; |
|
37 |
import java.nio.charset.CodingErrorAction; |
|
38 |
import java.nio.charset.IllegalCharsetNameException; |
|
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
39 |
import java.nio.charset.MalformedInputException; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
40 |
import java.nio.charset.UnmappableCharacterException; |
2 | 41 |
import java.nio.charset.UnsupportedCharsetException; |
42 |
import java.util.Arrays; |
|
33663 | 43 |
import jdk.internal.HotSpotIntrinsicCandidate; |
2 | 44 |
import sun.nio.cs.HistoricallyNamedCharset; |
2294 | 45 |
import sun.nio.cs.ArrayDecoder; |
46 |
import sun.nio.cs.ArrayEncoder; |
|
2 | 47 |
|
33663 | 48 |
import static java.lang.String.LATIN1; |
49 |
import static java.lang.String.UTF16; |
|
50 |
import static java.lang.String.COMPACT_STRINGS; |
|
48262 | 51 |
import static java.lang.Character.isSurrogate; |
52 |
import static java.lang.Character.highSurrogate; |
|
53 |
import static java.lang.Character.lowSurrogate; |
|
54 |
import static java.lang.Character.isSupplementaryCodePoint; |
|
55 |
import static java.lang.StringUTF16.putChar; |
|
33663 | 56 |
|
2 | 57 |
/** |
58 |
* Utility class for string encoding and decoding. |
|
59 |
*/ |
|
60 |
||
61 |
class StringCoding { |
|
62 |
||
63 |
private StringCoding() { } |
|
64 |
||
41
dfebd2609e77
6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents:
2
diff
changeset
|
65 |
/** The cached coders for each thread */ |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25991
diff
changeset
|
66 |
private static final ThreadLocal<SoftReference<StringDecoder>> decoder = |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
67 |
new ThreadLocal<>(); |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25991
diff
changeset
|
68 |
private static final ThreadLocal<SoftReference<StringEncoder>> encoder = |
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
5506
diff
changeset
|
69 |
new ThreadLocal<>(); |
2 | 70 |
|
49556
809b178407cc
8201179: Regression due loading java.nio.charset.StandardCharsets during bootstrap
redestad
parents:
49443
diff
changeset
|
71 |
private static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE; |
809b178407cc
8201179: Regression due loading java.nio.charset.StandardCharsets during bootstrap
redestad
parents:
49443
diff
changeset
|
72 |
private static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE; |
809b178407cc
8201179: Regression due loading java.nio.charset.StandardCharsets during bootstrap
redestad
parents:
49443
diff
changeset
|
73 |
private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE; |
809b178407cc
8201179: Regression due loading java.nio.charset.StandardCharsets during bootstrap
redestad
parents:
49443
diff
changeset
|
74 |
|
41
dfebd2609e77
6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents:
2
diff
changeset
|
75 |
private static <T> T deref(ThreadLocal<SoftReference<T>> tl) { |
dfebd2609e77
6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents:
2
diff
changeset
|
76 |
SoftReference<T> sr = tl.get(); |
2 | 77 |
if (sr == null) |
78 |
return null; |
|
79 |
return sr.get(); |
|
80 |
} |
|
81 |
||
41
dfebd2609e77
6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents:
2
diff
changeset
|
82 |
private static <T> void set(ThreadLocal<SoftReference<T>> tl, T ob) { |
22581
e868cde95050
8032779: Update code in java.lang to use newer language features
psandoz
parents:
14342
diff
changeset
|
83 |
tl.set(new SoftReference<>(ob)); |
2 | 84 |
} |
85 |
||
86 |
// Trim the given byte array to the given length |
|
33663 | 87 |
private static byte[] safeTrim(byte[] ba, int len, boolean isTrusted) { |
2294 | 88 |
if (len == ba.length && (isTrusted || System.getSecurityManager() == null)) |
2 | 89 |
return ba; |
90 |
else |
|
91 |
return Arrays.copyOf(ba, len); |
|
92 |
} |
|
93 |
||
94 |
private static int scale(int len, float expansionFactor) { |
|
95 |
// We need to perform double, not float, arithmetic; otherwise |
|
96 |
// we lose low order bits when len is larger than 2**24. |
|
97 |
return (int)(len * (double)expansionFactor); |
|
98 |
} |
|
99 |
||
100 |
private static Charset lookupCharset(String csn) { |
|
101 |
if (Charset.isSupported(csn)) { |
|
102 |
try { |
|
103 |
return Charset.forName(csn); |
|
104 |
} catch (UnsupportedCharsetException x) { |
|
105 |
throw new Error(x); |
|
106 |
} |
|
107 |
} |
|
108 |
return null; |
|
109 |
} |
|
110 |
||
33663 | 111 |
static class Result { |
112 |
byte[] value; |
|
113 |
byte coder; |
|
114 |
||
115 |
Result with() { |
|
116 |
coder = COMPACT_STRINGS ? LATIN1 : UTF16; |
|
117 |
value = new byte[0]; |
|
118 |
return this; |
|
119 |
} |
|
120 |
||
121 |
Result with(char[] val, int off, int len) { |
|
122 |
if (String.COMPACT_STRINGS) { |
|
123 |
byte[] bs = StringUTF16.compress(val, off, len); |
|
124 |
if (bs != null) { |
|
125 |
value = bs; |
|
126 |
coder = LATIN1; |
|
127 |
return this; |
|
128 |
} |
|
129 |
} |
|
130 |
coder = UTF16; |
|
131 |
value = StringUTF16.toBytes(val, off, len); |
|
132 |
return this; |
|
133 |
} |
|
134 |
||
135 |
Result with(byte[] val, byte coder) { |
|
136 |
this.coder = coder; |
|
137 |
value = val; |
|
138 |
return this; |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
@HotSpotIntrinsicCandidate |
|
36929
0dc62e7c6a29
8144693: Intrinsify StringCoding.hasNegatives() on SPARC
thartmann
parents:
34885
diff
changeset
|
143 |
public static boolean hasNegatives(byte[] ba, int off, int len) { |
33663 | 144 |
for (int i = off; i < off + len; i++) { |
145 |
if (ba[i] < 0) { |
|
146 |
return true; |
|
147 |
} |
|
148 |
} |
|
149 |
return false; |
|
150 |
} |
|
2 | 151 |
|
152 |
// -- Decoding -- |
|
33663 | 153 |
static class StringDecoder { |
2 | 154 |
private final String requestedCharsetName; |
155 |
private final Charset cs; |
|
33663 | 156 |
private final boolean isASCIICompatible; |
2 | 157 |
private final CharsetDecoder cd; |
33663 | 158 |
protected final Result result; |
2 | 159 |
|
33663 | 160 |
StringDecoder(Charset cs, String rcn) { |
2 | 161 |
this.requestedCharsetName = rcn; |
162 |
this.cs = cs; |
|
163 |
this.cd = cs.newDecoder() |
|
164 |
.onMalformedInput(CodingErrorAction.REPLACE) |
|
165 |
.onUnmappableCharacter(CodingErrorAction.REPLACE); |
|
33663 | 166 |
this.result = new Result(); |
167 |
this.isASCIICompatible = (cd instanceof ArrayDecoder) && |
|
168 |
((ArrayDecoder)cd).isASCIICompatible(); |
|
2 | 169 |
} |
170 |
||
171 |
String charsetName() { |
|
172 |
if (cs instanceof HistoricallyNamedCharset) |
|
173 |
return ((HistoricallyNamedCharset)cs).historicalName(); |
|
174 |
return cs.name(); |
|
175 |
} |
|
176 |
||
177 |
final String requestedCharsetName() { |
|
178 |
return requestedCharsetName; |
|
179 |
} |
|
180 |
||
33663 | 181 |
Result decode(byte[] ba, int off, int len) { |
182 |
if (len == 0) { |
|
183 |
return result.with(); |
|
184 |
} |
|
185 |
// fastpath for ascii compatible |
|
186 |
if (isASCIICompatible && !hasNegatives(ba, off, len)) { |
|
187 |
if (COMPACT_STRINGS) { |
|
188 |
return result.with(Arrays.copyOfRange(ba, off, off + len), |
|
189 |
LATIN1); |
|
190 |
} else { |
|
191 |
return result.with(StringLatin1.inflate(ba, off, len), UTF16); |
|
192 |
} |
|
193 |
} |
|
58561
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
194 |
// fastpath for always Latin1 decodable single byte |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
195 |
if (COMPACT_STRINGS && cd instanceof ArrayDecoder && ((ArrayDecoder)cd).isLatin1Decodable()) { |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
196 |
byte[] dst = new byte[len]; |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
197 |
((ArrayDecoder)cd).decodeToLatin1(ba, off, len, dst); |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
198 |
return result.with(dst, LATIN1); |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
199 |
} |
2 | 200 |
int en = scale(len, cd.maxCharsPerByte()); |
201 |
char[] ca = new char[en]; |
|
2294 | 202 |
if (cd instanceof ArrayDecoder) { |
203 |
int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca); |
|
33663 | 204 |
return result.with(ca, 0, clen); |
205 |
} |
|
206 |
cd.reset(); |
|
207 |
ByteBuffer bb = ByteBuffer.wrap(ba, off, len); |
|
208 |
CharBuffer cb = CharBuffer.wrap(ca); |
|
209 |
try { |
|
210 |
CoderResult cr = cd.decode(bb, cb, true); |
|
211 |
if (!cr.isUnderflow()) |
|
212 |
cr.throwException(); |
|
213 |
cr = cd.flush(cb); |
|
214 |
if (!cr.isUnderflow()) |
|
215 |
cr.throwException(); |
|
216 |
} catch (CharacterCodingException x) { |
|
217 |
// Substitution is always enabled, |
|
218 |
// so this shouldn't happen |
|
219 |
throw new Error(x); |
|
220 |
} |
|
221 |
return result.with(ca, 0, cb.position()); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
static Result decode(String charsetName, byte[] ba, int off, int len) |
|
2 | 226 |
throws UnsupportedEncodingException |
227 |
{ |
|
41
dfebd2609e77
6671834: (str) Eliminate StringCoding.java compile warnings
martin
parents:
2
diff
changeset
|
228 |
StringDecoder sd = deref(decoder); |
2 | 229 |
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName; |
230 |
if ((sd == null) || !(csn.equals(sd.requestedCharsetName()) |
|
231 |
|| csn.equals(sd.charsetName()))) { |
|
232 |
sd = null; |
|
233 |
try { |
|
234 |
Charset cs = lookupCharset(csn); |
|
33663 | 235 |
if (cs != null) { |
236 |
if (cs == UTF_8) { |
|
48262 | 237 |
return decodeUTF8(ba, off, len, true); |
238 |
} |
|
239 |
if (cs == ISO_8859_1) { |
|
240 |
return decodeLatin1(ba, off, len); |
|
33663 | 241 |
} |
48262 | 242 |
if (cs == US_ASCII) { |
243 |
return decodeASCII(ba, off, len); |
|
244 |
} |
|
245 |
sd = new StringDecoder(cs, csn); |
|
33663 | 246 |
} |
2 | 247 |
} catch (IllegalCharsetNameException x) {} |
248 |
if (sd == null) |
|
249 |
throw new UnsupportedEncodingException(csn); |
|
250 |
set(decoder, sd); |
|
251 |
} |
|
252 |
return sd.decode(ba, off, len); |
|
253 |
} |
|
254 |
||
33663 | 255 |
static Result decode(Charset cs, byte[] ba, int off, int len) { |
48262 | 256 |
if (cs == UTF_8) { |
257 |
return decodeUTF8(ba, off, len, true); |
|
258 |
} |
|
259 |
if (cs == ISO_8859_1) { |
|
260 |
return decodeLatin1(ba, off, len); |
|
261 |
} |
|
262 |
if (cs == US_ASCII) { |
|
263 |
return decodeASCII(ba, off, len); |
|
264 |
} |
|
265 |
||
2294 | 266 |
// (1)We never cache the "external" cs, the only benefit of creating |
267 |
// an additional StringDe/Encoder object to wrap it is to share the |
|
25979 | 268 |
// de/encode() method. These SD/E objects are short-lived, the young-gen |
269 |
// gc should be able to take care of them well. But the best approach |
|
2294 | 270 |
// is still not to generate them if not really necessary. |
271 |
// (2)The defensive copy of the input byte/char[] has a big performance |
|
272 |
// impact, as well as the outgoing result byte/char[]. Need to do the |
|
273 |
// optimization check of (sm==null && classLoader0==null) for both. |
|
44477
3d2229b0037b
8177631: Outdated performance advice in StringCoding
redestad
parents:
43790
diff
changeset
|
274 |
// (3)There might be a timing gap in isTrusted setting. getClassLoader0() |
25979 | 275 |
// is only checked (and then isTrusted gets set) when (SM==null). It is |
2294 | 276 |
// possible that the SM==null for now but then SM is NOT null later |
277 |
// when safeTrim() is invoked...the "safe" way to do is to redundant |
|
278 |
// check (... && (isTrusted || SM == null || getClassLoader0())) in trim |
|
25979 | 279 |
// but it then can be argued that the SM is null when the operation |
2294 | 280 |
// is started... |
281 |
CharsetDecoder cd = cs.newDecoder(); |
|
33663 | 282 |
// ascii fastpath |
48262 | 283 |
if ((cd instanceof ArrayDecoder) && |
284 |
((ArrayDecoder)cd).isASCIICompatible() && !hasNegatives(ba, off, len)) { |
|
285 |
return decodeLatin1(ba, off, len); |
|
33663 | 286 |
} |
58561
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
287 |
// fastpath for always Latin1 decodable single byte |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
288 |
if (COMPACT_STRINGS && cd instanceof ArrayDecoder && ((ArrayDecoder)cd).isLatin1Decodable()) { |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
289 |
byte[] dst = new byte[len]; |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
290 |
((ArrayDecoder)cd).decodeToLatin1(ba, off, len, dst); |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
291 |
return new Result().with(dst, LATIN1); |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
292 |
} |
3968bf3673c5
8231717: Improve performance of charset decoding when charset is always compactable
aleonard
parents:
51447
diff
changeset
|
293 |
|
2294 | 294 |
int en = scale(len, cd.maxCharsPerByte()); |
33663 | 295 |
if (len == 0) { |
296 |
return new Result().with(); |
|
297 |
} |
|
9547
454881baaca0
7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents:
9035
diff
changeset
|
298 |
cd.onMalformedInput(CodingErrorAction.REPLACE) |
454881baaca0
7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents:
9035
diff
changeset
|
299 |
.onUnmappableCharacter(CodingErrorAction.REPLACE) |
454881baaca0
7040220: java/char_encodin Optimize UTF-8 charset for String.getBytes()/new String(byte[])
sherman
parents:
9035
diff
changeset
|
300 |
.reset(); |
33663 | 301 |
char[] ca = new char[en]; |
2294 | 302 |
if (cd instanceof ArrayDecoder) { |
303 |
int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca); |
|
33663 | 304 |
return new Result().with(ca, 0, clen); |
2294 | 305 |
} |
48262 | 306 |
if (cs.getClass().getClassLoader0() != null && |
307 |
System.getSecurityManager() != null) { |
|
308 |
ba = Arrays.copyOfRange(ba, off, off + len); |
|
309 |
off = 0; |
|
310 |
} |
|
33663 | 311 |
ByteBuffer bb = ByteBuffer.wrap(ba, off, len); |
312 |
CharBuffer cb = CharBuffer.wrap(ca); |
|
313 |
try { |
|
314 |
CoderResult cr = cd.decode(bb, cb, true); |
|
315 |
if (!cr.isUnderflow()) |
|
316 |
cr.throwException(); |
|
317 |
cr = cd.flush(cb); |
|
318 |
if (!cr.isUnderflow()) |
|
319 |
cr.throwException(); |
|
320 |
} catch (CharacterCodingException x) { |
|
321 |
// Substitution is always enabled, |
|
322 |
// so this shouldn't happen |
|
323 |
throw new Error(x); |
|
324 |
} |
|
325 |
return new Result().with(ca, 0, cb.position()); |
|
2 | 326 |
} |
327 |
||
33663 | 328 |
static Result decode(byte[] ba, int off, int len) { |
48262 | 329 |
Charset cs = Charset.defaultCharset(); |
330 |
if (cs == UTF_8) { |
|
331 |
return decodeUTF8(ba, off, len, true); |
|
332 |
} |
|
333 |
if (cs == ISO_8859_1) { |
|
334 |
return decodeLatin1(ba, off, len); |
|
2 | 335 |
} |
48262 | 336 |
if (cs == US_ASCII) { |
337 |
return decodeASCII(ba, off, len); |
|
2 | 338 |
} |
48262 | 339 |
StringDecoder sd = deref(decoder); |
340 |
if (sd == null || !cs.name().equals(sd.cs.name())) { |
|
341 |
sd = new StringDecoder(cs, cs.name()); |
|
342 |
set(decoder, sd); |
|
343 |
} |
|
344 |
return sd.decode(ba, off, len); |
|
2 | 345 |
} |
346 |
||
347 |
// -- Encoding -- |
|
348 |
private static class StringEncoder { |
|
349 |
private Charset cs; |
|
350 |
private CharsetEncoder ce; |
|
33663 | 351 |
private final boolean isASCIICompatible; |
2 | 352 |
private final String requestedCharsetName; |
2294 | 353 |
private final boolean isTrusted; |
2 | 354 |
|
355 |
private StringEncoder(Charset cs, String rcn) { |
|
356 |
this.requestedCharsetName = rcn; |
|
357 |
this.cs = cs; |
|
358 |
this.ce = cs.newEncoder() |
|
359 |
.onMalformedInput(CodingErrorAction.REPLACE) |
|
360 |
.onUnmappableCharacter(CodingErrorAction.REPLACE); |
|
2294 | 361 |
this.isTrusted = (cs.getClass().getClassLoader0() == null); |
33663 | 362 |
this.isASCIICompatible = (ce instanceof ArrayEncoder) && |
363 |
((ArrayEncoder)ce).isASCIICompatible(); |
|
2 | 364 |
} |
365 |
||
366 |
String charsetName() { |
|
367 |
if (cs instanceof HistoricallyNamedCharset) |
|
368 |
return ((HistoricallyNamedCharset)cs).historicalName(); |
|
369 |
return cs.name(); |
|
370 |
} |
|
371 |
||
372 |
final String requestedCharsetName() { |
|
373 |
return requestedCharsetName; |
|
374 |
} |
|
375 |
||
33663 | 376 |
byte[] encode(byte coder, byte[] val) { |
377 |
// fastpath for ascii compatible |
|
378 |
if (coder == LATIN1 && isASCIICompatible && |
|
379 |
!hasNegatives(val, 0, val.length)) { |
|
380 |
return Arrays.copyOf(val, val.length); |
|
381 |
} |
|
382 |
int len = val.length >> coder; // assume LATIN1=0/UTF16=1; |
|
2 | 383 |
int en = scale(len, ce.maxBytesPerChar()); |
384 |
byte[] ba = new byte[en]; |
|
33663 | 385 |
if (len == 0) { |
2 | 386 |
return ba; |
33663 | 387 |
} |
2294 | 388 |
if (ce instanceof ArrayEncoder) { |
33663 | 389 |
int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba) |
390 |
: ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba); |
|
391 |
if (blen != -1) { |
|
392 |
return safeTrim(ba, blen, isTrusted); |
|
2294 | 393 |
} |
2 | 394 |
} |
33663 | 395 |
char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val) |
396 |
: StringUTF16.toChars(val); |
|
397 |
ce.reset(); |
|
398 |
ByteBuffer bb = ByteBuffer.wrap(ba); |
|
399 |
CharBuffer cb = CharBuffer.wrap(ca, 0, len); |
|
400 |
try { |
|
401 |
CoderResult cr = ce.encode(cb, bb, true); |
|
402 |
if (!cr.isUnderflow()) |
|
403 |
cr.throwException(); |
|
404 |
cr = ce.flush(bb); |
|
405 |
if (!cr.isUnderflow()) |
|
406 |
cr.throwException(); |
|
407 |
} catch (CharacterCodingException x) { |
|
408 |
// Substitution is always enabled, |
|
409 |
// so this shouldn't happen |
|
410 |
throw new Error(x); |
|
411 |
} |
|
412 |
return safeTrim(ba, bb.position(), isTrusted); |
|
2 | 413 |
} |
414 |
} |
|
415 |
||
48262 | 416 |
static byte[] encode(String charsetName, byte coder, byte[] val) |
417 |
throws UnsupportedEncodingException |
|
418 |
{ |
|
419 |
StringEncoder se = deref(encoder); |
|
420 |
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName; |
|
421 |
if ((se == null) || !(csn.equals(se.requestedCharsetName()) |
|
422 |
|| csn.equals(se.charsetName()))) { |
|
423 |
se = null; |
|
424 |
try { |
|
425 |
Charset cs = lookupCharset(csn); |
|
426 |
if (cs != null) { |
|
427 |
if (cs == UTF_8) { |
|
428 |
return encodeUTF8(coder, val, true); |
|
429 |
} |
|
430 |
if (cs == ISO_8859_1) { |
|
431 |
return encode8859_1(coder, val); |
|
432 |
} |
|
433 |
if (cs == US_ASCII) { |
|
434 |
return encodeASCII(coder, val); |
|
435 |
} |
|
436 |
se = new StringEncoder(cs, csn); |
|
437 |
} |
|
438 |
} catch (IllegalCharsetNameException x) {} |
|
439 |
if (se == null) { |
|
440 |
throw new UnsupportedEncodingException (csn); |
|
441 |
} |
|
442 |
set(encoder, se); |
|
33663 | 443 |
} |
48262 | 444 |
return se.encode(coder, val); |
33663 | 445 |
} |
446 |
||
48262 | 447 |
static byte[] encode(Charset cs, byte coder, byte[] val) { |
448 |
if (cs == UTF_8) { |
|
449 |
return encodeUTF8(coder, val, true); |
|
450 |
} |
|
451 |
if (cs == ISO_8859_1) { |
|
452 |
return encode8859_1(coder, val); |
|
453 |
} |
|
454 |
if (cs == US_ASCII) { |
|
455 |
return encodeASCII(coder, val); |
|
456 |
} |
|
457 |
CharsetEncoder ce = cs.newEncoder(); |
|
458 |
// fastpath for ascii compatible |
|
459 |
if (coder == LATIN1 && (((ce instanceof ArrayEncoder) && |
|
460 |
((ArrayEncoder)ce).isASCIICompatible() && |
|
461 |
!hasNegatives(val, 0, val.length)))) { |
|
33663 | 462 |
return Arrays.copyOf(val, val.length); |
463 |
} |
|
48262 | 464 |
int len = val.length >> coder; // assume LATIN1=0/UTF16=1; |
465 |
int en = scale(len, ce.maxBytesPerChar()); |
|
466 |
byte[] ba = new byte[en]; |
|
467 |
if (len == 0) { |
|
468 |
return ba; |
|
469 |
} |
|
470 |
ce.onMalformedInput(CodingErrorAction.REPLACE) |
|
471 |
.onUnmappableCharacter(CodingErrorAction.REPLACE) |
|
472 |
.reset(); |
|
473 |
if (ce instanceof ArrayEncoder) { |
|
474 |
int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba) |
|
475 |
: ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba); |
|
476 |
if (blen != -1) { |
|
477 |
return safeTrim(ba, blen, true); |
|
33663 | 478 |
} |
479 |
} |
|
48262 | 480 |
boolean isTrusted = cs.getClass().getClassLoader0() == null || |
481 |
System.getSecurityManager() == null; |
|
482 |
char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val) |
|
483 |
: StringUTF16.toChars(val); |
|
484 |
ByteBuffer bb = ByteBuffer.wrap(ba); |
|
485 |
CharBuffer cb = CharBuffer.wrap(ca, 0, len); |
|
486 |
try { |
|
487 |
CoderResult cr = ce.encode(cb, bb, true); |
|
488 |
if (!cr.isUnderflow()) |
|
489 |
cr.throwException(); |
|
490 |
cr = ce.flush(bb); |
|
491 |
if (!cr.isUnderflow()) |
|
492 |
cr.throwException(); |
|
493 |
} catch (CharacterCodingException x) { |
|
494 |
throw new Error(x); |
|
33663 | 495 |
} |
48262 | 496 |
return safeTrim(ba, bb.position(), isTrusted); |
33663 | 497 |
} |
498 |
||
48262 | 499 |
static byte[] encode(byte coder, byte[] val) { |
500 |
Charset cs = Charset.defaultCharset(); |
|
501 |
if (cs == UTF_8) { |
|
502 |
return encodeUTF8(coder, val, true); |
|
503 |
} |
|
504 |
if (cs == ISO_8859_1) { |
|
505 |
return encode8859_1(coder, val); |
|
506 |
} |
|
507 |
if (cs == US_ASCII) { |
|
508 |
return encodeASCII(coder, val); |
|
509 |
} |
|
510 |
StringEncoder se = deref(encoder); |
|
511 |
if (se == null || !cs.name().equals(se.cs.name())) { |
|
512 |
se = new StringEncoder(cs, cs.name()); |
|
513 |
set(encoder, se); |
|
514 |
} |
|
515 |
return se.encode(coder, val); |
|
516 |
} |
|
517 |
||
518 |
/** |
|
519 |
* Print a message directly to stderr, bypassing all character conversion |
|
520 |
* methods. |
|
521 |
* @param msg message to print |
|
522 |
*/ |
|
523 |
private static native void err(String msg); |
|
524 |
||
525 |
/* The cached Result for each thread */ |
|
526 |
private static final ThreadLocal<StringCoding.Result> |
|
527 |
resultCached = new ThreadLocal<>() { |
|
528 |
protected StringCoding.Result initialValue() { |
|
529 |
return new StringCoding.Result(); |
|
530 |
}}; |
|
531 |
||
532 |
////////////////////////// ascii ////////////////////////////// |
|
533 |
||
534 |
private static Result decodeASCII(byte[] ba, int off, int len) { |
|
535 |
Result result = resultCached.get(); |
|
536 |
if (COMPACT_STRINGS && !hasNegatives(ba, off, len)) { |
|
537 |
return result.with(Arrays.copyOfRange(ba, off, off + len), |
|
538 |
LATIN1); |
|
539 |
} |
|
540 |
byte[] dst = new byte[len<<1]; |
|
541 |
int dp = 0; |
|
542 |
while (dp < len) { |
|
543 |
int b = ba[off++]; |
|
544 |
putChar(dst, dp++, (b >= 0) ? (char)b : repl); |
|
545 |
} |
|
546 |
return result.with(dst, UTF16); |
|
547 |
} |
|
548 |
||
549 |
private static byte[] encodeASCII(byte coder, byte[] val) { |
|
33663 | 550 |
if (coder == LATIN1) { |
551 |
byte[] dst = new byte[val.length]; |
|
552 |
for (int i = 0; i < val.length; i++) { |
|
553 |
if (val[i] < 0) { |
|
554 |
dst[i] = '?'; |
|
555 |
} else { |
|
556 |
dst[i] = val[i]; |
|
557 |
} |
|
558 |
} |
|
559 |
return dst; |
|
560 |
} |
|
561 |
int len = val.length >> 1; |
|
562 |
byte[] dst = new byte[len]; |
|
563 |
int dp = 0; |
|
564 |
for (int i = 0; i < len; i++) { |
|
565 |
char c = StringUTF16.getChar(val, i); |
|
566 |
if (c < 0x80) { |
|
567 |
dst[dp++] = (byte)c; |
|
568 |
continue; |
|
569 |
} |
|
570 |
if (Character.isHighSurrogate(c) && i + 1 < len && |
|
571 |
Character.isLowSurrogate(StringUTF16.getChar(val, i + 1))) { |
|
572 |
i++; |
|
573 |
} |
|
574 |
dst[dp++] = '?'; |
|
575 |
} |
|
576 |
if (len == dp) { |
|
577 |
return dst; |
|
578 |
} |
|
579 |
return Arrays.copyOf(dst, dp); |
|
580 |
} |
|
581 |
||
48262 | 582 |
////////////////////////// latin1/8859_1 /////////////////////////// |
583 |
||
584 |
private static Result decodeLatin1(byte[] ba, int off, int len) { |
|
585 |
Result result = resultCached.get(); |
|
586 |
if (COMPACT_STRINGS) { |
|
587 |
return result.with(Arrays.copyOfRange(ba, off, off + len), LATIN1); |
|
588 |
} else { |
|
589 |
return result.with(StringLatin1.inflate(ba, off, len), UTF16); |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
@HotSpotIntrinsicCandidate |
|
594 |
private static int implEncodeISOArray(byte[] sa, int sp, |
|
595 |
byte[] da, int dp, int len) { |
|
596 |
int i = 0; |
|
597 |
for (; i < len; i++) { |
|
598 |
char c = StringUTF16.getChar(sa, sp++); |
|
599 |
if (c > '\u00FF') |
|
600 |
break; |
|
601 |
da[dp++] = (byte)c; |
|
602 |
} |
|
603 |
return i; |
|
604 |
} |
|
605 |
||
606 |
private static byte[] encode8859_1(byte coder, byte[] val) { |
|
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
607 |
return encode8859_1(coder, val, true); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
608 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
609 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
610 |
private static byte[] encode8859_1(byte coder, byte[] val, boolean doReplace) { |
33663 | 611 |
if (coder == LATIN1) { |
48262 | 612 |
return Arrays.copyOf(val, val.length); |
613 |
} |
|
614 |
int len = val.length >> 1; |
|
615 |
byte[] dst = new byte[len]; |
|
616 |
int dp = 0; |
|
617 |
int sp = 0; |
|
618 |
int sl = len; |
|
619 |
while (sp < sl) { |
|
620 |
int ret = implEncodeISOArray(val, sp, dst, dp, len); |
|
621 |
sp = sp + ret; |
|
622 |
dp = dp + ret; |
|
623 |
if (ret != len) { |
|
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
624 |
if (!doReplace) { |
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
625 |
throwUnmappable(sp, 1); |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
626 |
} |
48262 | 627 |
char c = StringUTF16.getChar(val, sp++); |
628 |
if (Character.isHighSurrogate(c) && sp < sl && |
|
629 |
Character.isLowSurrogate(StringUTF16.getChar(val, sp))) { |
|
630 |
sp++; |
|
33663 | 631 |
} |
48262 | 632 |
dst[dp++] = '?'; |
633 |
len = sl - sp; |
|
33663 | 634 |
} |
635 |
} |
|
636 |
if (dp == dst.length) { |
|
637 |
return dst; |
|
638 |
} |
|
639 |
return Arrays.copyOf(dst, dp); |
|
640 |
} |
|
641 |
||
48262 | 642 |
//////////////////////////////// utf8 //////////////////////////////////// |
643 |
||
644 |
private static boolean isNotContinuation(int b) { |
|
645 |
return (b & 0xc0) != 0x80; |
|
646 |
} |
|
647 |
||
648 |
private static boolean isMalformed3(int b1, int b2, int b3) { |
|
649 |
return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) || |
|
650 |
(b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80; |
|
651 |
} |
|
652 |
||
653 |
private static boolean isMalformed3_2(int b1, int b2) { |
|
654 |
return (b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) || |
|
655 |
(b2 & 0xc0) != 0x80; |
|
656 |
} |
|
657 |
||
658 |
private static boolean isMalformed4(int b2, int b3, int b4) { |
|
659 |
return (b2 & 0xc0) != 0x80 || (b3 & 0xc0) != 0x80 || |
|
660 |
(b4 & 0xc0) != 0x80; |
|
661 |
} |
|
662 |
||
663 |
private static boolean isMalformed4_2(int b1, int b2) { |
|
664 |
return (b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) || |
|
665 |
(b1 == 0xf4 && (b2 & 0xf0) != 0x80) || |
|
666 |
(b2 & 0xc0) != 0x80; |
|
667 |
} |
|
668 |
||
669 |
private static boolean isMalformed4_3(int b3) { |
|
670 |
return (b3 & 0xc0) != 0x80; |
|
671 |
} |
|
672 |
||
673 |
// for nb == 3/4 |
|
674 |
private static int malformedN(byte[] src, int sp, int nb) { |
|
675 |
if (nb == 3) { |
|
676 |
int b1 = src[sp++]; |
|
677 |
int b2 = src[sp++]; // no need to lookup b3 |
|
678 |
return ((b1 == (byte)0xe0 && (b2 & 0xe0) == 0x80) || |
|
679 |
isNotContinuation(b2)) ? 1 : 2; |
|
680 |
} else if (nb == 4) { // we don't care the speed here |
|
681 |
int b1 = src[sp++] & 0xff; |
|
682 |
int b2 = src[sp++] & 0xff; |
|
683 |
if (b1 > 0xf4 || |
|
684 |
(b1 == 0xf0 && (b2 < 0x90 || b2 > 0xbf)) || |
|
685 |
(b1 == 0xf4 && (b2 & 0xf0) != 0x80) || |
|
686 |
isNotContinuation(b2)) |
|
687 |
return 1; |
|
688 |
if (isNotContinuation(src[sp++])) |
|
689 |
return 2; |
|
690 |
return 3; |
|
2 | 691 |
} |
48262 | 692 |
assert false; |
693 |
return -1; |
|
694 |
} |
|
695 |
||
696 |
private static void throwMalformed(int off, int nb) { |
|
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
697 |
String msg = "malformed input off : " + off + ", length : " + nb; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
698 |
throw new IllegalArgumentException(msg, new MalformedInputException(nb)); |
48262 | 699 |
} |
700 |
||
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
701 |
private static void throwMalformed(byte[] val) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
702 |
int dp = 0; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
703 |
while (dp < val.length && val[dp] >=0) { dp++; } |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
704 |
throwMalformed(dp, 1); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
705 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
706 |
|
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
707 |
private static void throwUnmappable(int off, int nb) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
708 |
String msg = "malformed input off : " + off + ", length : " + nb; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
709 |
throw new IllegalArgumentException(msg, new UnmappableCharacterException(nb)); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
710 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
711 |
|
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
712 |
private static void throwUnmappable(byte[] val) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
713 |
int dp = 0; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
714 |
while (dp < val.length && val[dp] >=0) { dp++; } |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
715 |
throwUnmappable(dp, 1); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
716 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
717 |
|
48262 | 718 |
private static char repl = '\ufffd'; |
719 |
||
720 |
private static Result decodeUTF8(byte[] src, int sp, int len, boolean doReplace) { |
|
721 |
// ascii-bais, which has a relative impact to the non-ascii-only bytes |
|
722 |
if (COMPACT_STRINGS && !hasNegatives(src, sp, len)) |
|
723 |
return resultCached.get().with(Arrays.copyOfRange(src, sp, sp + len), |
|
724 |
LATIN1); |
|
725 |
return decodeUTF8_0(src, sp, len, doReplace); |
|
2 | 726 |
} |
727 |
||
48262 | 728 |
private static Result decodeUTF8_0(byte[] src, int sp, int len, boolean doReplace) { |
729 |
Result ret = resultCached.get(); |
|
730 |
||
731 |
int sl = sp + len; |
|
732 |
int dp = 0; |
|
733 |
byte[] dst = new byte[len]; |
|
734 |
||
735 |
if (COMPACT_STRINGS) { |
|
736 |
while (sp < sl) { |
|
737 |
int b1 = src[sp]; |
|
738 |
if (b1 >= 0) { |
|
739 |
dst[dp++] = (byte)b1; |
|
740 |
sp++; |
|
741 |
continue; |
|
742 |
} |
|
743 |
if ((b1 == (byte)0xc2 || b1 == (byte)0xc3) && |
|
744 |
sp + 1 < sl) { |
|
745 |
int b2 = src[sp + 1]; |
|
746 |
if (!isNotContinuation(b2)) { |
|
747 |
dst[dp++] = (byte)(((b1 << 6) ^ b2)^ |
|
748 |
(((byte) 0xC0 << 6) ^ |
|
749 |
((byte) 0x80 << 0))); |
|
750 |
sp += 2; |
|
751 |
continue; |
|
752 |
} |
|
753 |
} |
|
754 |
// anything not a latin1, including the repl |
|
755 |
// we have to go with the utf16 |
|
756 |
break; |
|
33663 | 757 |
} |
48262 | 758 |
if (sp == sl) { |
759 |
if (dp != dst.length) { |
|
760 |
dst = Arrays.copyOf(dst, dp); |
|
761 |
} |
|
762 |
return ret.with(dst, LATIN1); |
|
2294 | 763 |
} |
764 |
} |
|
48262 | 765 |
if (dp == 0) { |
766 |
dst = new byte[len << 1]; |
|
767 |
} else { |
|
768 |
byte[] buf = new byte[len << 1]; |
|
769 |
StringLatin1.inflate(dst, 0, buf, 0, dp); |
|
770 |
dst = buf; |
|
33663 | 771 |
} |
48262 | 772 |
while (sp < sl) { |
773 |
int b1 = src[sp++]; |
|
774 |
if (b1 >= 0) { |
|
775 |
putChar(dst, dp++, (char) b1); |
|
776 |
} else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) { |
|
777 |
if (sp < sl) { |
|
778 |
int b2 = src[sp++]; |
|
779 |
if (isNotContinuation(b2)) { |
|
780 |
if (!doReplace) { |
|
781 |
throwMalformed(sp - 1, 1); |
|
782 |
} |
|
783 |
putChar(dst, dp++, repl); |
|
784 |
sp--; |
|
785 |
} else { |
|
786 |
putChar(dst, dp++, (char)(((b1 << 6) ^ b2)^ |
|
787 |
(((byte) 0xC0 << 6) ^ |
|
788 |
((byte) 0x80 << 0)))); |
|
789 |
} |
|
790 |
continue; |
|
791 |
} |
|
792 |
if (!doReplace) { |
|
793 |
throwMalformed(sp, 1); // underflow() |
|
794 |
} |
|
795 |
putChar(dst, dp++, repl); |
|
796 |
break; |
|
797 |
} else if ((b1 >> 4) == -2) { |
|
798 |
if (sp + 1 < sl) { |
|
799 |
int b2 = src[sp++]; |
|
800 |
int b3 = src[sp++]; |
|
801 |
if (isMalformed3(b1, b2, b3)) { |
|
802 |
if (!doReplace) { |
|
803 |
throwMalformed(sp - 3, 3); |
|
804 |
} |
|
805 |
putChar(dst, dp++, repl); |
|
806 |
sp -= 3; |
|
807 |
sp += malformedN(src, sp, 3); |
|
808 |
} else { |
|
809 |
char c = (char)((b1 << 12) ^ |
|
810 |
(b2 << 6) ^ |
|
811 |
(b3 ^ |
|
812 |
(((byte) 0xE0 << 12) ^ |
|
813 |
((byte) 0x80 << 6) ^ |
|
814 |
((byte) 0x80 << 0)))); |
|
815 |
if (isSurrogate(c)) { |
|
816 |
if (!doReplace) { |
|
817 |
throwMalformed(sp - 3, 3); |
|
818 |
} |
|
819 |
putChar(dst, dp++, repl); |
|
820 |
} else { |
|
821 |
putChar(dst, dp++, c); |
|
822 |
} |
|
823 |
} |
|
824 |
continue; |
|
825 |
} |
|
826 |
if (sp < sl && isMalformed3_2(b1, src[sp])) { |
|
827 |
if (!doReplace) { |
|
828 |
throwMalformed(sp - 1, 2); |
|
829 |
} |
|
830 |
putChar(dst, dp++, repl); |
|
831 |
continue; |
|
832 |
} |
|
833 |
if (!doReplace){ |
|
834 |
throwMalformed(sp, 1); |
|
835 |
} |
|
836 |
putChar(dst, dp++, repl); |
|
837 |
break; |
|
838 |
} else if ((b1 >> 3) == -2) { |
|
839 |
if (sp + 2 < sl) { |
|
840 |
int b2 = src[sp++]; |
|
841 |
int b3 = src[sp++]; |
|
842 |
int b4 = src[sp++]; |
|
843 |
int uc = ((b1 << 18) ^ |
|
844 |
(b2 << 12) ^ |
|
845 |
(b3 << 6) ^ |
|
846 |
(b4 ^ |
|
847 |
(((byte) 0xF0 << 18) ^ |
|
848 |
((byte) 0x80 << 12) ^ |
|
849 |
((byte) 0x80 << 6) ^ |
|
850 |
((byte) 0x80 << 0)))); |
|
851 |
if (isMalformed4(b2, b3, b4) || |
|
852 |
!isSupplementaryCodePoint(uc)) { // shortest form check |
|
853 |
if (!doReplace) { |
|
854 |
throwMalformed(sp - 4, 4); |
|
855 |
} |
|
856 |
putChar(dst, dp++, repl); |
|
857 |
sp -= 4; |
|
858 |
sp += malformedN(src, sp, 4); |
|
859 |
} else { |
|
860 |
putChar(dst, dp++, highSurrogate(uc)); |
|
861 |
putChar(dst, dp++, lowSurrogate(uc)); |
|
862 |
} |
|
863 |
continue; |
|
864 |
} |
|
865 |
b1 &= 0xff; |
|
866 |
if (b1 > 0xf4 || |
|
867 |
sp < sl && isMalformed4_2(b1, src[sp] & 0xff)) { |
|
868 |
if (!doReplace) { |
|
869 |
throwMalformed(sp - 1, 1); // or 2 |
|
870 |
} |
|
871 |
putChar(dst, dp++, repl); |
|
872 |
continue; |
|
873 |
} |
|
874 |
if (!doReplace) { |
|
875 |
throwMalformed(sp - 1, 1); |
|
876 |
} |
|
877 |
sp++; |
|
878 |
putChar(dst, dp++, repl); |
|
879 |
if (sp < sl && isMalformed4_3(src[sp])) { |
|
880 |
continue; |
|
881 |
} |
|
882 |
break; |
|
883 |
} else { |
|
884 |
if (!doReplace) { |
|
885 |
throwMalformed(sp - 1, 1); |
|
886 |
} |
|
887 |
putChar(dst, dp++, repl); |
|
888 |
} |
|
889 |
} |
|
890 |
if (dp != len) { |
|
891 |
dst = Arrays.copyOf(dst, dp << 1); |
|
892 |
} |
|
893 |
return ret.with(dst, UTF16); |
|
2 | 894 |
} |
895 |
||
48262 | 896 |
private static byte[] encodeUTF8(byte coder, byte[] val, boolean doReplace) { |
897 |
if (coder == UTF16) |
|
898 |
return encodeUTF8_UTF16(val, doReplace); |
|
899 |
||
900 |
if (!hasNegatives(val, 0, val.length)) |
|
901 |
return Arrays.copyOf(val, val.length); |
|
902 |
||
903 |
int dp = 0; |
|
904 |
byte[] dst = new byte[val.length << 1]; |
|
905 |
for (int sp = 0; sp < val.length; sp++) { |
|
906 |
byte c = val[sp]; |
|
907 |
if (c < 0) { |
|
908 |
dst[dp++] = (byte)(0xc0 | ((c & 0xff) >> 6)); |
|
909 |
dst[dp++] = (byte)(0x80 | (c & 0x3f)); |
|
910 |
} else { |
|
911 |
dst[dp++] = c; |
|
912 |
} |
|
2 | 913 |
} |
48262 | 914 |
if (dp == dst.length) |
915 |
return dst; |
|
916 |
return Arrays.copyOf(dst, dp); |
|
2 | 917 |
} |
34885 | 918 |
|
48262 | 919 |
private static byte[] encodeUTF8_UTF16(byte[] val, boolean doReplace) { |
920 |
int dp = 0; |
|
921 |
int sp = 0; |
|
922 |
int sl = val.length >> 1; |
|
923 |
byte[] dst = new byte[sl * 3]; |
|
924 |
char c; |
|
925 |
while (sp < sl && (c = StringUTF16.getChar(val, sp)) < '\u0080') { |
|
926 |
// ascii fast loop; |
|
927 |
dst[dp++] = (byte)c; |
|
928 |
sp++; |
|
929 |
} |
|
930 |
while (sp < sl) { |
|
931 |
c = StringUTF16.getChar(val, sp++); |
|
932 |
if (c < 0x80) { |
|
933 |
dst[dp++] = (byte)c; |
|
934 |
} else if (c < 0x800) { |
|
935 |
dst[dp++] = (byte)(0xc0 | (c >> 6)); |
|
936 |
dst[dp++] = (byte)(0x80 | (c & 0x3f)); |
|
937 |
} else if (Character.isSurrogate(c)) { |
|
938 |
int uc = -1; |
|
939 |
char c2; |
|
940 |
if (Character.isHighSurrogate(c) && sp < sl && |
|
941 |
Character.isLowSurrogate(c2 = StringUTF16.getChar(val, sp))) { |
|
942 |
uc = Character.toCodePoint(c, c2); |
|
943 |
} |
|
944 |
if (uc < 0) { |
|
945 |
if (doReplace) { |
|
946 |
dst[dp++] = '?'; |
|
947 |
} else { |
|
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
948 |
throwUnmappable(sp - 1, 1); // or 2, does not matter here |
48262 | 949 |
} |
950 |
} else { |
|
951 |
dst[dp++] = (byte)(0xf0 | ((uc >> 18))); |
|
952 |
dst[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f)); |
|
953 |
dst[dp++] = (byte)(0x80 | ((uc >> 6) & 0x3f)); |
|
954 |
dst[dp++] = (byte)(0x80 | (uc & 0x3f)); |
|
955 |
sp++; // 2 chars |
|
956 |
} |
|
957 |
} else { |
|
958 |
// 3 bytes, 16 bits |
|
959 |
dst[dp++] = (byte)(0xe0 | ((c >> 12))); |
|
960 |
dst[dp++] = (byte)(0x80 | ((c >> 6) & 0x3f)); |
|
961 |
dst[dp++] = (byte)(0x80 | (c & 0x3f)); |
|
962 |
} |
|
963 |
} |
|
964 |
if (dp == dst.length) { |
|
965 |
return dst; |
|
966 |
} |
|
967 |
return Arrays.copyOf(dst, dp); |
|
968 |
} |
|
969 |
||
970 |
////////////////////// for j.u.z.ZipCoder ////////////////////////// |
|
971 |
||
972 |
/* |
|
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
973 |
* Throws iae, instead of replacing, if malformed or unmappable. |
34885 | 974 |
*/ |
48262 | 975 |
static String newStringUTF8NoRepl(byte[] src, int off, int len) { |
976 |
if (COMPACT_STRINGS && !hasNegatives(src, off, len)) |
|
977 |
return new String(Arrays.copyOfRange(src, off, off + len), LATIN1); |
|
978 |
Result ret = decodeUTF8_0(src, off, len, false); |
|
979 |
return new String(ret.value, ret.coder); |
|
980 |
} |
|
981 |
||
982 |
/* |
|
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
983 |
* Throws iae, instead of replacing, if unmappable. |
48262 | 984 |
*/ |
985 |
static byte[] getBytesUTF8NoRepl(String s) { |
|
986 |
return encodeUTF8(s.coder(), s.value(), false); |
|
987 |
} |
|
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
988 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
989 |
////////////////////// for j.n.f.Files ////////////////////////// |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
990 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
991 |
private static boolean isASCII(byte[] src) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
992 |
return !hasNegatives(src, 0, src.length); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
993 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
994 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
995 |
private static String newStringLatin1(byte[] src) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
996 |
if (COMPACT_STRINGS) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
997 |
return new String(src, LATIN1); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
998 |
return new String(StringLatin1.inflate(src, 0, src.length), UTF16); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
999 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1000 |
|
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1001 |
static String newStringNoRepl(byte[] src, Charset cs) throws CharacterCodingException { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1002 |
try { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1003 |
return newStringNoRepl1(src, cs); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1004 |
} catch (IllegalArgumentException e) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1005 |
//newStringNoRepl1 throws IAE with MalformedInputException or CCE as the cause |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1006 |
Throwable cause = e.getCause(); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1007 |
if (cause instanceof MalformedInputException) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1008 |
throw (MalformedInputException)cause; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1009 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1010 |
throw (CharacterCodingException)cause; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1011 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1012 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1013 |
|
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1014 |
static String newStringNoRepl1(byte[] src, Charset cs) { |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1015 |
if (cs == UTF_8) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1016 |
if (COMPACT_STRINGS && isASCII(src)) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1017 |
return new String(src, LATIN1); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1018 |
Result ret = decodeUTF8_0(src, 0, src.length, false); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1019 |
return new String(ret.value, ret.coder); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1020 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1021 |
if (cs == ISO_8859_1) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1022 |
return newStringLatin1(src); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1023 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1024 |
if (cs == US_ASCII) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1025 |
if (isASCII(src)) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1026 |
return newStringLatin1(src); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1027 |
} else { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1028 |
throwMalformed(src); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1029 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1030 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1031 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1032 |
CharsetDecoder cd = cs.newDecoder(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1033 |
// ascii fastpath |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1034 |
if ((cd instanceof ArrayDecoder) && |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1035 |
((ArrayDecoder)cd).isASCIICompatible() && isASCII(src)) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1036 |
return newStringLatin1(src); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1037 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1038 |
int len = src.length; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1039 |
if (len == 0) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1040 |
return ""; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1041 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1042 |
int en = scale(len, cd.maxCharsPerByte()); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1043 |
char[] ca = new char[en]; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1044 |
if (cs.getClass().getClassLoader0() != null && |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1045 |
System.getSecurityManager() != null) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1046 |
src = Arrays.copyOf(src, len); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1047 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1048 |
ByteBuffer bb = ByteBuffer.wrap(src); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1049 |
CharBuffer cb = CharBuffer.wrap(ca); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1050 |
try { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1051 |
CoderResult cr = cd.decode(bb, cb, true); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1052 |
if (!cr.isUnderflow()) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1053 |
cr.throwException(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1054 |
cr = cd.flush(cb); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1055 |
if (!cr.isUnderflow()) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1056 |
cr.throwException(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1057 |
} catch (CharacterCodingException x) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1058 |
throw new IllegalArgumentException(x); // todo |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1059 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1060 |
Result ret = resultCached.get().with(ca, 0, cb.position()); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1061 |
return new String(ret.value, ret.coder); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1062 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1063 |
|
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1064 |
/* |
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1065 |
* Throws CCE, instead of replacing, if unmappable. |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1066 |
*/ |
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1067 |
static byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1068 |
try { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1069 |
return getBytesNoRepl1(s, cs); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1070 |
} catch (IllegalArgumentException e) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1071 |
//getBytesNoRepl1 throws IAE with UnmappableCharacterException or CCE as the cause |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1072 |
Throwable cause = e.getCause(); |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1073 |
if (cause instanceof UnmappableCharacterException) { |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1074 |
throw (UnmappableCharacterException)cause; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1075 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1076 |
throw (CharacterCodingException)cause; |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1077 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1078 |
} |
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1079 |
|
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1080 |
static byte[] getBytesNoRepl1(String s, Charset cs) { |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1081 |
byte[] val = s.value(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1082 |
byte coder = s.coder(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1083 |
if (cs == UTF_8) { |
51447
8dfed4387312
8209576: java.nio.file.Files.writeString writes garbled UTF-16 instead of UTF-8
joehw
parents:
50820
diff
changeset
|
1084 |
if (coder == LATIN1 && isASCII(val)) { |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1085 |
return val; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1086 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1087 |
return encodeUTF8(coder, val, false); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1088 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1089 |
if (cs == ISO_8859_1) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1090 |
if (coder == LATIN1) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1091 |
return val; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1092 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1093 |
return encode8859_1(coder, val, false); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1094 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1095 |
if (cs == US_ASCII) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1096 |
if (coder == LATIN1) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1097 |
if (isASCII(val)) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1098 |
return val; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1099 |
} else { |
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1100 |
throwUnmappable(val); |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1101 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1102 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1103 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1104 |
CharsetEncoder ce = cs.newEncoder(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1105 |
// fastpath for ascii compatible |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1106 |
if (coder == LATIN1 && (((ce instanceof ArrayEncoder) && |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1107 |
((ArrayEncoder)ce).isASCIICompatible() && |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1108 |
isASCII(val)))) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1109 |
return val; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1110 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1111 |
int len = val.length >> coder; // assume LATIN1=0/UTF16=1; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1112 |
int en = scale(len, ce.maxBytesPerChar()); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1113 |
byte[] ba = new byte[en]; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1114 |
if (len == 0) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1115 |
return ba; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1116 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1117 |
if (ce instanceof ArrayEncoder) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1118 |
int blen = (coder == LATIN1 ) ? ((ArrayEncoder)ce).encodeFromLatin1(val, 0, len, ba) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1119 |
: ((ArrayEncoder)ce).encodeFromUTF16(val, 0, len, ba); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1120 |
if (blen != -1) { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1121 |
return safeTrim(ba, blen, true); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1122 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1123 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1124 |
boolean isTrusted = cs.getClass().getClassLoader0() == null || |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1125 |
System.getSecurityManager() == null; |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1126 |
char[] ca = (coder == LATIN1 ) ? StringLatin1.toChars(val) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1127 |
: StringUTF16.toChars(val); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1128 |
ByteBuffer bb = ByteBuffer.wrap(ba); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1129 |
CharBuffer cb = CharBuffer.wrap(ca, 0, len); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1130 |
try { |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1131 |
CoderResult cr = ce.encode(cb, bb, true); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1132 |
if (!cr.isUnderflow()) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1133 |
cr.throwException(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1134 |
cr = ce.flush(bb); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1135 |
if (!cr.isUnderflow()) |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1136 |
cr.throwException(); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1137 |
} catch (CharacterCodingException x) { |
50820
35f52a3cd6bd
8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents:
50552
diff
changeset
|
1138 |
throw new IllegalArgumentException(x); |
50552
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1139 |
} |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1140 |
return safeTrim(ba, bb.position(), isTrusted); |
7439ceaae8e4
8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents:
49556
diff
changeset
|
1141 |
} |
2 | 1142 |
} |