JDK-8328714 : Shift_JIS encoded content gets garbled with Java 22
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 22
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2024-03-20
  • Updated: 2024-04-08
  • Resolved: 2024-04-08
Related Reports
Duplicate :  
Description
ADDITIONAL SYSTEM INFORMATION :
macOS 14.3.1
Java version: OpenJDK 22.0 macOS / AArch64

A DESCRIPTION OF THE PROBLEM :
Strings encoded in Shift_JIS become garbled when read from files.
It worked fine until Java 21 as follows:
Java Version:21.0.2
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:SJIS
日本語です
Decoded string matches:true
日本語です
Content from file matches:true

REGRESSION : Last worked in version 21.0.2

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the attached source code.



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Java Version:22
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:Shift_JIS
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
ACTUAL -
Java Version:22
---------Testing:UTF-8
日本語です
Decoded string matches:true
日本語です
Content from file matches:true
---------Testing:Shift_JIS
日本語です
Decoded string matches:true
åe,gžŠg0Y0
Content from file matches:false

---------- BEGIN SOURCE ----------
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws IOException {
        System.out.println("Java Version:" + System.getProperty("java.version"));

        String[] encodings = {"UTF-8", "Shift_JIS"};
        for (String encoding : encodings) {
            test(encoding);
        }
    }

    static void test(String encoding) throws IOException {
        System.out.println("---------Testing:" + encoding);
        Charset charset = Charset.forName(encoding);
        Path path = Path.of(encoding + "_encoded.txt");
        String originalContent = "日本語です";
        byte[] encodedBytes = originalContent.getBytes(charset);
        String decodedString = new String(encodedBytes, charset);
        System.out.println(decodedString);
        System.out.println("Decoded string matches:" + decodedString.equals(originalContent));

        Files.writeString(path, originalContent, charset);
        String read = Files.readString(path, charset);
        System.out.println(read);
        boolean matches = read.equals(originalContent);
        System.out.println("Content from file matches:" + matches);

    }
}
---------- END SOURCE ----------

FREQUENCY : always



Comments
The test case is reproducible on Windows 10 for JDK 22. Verified that it works for JDK 22.0.1. Mailed the submitter with the known issue mentioned in the JDK 22 release notes https://www.oracle.com/java/technologies/javase/22-relnote-issues.html#JDK-8325605 Closing this incident as 'Not an issue'
21-03-2024

Likely JDK-8325590, listed as known issue in JDK 22 release notes, fixed for JDK 23 and 22.0.1
21-03-2024