233 CharBuffer cb = CharBuffer.wrap(ca);
234 try {
235 CoderResult cr = cd.decode(bb, cb, true);
236 if (!cr.isUnderflow())
237 cr.throwException();
238 cr = cd.flush(cb);
239 if (!cr.isUnderflow())
240 cr.throwException();
241 } catch (CharacterCodingException x) {
242 // Substitution is always enabled,
243 // so this shouldn't happen
244 throw new Error(x);
245 }
246 return safeTrim(ca, cb.position(), cs, isTrusted);
247 }
248 }
249
250 static char[] decode(byte[] ba, int off, int len) {
251 String csn = Charset.defaultCharset().name();
252 try {
253 return decode(csn, ba, off, len);
254 } catch (UnsupportedEncodingException x) {
255 warnUnsupportedCharset(csn);
256 }
257 try {
258 return decode("ISO-8859-1", ba, off, len);
259 } catch (UnsupportedEncodingException x) {
260 // If this code is hit during VM initialization, MessageUtils is
261 // the only way we will be able to get any kind of error message.
262 MessageUtils.err("ISO-8859-1 charset not available: "
263 + x.toString());
264 // If we can not find ISO-8859-1 (a required encoding) then things
265 // are seriously wrong with the installation.
266 System.exit(1);
267 return null;
268 }
269 }
270
271 // -- Encoding --
272 private static class StringEncoder {
306 ce.reset();
307 ByteBuffer bb = ByteBuffer.wrap(ba);
308 CharBuffer cb = CharBuffer.wrap(ca, off, len);
309 try {
310 CoderResult cr = ce.encode(cb, bb, true);
311 if (!cr.isUnderflow())
312 cr.throwException();
313 cr = ce.flush(bb);
314 if (!cr.isUnderflow())
315 cr.throwException();
316 } catch (CharacterCodingException x) {
317 // Substitution is always enabled,
318 // so this shouldn't happen
319 throw new Error(x);
320 }
321 return safeTrim(ba, bb.position(), cs, isTrusted);
322 }
323 }
324 }
325
326 static byte[] encode(String charsetName, char[] ca, int off, int len)
327 throws UnsupportedEncodingException
328 {
329 StringEncoder se = deref(encoder);
330 String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
331 if ((se == null) || !(csn.equals(se.requestedCharsetName())
332 || csn.equals(se.charsetName()))) {
333 se = null;
334 try {
335 Charset cs = lookupCharset(csn);
336 if (cs != null)
337 se = new StringEncoder(cs, csn);
338 } catch (IllegalCharsetNameException x) {}
339 if (se == null)
340 throw new UnsupportedEncodingException (csn);
341 set(encoder, se);
342 }
343 return se.encode(ca, off, len);
344 }
345
346 static byte[] encode(Charset cs, char[] ca, int off, int len) {
347 CharsetEncoder ce = cs.newEncoder();
348 int en = scale(len, ce.maxBytesPerChar());
349 byte[] ba = new byte[en];
350 if (len == 0)
351 return ba;
352 boolean isTrusted = false;
353 if (System.getSecurityManager() != null) {
354 if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
355 ca = Arrays.copyOfRange(ca, off, off + len);
356 off = 0;
357 }
358 }
359 ce.onMalformedInput(CodingErrorAction.REPLACE)
360 .onUnmappableCharacter(CodingErrorAction.REPLACE)
361 .reset();
362 if (ce instanceof ArrayEncoder) {
363 int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
364 return safeTrim(ba, blen, cs, isTrusted);
365 } else {
366 ByteBuffer bb = ByteBuffer.wrap(ba);
367 CharBuffer cb = CharBuffer.wrap(ca, off, len);
368 try {
369 CoderResult cr = ce.encode(cb, bb, true);
370 if (!cr.isUnderflow())
371 cr.throwException();
372 cr = ce.flush(bb);
373 if (!cr.isUnderflow())
374 cr.throwException();
375 } catch (CharacterCodingException x) {
376 throw new Error(x);
377 }
378 return safeTrim(ba, bb.position(), cs, isTrusted);
379 }
380 }
381
382 static byte[] encode(char[] ca, int off, int len) {
383 String csn = Charset.defaultCharset().name();
384 try {
385 return encode(csn, ca, off, len);
386 } catch (UnsupportedEncodingException x) {
387 warnUnsupportedCharset(csn);
388 }
389 try {
390 return encode("ISO-8859-1", ca, off, len);
391 } catch (UnsupportedEncodingException x) {
392 // If this code is hit during VM initialization, MessageUtils is
393 // the only way we will be able to get any kind of error message.
394 MessageUtils.err("ISO-8859-1 charset not available: "
395 + x.toString());
396 // If we can not find ISO-8859-1 (a required encoding) then things
397 // are seriously wrong with the installation.
398 System.exit(1);
399 return null;
400 }
401 }
402 }
|
233 CharBuffer cb = CharBuffer.wrap(ca);
234 try {
235 CoderResult cr = cd.decode(bb, cb, true);
236 if (!cr.isUnderflow())
237 cr.throwException();
238 cr = cd.flush(cb);
239 if (!cr.isUnderflow())
240 cr.throwException();
241 } catch (CharacterCodingException x) {
242 // Substitution is always enabled,
243 // so this shouldn't happen
244 throw new Error(x);
245 }
246 return safeTrim(ca, cb.position(), cs, isTrusted);
247 }
248 }
249
250 static char[] decode(byte[] ba, int off, int len) {
251 String csn = Charset.defaultCharset().name();
252 try {
253 // use "name" version for caching.
254 return decode(csn, ba, off, len);
255 } catch (UnsupportedEncodingException x) {
256 warnUnsupportedCharset(csn);
257 }
258 try {
259 return decode("ISO-8859-1", ba, off, len);
260 } catch (UnsupportedEncodingException x) {
261 // If this code is hit during VM initialization, MessageUtils is
262 // the only way we will be able to get any kind of error message.
263 MessageUtils.err("ISO-8859-1 charset not available: "
264 + x.toString());
265 // If we can not find ISO-8859-1 (a required encoding) then things
266 // are seriously wrong with the installation.
267 System.exit(1);
268 return null;
269 }
270 }
271
272 // -- Encoding --
273 private static class StringEncoder {
307 ce.reset();
308 ByteBuffer bb = ByteBuffer.wrap(ba);
309 CharBuffer cb = CharBuffer.wrap(ca, off, len);
310 try {
311 CoderResult cr = ce.encode(cb, bb, true);
312 if (!cr.isUnderflow())
313 cr.throwException();
314 cr = ce.flush(bb);
315 if (!cr.isUnderflow())
316 cr.throwException();
317 } catch (CharacterCodingException x) {
318 // Substitution is always enabled,
319 // so this shouldn't happen
320 throw new Error(x);
321 }
322 return safeTrim(ba, bb.position(), cs, isTrusted);
323 }
324 }
325 }
326
327 static byte[] encode(String charsetName, char[] ca)
328 throws UnsupportedEncodingException {
329 return encode(charsetName, ca, 0, ca.length);
330 }
331
332 static byte[] encode(String charsetName, char[] ca, int off, int len)
333 throws UnsupportedEncodingException
334 {
335 StringEncoder se = deref(encoder);
336 String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
337 if ((se == null) || !(csn.equals(se.requestedCharsetName())
338 || csn.equals(se.charsetName()))) {
339 se = null;
340 try {
341 Charset cs = lookupCharset(csn);
342 if (cs != null)
343 se = new StringEncoder(cs, csn);
344 } catch (IllegalCharsetNameException x) {}
345 if (se == null)
346 throw new UnsupportedEncodingException (csn);
347 set(encoder, se);
348 }
349 return se.encode(ca, off, len);
350 }
351
352 static byte[] encode(Charset cs, char[] ca) {
353 return encode(cs, ca, 0, ca.length);
354 }
355
356 static byte[] encode(Charset cs, char[] ca, int off, int len) {
357 CharsetEncoder ce = cs.newEncoder();
358 int en = scale(len, ce.maxBytesPerChar());
359 byte[] ba = new byte[en];
360 if (len == 0)
361 return ba;
362 boolean isTrusted = false;
363 if (System.getSecurityManager() != null) {
364 if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
365 ca = Arrays.copyOfRange(ca, off, off + len);
366 off = 0;
367 }
368 }
369 ce.onMalformedInput(CodingErrorAction.REPLACE)
370 .onUnmappableCharacter(CodingErrorAction.REPLACE)
371 .reset();
372 if (ce instanceof ArrayEncoder) {
373 int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba);
374 return safeTrim(ba, blen, cs, isTrusted);
375 } else {
376 ByteBuffer bb = ByteBuffer.wrap(ba);
377 CharBuffer cb = CharBuffer.wrap(ca, off, len);
378 try {
379 CoderResult cr = ce.encode(cb, bb, true);
380 if (!cr.isUnderflow())
381 cr.throwException();
382 cr = ce.flush(bb);
383 if (!cr.isUnderflow())
384 cr.throwException();
385 } catch (CharacterCodingException x) {
386 throw new Error(x);
387 }
388 return safeTrim(ba, bb.position(), cs, isTrusted);
389 }
390 }
391
392 static byte[] encode(char[] ca) {
393 return encode(ca, 0, ca.length);
394 }
395
396 static byte[] encode(char[] ca, int off, int len) {
397 String csn = Charset.defaultCharset().name();
398 try {
399 // use "name" version for caching.
400 return encode(csn, ca, off, len);
401 } catch (UnsupportedEncodingException x) {
402 warnUnsupportedCharset(csn);
403 }
404 try {
405 return encode("ISO-8859-1", ca, off, len);
406 } catch (UnsupportedEncodingException x) {
407 // If this code is hit during VM initialization, MessageUtils is
408 // the only way we will be able to get any kind of error message.
409 MessageUtils.err("ISO-8859-1 charset not available: "
410 + x.toString());
411 // If we can not find ISO-8859-1 (a required encoding) then things
412 // are seriously wrong with the installation.
413 System.exit(1);
414 return null;
415 }
416 }
417 }
|