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

Files.readString ignores encoding errors for UTF-16

    XMLWordPrintable

Details

    • b23
    • 19
    • b29
    • generic
    • generic
    • Verified

    Backports

      Description

        A DESCRIPTION OF THE PROBLEM :
        When using Files.readString(...) with UTF-16, encoding errors are ignored/ replaced, rather than throwing a MalformedInputException. This is the result of a fix of JDK-8286287, but is not matching the previous behavior of JDK 11 to JDK 16 and also inconsistent to the behavior for most other charsets, e.g. UTF-8.

        REGRESSION : Last worked in version 16

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Run the test code.

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        It should throw a MalformedInputException, as it does from JDK 11 to JDK 16. Note that when you change UTF_16 to UTF_8, the code does exhibit the expected behavior in all versions from JDK 11 to JDK 19.
        ACTUAL -
        It does not throw an exception with UTF_16 but silently proceeds with a replacement character (U+FFFD)

        ---------- BEGIN SOURCE ----------
        import java.io.*;
        import java.nio.file.*;
        import java.nio.charset.*;

        class Test {
          public static void main(String... arg) throws IOException {
            Path p = Files.write(Files.createTempFile("charset", "test"), new byte[] { -50 });
            try(Closeable c = () -> Files.delete(p)) {
                String s = Files.readString(p, StandardCharsets.UTF_16);
            }
          }
        }

        ---------- END SOURCE ----------

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                  Created:
                  Updated:
                  Resolved: