Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8287541

Files.writeString fails to throw IOException for charset "windows-1252"

XMLWordPrintable

    • b26
    • generic
    • generic
    • Verified

        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


              naoto Naoto Sato
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Created:
                Updated:
                Resolved: