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

Java default file encoding not taking precedence as default charset encoding when -Duser.* set

XMLWordPrintable

      ======
      NOTE:
      =====
      - Must be run in Japanese Locale as Eg

         LANG=ja
         LC_CTYPE="ja"
         LC_NUMERIC="ja"
         LC_TIME="ja"
         LC_COLLATE="ja"
         LC_MONETARY="ja"
         LC_MESSAGES="ja"
         LC_ALL=

      - Run the below test on JDK 1.4.2.x, 1.5.x
        (1.5.x is is the reference expected)
      - java -Dfile.encoding=MS932 Test2
        Expected: See that the default encoding is MS932
      - java -Dfile.encoding=MS932 -Duser.language=ja Test2
         Actual: You get EUC_JP_SOLARIS !!! not MS932

      ========
      Problem
      ========
      So the problem is that when program that is progranmmed to
      assume to have a certain file.encoding and that Java program
      is not code to open with InputStreamReader(fs,charset) will
      have problem.


      TEST Case
      ==========
      import java.util.*;
      import java.io.*;

      public class Test2 {
          public static void main(String args[]) {
              System.out.println("os: "+System.getProperty("os.name"));
              System.out.println("Locale: "+java.util.Locale.getDefault());
              System.out.println("Lang: "+
                     java.util.Locale.getDefault().getLanguage());
              System.out.println("FileEnc: "+
                   System.getProperty("file.encoding"));
              System.out.println("UserLang: "+
                   System.getProperty("user.language"));
              System.out.println("DefEncName: "+
                   sun.io.Converters.getDefaultEncodingName());
              try {
                    String[] encode = { "", "UTF-8", "EUC-JP", "Shift_JIS", "MS932" };
                 File fileDir = new File("./file");
                 File[] files = fileDir.listFiles();
                 for (int i = 0; i < files.length; i++) {
                     for (int j = 0; j < encode.length; j++) {
                      BufferedReader br = null;
                      InputStreamReader isr = null;
                      if (encode[j] == "") {
                              isr = new InputStreamReader(new
                                    FileInputStream(files[i]));
                      } else {
                              isr = new InputStreamReader(
                                  new FileInputStream(files[i]), encode[j]);
                      }
                      String enc = isr.getEncoding();
                      System.out.print("File=" + files[i]);
                      System.out.print(" enc[]=" + encode[j]);
                      System.out.println(" isr.getEncoding()=" + enc);
                    }
                  }
              } catch (Exception e) { e.printStackTrace(); };
          }
      }

      Output JDK 1.4.2_08
      os: SunOS
      Locale: en_US
      Lang: en
      FileEnc: MS932
      UserLang: en
      DefEncName: eucJP-open --> This is used by InputStreamReader....

      ==========
      NOTE
      ==========
      This works fine in JDK 1.5.0_02 so that means some backport

      ###@###.### 2005-05-27 09:13:37 GMT

            mr Mark Reinhold
            cchea Chee-Weng Chea (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: