-
Bug
-
Resolution: Fixed
-
P3
-
17, 18, 19
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8303670 | 17.0.8-oracle | Weibing Xiao | P3 | Resolved | Fixed | b01 |
JDK-8305152 | 17.0.8 | Goetz Lindenmaier | P3 | Resolved | Fixed | b01 |
A DESCRIPTION OF THE PROBLEM :
If a text file is UTF-8 and has an odd number of characters, trying to read it as UTF-16 throws an Error caused by MalformedInputException:
Files.readString(Paths.get("text.txt"), StandardCharsets.UTF_16);
A comment in the java.lang.String method which causes the Error says "// Substitution is always enabled, // so this shouldn't happen". Clearly the intent is that only the MalformedInputException should be thrown.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a UTF-8 file text.txt containing an odd number of characters. Create, compile and run Test.java with the provided source code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Exception caught, no output.
ACTUAL -
Exception in thread "main" java.lang.Error: java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.lang.String.decodeWithDecoder(String.java:1220)
at java.base/java.lang.String.newStringNoRepl1(String.java:796)
at java.base/java.lang.String.newStringNoRepl(String.java:748)
at java.base/java.lang.System$2.newStringNoRepl(System.java:2439)
at java.base/java.nio.file.Files.readString(Files.java:3370)
at Test.main(Test.java:8)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
at java.base/java.lang.String.decodeWithDecoder(String.java:1213)
... 5 more
---------- BEGIN SOURCE ----------
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Test {
public static void main(String[] args) {
try {
Files.readString(Paths.get("text.txt"), StandardCharsets.UTF_16);
} catch (Exception e) {}
}
}
---------- END SOURCE ----------
FREQUENCY : always
If a text file is UTF-8 and has an odd number of characters, trying to read it as UTF-16 throws an Error caused by MalformedInputException:
Files.readString(Paths.get("text.txt"), StandardCharsets.UTF_16);
A comment in the java.lang.String method which causes the Error says "// Substitution is always enabled, // so this shouldn't happen". Clearly the intent is that only the MalformedInputException should be thrown.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a UTF-8 file text.txt containing an odd number of characters. Create, compile and run Test.java with the provided source code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Exception caught, no output.
ACTUAL -
Exception in thread "main" java.lang.Error: java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.lang.String.decodeWithDecoder(String.java:1220)
at java.base/java.lang.String.newStringNoRepl1(String.java:796)
at java.base/java.lang.String.newStringNoRepl(String.java:748)
at java.base/java.lang.System$2.newStringNoRepl(System.java:2439)
at java.base/java.nio.file.Files.readString(Files.java:3370)
at Test.main(Test.java:8)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.base/java.nio.charset.CoderResult.throwException(CoderResult.java:274)
at java.base/java.lang.String.decodeWithDecoder(String.java:1213)
... 5 more
---------- BEGIN SOURCE ----------
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Test {
public static void main(String[] args) {
try {
Files.readString(Paths.get("text.txt"), StandardCharsets.UTF_16);
} catch (Exception e) {}
}
}
---------- END SOURCE ----------
FREQUENCY : always
- backported by
-
JDK-8303670 Reading file as UTF-16 causes Error which "shouldn't happen"
- Resolved
-
JDK-8305152 Reading file as UTF-16 causes Error which "shouldn't happen"
- Resolved
- relates to
-
JDK-8288589 Files.readString ignores encoding errors for UTF-16
- Closed
- links to
-
Commit openjdk/jdk17u-dev/6ba50fcf
-
Commit openjdk/jdk/cc7560e9
-
Review openjdk/jdk17u-dev/1221
-
Review openjdk/jdk/8640
(2 links to)