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 {
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 charset name decode() variant which provides 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 {
366 } else {
367 ByteBuffer bb = ByteBuffer.wrap(ba);
368 CharBuffer cb = CharBuffer.wrap(ca, off, len);
369 try {
370 CoderResult cr = ce.encode(cb, bb, true);
371 if (!cr.isUnderflow())
372 cr.throwException();
373 cr = ce.flush(bb);
374 if (!cr.isUnderflow())
375 cr.throwException();
376 } catch (CharacterCodingException x) {
377 throw new Error(x);
378 }
379 return safeTrim(ba, bb.position(), cs, isTrusted);
380 }
381 }
382
383 static byte[] encode(char[] ca, int off, int len) {
384 String csn = Charset.defaultCharset().name();
385 try {
386 // use charset name encode() variant which provides caching.
387 return encode(csn, ca, off, len);
388 } catch (UnsupportedEncodingException x) {
389 warnUnsupportedCharset(csn);
390 }
391 try {
392 return encode("ISO-8859-1", ca, off, len);
393 } catch (UnsupportedEncodingException x) {
394 // If this code is hit during VM initialization, MessageUtils is
395 // the only way we will be able to get any kind of error message.
396 MessageUtils.err("ISO-8859-1 charset not available: "
397 + x.toString());
398 // If we can not find ISO-8859-1 (a required encoding) then things
399 // are seriously wrong with the installation.
400 System.exit(1);
401 return null;
402 }
403 }
404 }
|