mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8288589: Files.readString ignores encoding errors for UTF-16
Reviewed-by: rriggs, iris, alanb
This commit is contained in:
parent
ef17ee4dea
commit
2728770e3d
4 changed files with 163 additions and 88 deletions
|
@ -658,6 +658,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) {
|
||||
|
@ -665,7 +667,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) {
|
||||
|
@ -791,7 +799,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) {
|
||||
|
@ -1199,23 +1213,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();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue