-
Bug
-
Resolution: Fixed
-
P3
-
11, 17, 18, 19
-
b26
-
generic
-
generic
-
Verified
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8308800 | 17.0.9-oracle | Weibing Xiao | P3 | Resolved | Fixed | b01 |
JDK-8305153 | 17.0.8 | Goetz Lindenmaier | P3 | Resolved | Fixed | b01 |
A DESCRIPTION OF THE PROBLEM :
Files.writeString is specified to throw IOException when a character cannot be encoded. Instead it uses replacement char "?" when used with sun.nio.cs.ArrayEncoder like Charset.forName("windows-1252"):
probably an error in sun.nio.cs.SingleByte.Encoder.encode(char)
=> Illegal content written on western Windows Systems
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
UnmappableCharacterException
ACTUAL -
JDK uses replacement character
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
public class EncodingBug {
public static void main(String[] args) throws IOException {
test("\u00f6\u00fc\u20ac"); // OK, chars of windows-1252
// fails:
test("\u041e");
test("\u0080");
test("\u0080\u041e");
}
private static void test(String s) throws IOException {
Path file = Files.createTempFile("prefix", "suffix");
Charset charset = Charset.forName("windows-1252");
// Charset charset = StandardCharsets.ISO_8859_1; -> UnmappableCharacterException
Files.writeString(file, s, charset); // should throw IOException (UnmappableCharacterException)!!
String s2 = Files.readString(file, charset);
if (!s.equals(s2))
throw new IllegalStateException(s2); // JDK used replacement character :-(
}
}
---------- END SOURCE ----------
FREQUENCY : always
Files.writeString is specified to throw IOException when a character cannot be encoded. Instead it uses replacement char "?" when used with sun.nio.cs.ArrayEncoder like Charset.forName("windows-1252"):
probably an error in sun.nio.cs.SingleByte.Encoder.encode(char)
=> Illegal content written on western Windows Systems
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
UnmappableCharacterException
ACTUAL -
JDK uses replacement character
---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
public class EncodingBug {
public static void main(String[] args) throws IOException {
test("\u00f6\u00fc\u20ac"); // OK, chars of windows-1252
// fails:
test("\u041e");
test("\u0080");
test("\u0080\u041e");
}
private static void test(String s) throws IOException {
Path file = Files.createTempFile("prefix", "suffix");
Charset charset = Charset.forName("windows-1252");
// Charset charset = StandardCharsets.ISO_8859_1; -> UnmappableCharacterException
Files.writeString(file, s, charset); // should throw IOException (UnmappableCharacterException)!!
String s2 = Files.readString(file, charset);
if (!s.equals(s2))
throw new IllegalStateException(s2); // JDK used replacement character :-(
}
}
---------- END SOURCE ----------
FREQUENCY : always
- backported by
-
JDK-8305153 Files.writeString fails to throw IOException for charset "windows-1252"
- Resolved
-
JDK-8308800 Files.writeString fails to throw IOException for charset "windows-1252"
- Resolved
- links to
-
Commit openjdk/jdk17u-dev/59d82117
-
Commit openjdk/jdk/6fb84e2c
-
Review openjdk/jdk17u-dev/1222
-
Review openjdk/jdk/9019
(1 links to)