|
50 | 50 | import java.nio.charset.CharacterCodingException;
|
51 | 51 | import java.nio.charset.Charset;
|
52 | 52 | import java.nio.charset.CodingErrorAction;
|
| 53 | +import java.nio.charset.IllegalCharsetNameException; |
53 | 54 | import java.nio.charset.StandardCharsets;
|
| 55 | +import java.nio.charset.UnsupportedCharsetException; |
54 | 56 | import java.util.Arrays;
|
55 | 57 | import java.util.HashMap;
|
56 | 58 | import java.util.List;
|
@@ -411,15 +413,18 @@ Object encode(Object str, @SuppressWarnings("unused") Object encoding, @Suppress
|
411 | 413 | @TruffleBoundary
|
412 | 414 | private PBytes encodeString(String self, String encoding, String errors) {
|
413 | 415 | CodingErrorAction errorAction = convertCodingErrorAction(errors);
|
| 416 | + Charset charset; |
| 417 | + try { |
| 418 | + charset = getCharset(encoding); |
| 419 | + } catch (UnsupportedCharsetException | IllegalCharsetNameException e) { |
| 420 | + throw raise(LookupError, "unknown encoding: %s", encoding); |
| 421 | + } |
414 | 422 | try {
|
415 |
| - Charset charset = getCharset(encoding); |
416 | 423 | ByteBuffer encoded = charset.newEncoder().onMalformedInput(errorAction).onUnmappableCharacter(errorAction).encode(CharBuffer.wrap(self));
|
417 | 424 | int n = encoded.remaining();
|
418 | 425 | byte[] data = new byte[n];
|
419 | 426 | encoded.get(data);
|
420 | 427 | return factory().createBytes(data);
|
421 |
| - } catch (IllegalArgumentException e) { |
422 |
| - throw raise(LookupError, "unknown encoding: %s", encoding); |
423 | 428 | } catch (CharacterCodingException e) {
|
424 | 429 | throw raise(UnicodeEncodeError, e);
|
425 | 430 | }
|
@@ -541,12 +546,15 @@ private ByteBuffer getBytesBuffer(PIBytesLike bytesLike) {
|
541 | 546 | @TruffleBoundary
|
542 | 547 | String decodeBytes(ByteBuffer bytes, String encoding, String errors) {
|
543 | 548 | CodingErrorAction errorAction = convertCodingErrorAction(errors);
|
| 549 | + Charset charset; |
| 550 | + try { |
| 551 | + charset = getCharset(encoding); |
| 552 | + } catch (UnsupportedCharsetException | IllegalCharsetNameException e) { |
| 553 | + throw raise(LookupError, "unknown encoding: %s", encoding); |
| 554 | + } |
544 | 555 | try {
|
545 |
| - Charset charset = getCharset(encoding); |
546 | 556 | CharBuffer decoded = charset.newDecoder().onMalformedInput(errorAction).onUnmappableCharacter(errorAction).decode(bytes);
|
547 | 557 | return String.valueOf(decoded);
|
548 |
| - } catch (IllegalArgumentException e) { |
549 |
| - throw raise(LookupError, "unknown encoding: %s", encoding); |
550 | 558 | } catch (CharacterCodingException e) {
|
551 | 559 | throw raise(UnicodeDecodeError, e);
|
552 | 560 | }
|
@@ -623,7 +631,7 @@ Object lookup(String encoding) {
|
623 | 631 | try {
|
624 | 632 | getCharset(encoding);
|
625 | 633 | return true;
|
626 |
| - } catch (IllegalArgumentException e) { |
| 634 | + } catch (UnsupportedCharsetException | IllegalCharsetNameException e) { |
627 | 635 | return PNone.NONE;
|
628 | 636 | }
|
629 | 637 | }
|
|
0 commit comments