diff options
| author | Goetz Lindenmaier <goetz@openjdk.org> | 2023-04-16 14:16:26 +0000 |
|---|---|---|
| committer | Goetz Lindenmaier <goetz@openjdk.org> | 2023-04-16 14:16:26 +0000 |
| commit | acbac0d169138f028841bc264d308a429d0a5370 (patch) | |
| tree | 7b80ba5675198c4428213d205abea4a79c7a907b /src/java.base/share/classes/java/lang/String.java | |
| parent | 01cf112cbb5603c285d0d4bb54a840e4585f8735 (diff) | |
| download | openjdk-acbac0d169138f028841bc264d308a429d0a5370.tar.gz openjdk-acbac0d169138f028841bc264d308a429d0a5370.zip | |
8288589: Files.readString ignores encoding errors for UTF-16
Backport-of: 2728770e3d73bb11c4d6e54b5aff91588a1a780b
Diffstat (limited to 'src/java.base/share/classes/java/lang/String.java')
| -rw-r--r-- | src/java.base/share/classes/java/lang/String.java | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java index 884a117727e..de62d82a331 100644 --- a/src/java.base/share/classes/java/lang/String.java +++ b/src/java.base/share/classes/java/lang/String.java @@ -651,6 +651,8 @@ public final class String // decode using CharsetDecoder int en = scale(length, cd.maxCharsPerByte()); + cd.onMalformedInput(CodingErrorAction.REPLACE) + .onUnmappableCharacter(CodingErrorAction.REPLACE); char[] ca = new char[en]; if (charset.getClass().getClassLoader0() != null && System.getSecurityManager() != null) { @@ -658,7 +660,13 @@ public final class String offset = 0; } - int caLen = decodeWithDecoder(cd, ca, bytes, offset, length); + int caLen; + try { + caLen = decodeWithDecoder(cd, ca, bytes, offset, length); + } catch (CharacterCodingException x) { + // Substitution is enabled, so this shouldn't happen + throw new Error(x); + } if (COMPACT_STRINGS) { byte[] bs = StringUTF16.compress(ca, 0, caLen); if (bs != null) { @@ -779,7 +787,13 @@ public final class String System.getSecurityManager() != null) { src = Arrays.copyOf(src, len); } - int caLen = decodeWithDecoder(cd, ca, src, 0, src.length); + int caLen; + try { + caLen = decodeWithDecoder(cd, ca, src, 0, src.length); + } catch (CharacterCodingException x) { + // throw via IAE + throw new IllegalArgumentException(x); + } if (COMPACT_STRINGS) { byte[] bs = StringUTF16.compress(ca, 0, caLen); if (bs != null) { @@ -1189,23 +1203,16 @@ public final class String return dp; } - private static int decodeWithDecoder(CharsetDecoder cd, char[] dst, byte[] src, int offset, int length) { + private static int decodeWithDecoder(CharsetDecoder cd, char[] dst, byte[] src, int offset, int length) + throws CharacterCodingException { ByteBuffer bb = ByteBuffer.wrap(src, offset, length); CharBuffer cb = CharBuffer.wrap(dst, 0, dst.length); - cd.onMalformedInput(CodingErrorAction.REPLACE) - .onUnmappableCharacter(CodingErrorAction.REPLACE); - try { - CoderResult cr = cd.decode(bb, cb, true); - if (!cr.isUnderflow()) - cr.throwException(); - cr = cd.flush(cb); - if (!cr.isUnderflow()) - cr.throwException(); - } catch (CharacterCodingException x) { - // Substitution is always enabled, - // so this shouldn't happen - throw new Error(x); - } + CoderResult cr = cd.decode(bb, cb, true); + if (!cr.isUnderflow()) + cr.throwException(); + cr = cd.flush(cb); + if (!cr.isUnderflow()) + cr.throwException(); return cb.position(); } |
