8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause

Reviewed-by: sherman, alanb, lancea
This commit is contained in:
Joe Wang 2018-06-27 09:31:51 -07:00
parent 884f9db3c6
commit e5ac84c7b0
5 changed files with 80 additions and 49 deletions

View file

@ -36,6 +36,8 @@ import java.nio.charset.CharacterCodingException;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.MalformedInputException;
import java.nio.charset.UnmappableCharacterException;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Arrays;
import jdk.internal.HotSpotIntrinsicCandidate;
@ -607,7 +609,7 @@ class StringCoding {
dp = dp + ret;
if (ret != len) {
if (!doReplace) {
throwMalformed(sp, 1);
throwUnmappable(sp, 1);
}
char c = StringUTF16.getChar(val, sp++);
if (Character.isHighSurrogate(c) && sp < sl &&
@ -679,8 +681,8 @@ class StringCoding {
}
private static void throwMalformed(int off, int nb) {
throw new IllegalArgumentException("malformed input off : " + off +
", length : " + nb);
String msg = "malformed input off : " + off + ", length : " + nb;
throw new IllegalArgumentException(msg, new MalformedInputException(nb));
}
private static void throwMalformed(byte[] val) {
@ -689,6 +691,17 @@ class StringCoding {
throwMalformed(dp, 1);
}
private static void throwUnmappable(int off, int nb) {
String msg = "malformed input off : " + off + ", length : " + nb;
throw new IllegalArgumentException(msg, new UnmappableCharacterException(nb));
}
private static void throwUnmappable(byte[] val) {
int dp = 0;
while (dp < val.length && val[dp] >=0) { dp++; }
throwUnmappable(dp, 1);
}
private static char repl = '\ufffd';
private static Result decodeUTF8(byte[] src, int sp, int len, boolean doReplace) {
@ -919,7 +932,7 @@ class StringCoding {
if (doReplace) {
dst[dp++] = '?';
} else {
throwMalformed(sp - 1, 1); // or 2, does not matter here
throwUnmappable(sp - 1, 1); // or 2, does not matter here
}
} else {
dst[dp++] = (byte)(0xf0 | ((uc >> 18)));
@ -972,7 +985,20 @@ class StringCoding {
return new String(StringLatin1.inflate(src, 0, src.length), UTF16);
}
static String newStringNoRepl(byte[] src, Charset cs) {
static String newStringNoRepl(byte[] src, Charset cs) throws CharacterCodingException {
try {
return newStringNoRepl1(src, cs);
} catch (IllegalArgumentException e) {
//newStringNoRepl1 throws IAE with MalformedInputException or CCE as the cause
Throwable cause = e.getCause();
if (cause instanceof MalformedInputException) {
throw (MalformedInputException)cause;
}
throw (CharacterCodingException)cause;
}
}
static String newStringNoRepl1(byte[] src, Charset cs) {
if (cs == UTF_8) {
if (COMPACT_STRINGS && isASCII(src))
return new String(src, LATIN1);
@ -1023,9 +1049,22 @@ class StringCoding {
}
/*
* Throws iae, instead of replacing, if unmappable.
* Throws CCE, instead of replacing, if unmappable.
*/
static byte[] getBytesNoRepl(String s, Charset cs) {
static byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
try {
return getBytesNoRepl1(s, cs);
} catch (IllegalArgumentException e) {
//getBytesNoRepl1 throws IAE with UnmappableCharacterException or CCE as the cause
Throwable cause = e.getCause();
if (cause instanceof UnmappableCharacterException) {
throw (UnmappableCharacterException)cause;
}
throw (CharacterCodingException)cause;
}
}
static byte[] getBytesNoRepl1(String s, Charset cs) {
byte[] val = s.value();
byte coder = s.coder();
if (cs == UTF_8) {
@ -1045,7 +1084,7 @@ class StringCoding {
if (isASCII(val)) {
return val;
} else {
throwMalformed(val);
throwUnmappable(val);
}
}
}
@ -1083,7 +1122,7 @@ class StringCoding {
if (!cr.isUnderflow())
cr.throwException();
} catch (CharacterCodingException x) {
throw new Error(x);
throw new IllegalArgumentException(x);
}
return safeTrim(ba, bb.position(), isTrusted);
}

View file

@ -41,6 +41,7 @@ import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.nio.charset.CharacterCodingException;
import java.security.AccessControlContext;
import java.security.ProtectionDomain;
import java.security.AccessController;
@ -2184,11 +2185,11 @@ public final class System {
return ModuleLayer.layers(loader);
}
public String newStringNoRepl(byte[] bytes, Charset cs) {
public String newStringNoRepl(byte[] bytes, Charset cs) throws CharacterCodingException {
return StringCoding.newStringNoRepl(bytes, cs);
}
public byte[] getBytesNoRepl(String s, Charset cs) {
public byte[] getBytesNoRepl(String s, Charset cs) throws CharacterCodingException {
return StringCoding.getBytesNoRepl(s, cs);
}

View file

@ -3281,11 +3281,7 @@ public final class Files {
Objects.requireNonNull(cs);
byte[] ba = readAllBytes(path);
try {
return JLA.newStringNoRepl(ba, cs);
} catch (IllegalArgumentException e) {
throw new IOException(e);
}
return JLA.newStringNoRepl(ba, cs);
}
/**
@ -3636,12 +3632,8 @@ public final class Files {
Objects.requireNonNull(csq);
Objects.requireNonNull(cs);
try {
byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
write(path, bytes, options);
} catch (IllegalArgumentException e) {
throw new IOException(e);
}
byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
write(path, bytes, options);
return path;
}