8286287: Reading file as UTF-16 causes Error which "shouldn't happen"

Reviewed-by: jpai, bpb, rriggs
This commit is contained in:
Naoto Sato 2022-05-12 15:58:19 +00:00
parent 82aa045584
commit cc7560e995
2 changed files with 52 additions and 2 deletions

View file

@ -658,8 +658,6 @@ 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) {
@ -1207,6 +1205,8 @@ public final class String
private static int decodeWithDecoder(CharsetDecoder cd, char[] dst, byte[] src, int offset, int length) {
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())