src/java.base/share/classes/java/nio/charset/Charset.java

Print this page

        

@@ -591,10 +591,11 @@
                 }
             });
     }
 
     private static volatile Charset defaultCharset;
+    private static volatile Charset defaultUnicodeCharset;
 
     /**
      * Returns the default charset of this Java virtual machine.
      *
      * <p> The default charset is determined during virtual-machine startup and

@@ -618,10 +619,38 @@
             }
         }
         return defaultCharset;
     }
 
+    /**
+     * Returns the default unicode charset of this Java virtual machine.
+     *
+     * <p> The default unicode charset is determined during virtual-machine startup
+     * and depends on command line option "file.encoding.unicode".
+     * UTF8 is used by default.
+     *
+     * @return  A charset object for the default unicode charset
+     *
+     */
+    public static Charset defaultUnicodeCharset() {
+        if (defaultUnicodeCharset == null) {
+            synchronized (Charset.class) {
+                defaultUnicodeCharset = forName("UTF-8");
+
+                String csn = AccessController.doPrivileged(
+                    new GetPropertyAction("file.encoding.unicode"));
+                if (csn != null) {
+                    Charset cs = lookup(csn);
+                    if (cs != null) {
+                        defaultUnicodeCharset = cs;
+                    }
+                }
+            }
+        }
+        return defaultUnicodeCharset;
+    }
+
 
     /* -- Instance fields and methods -- */
 
     private final String name;          // tickles a bug in oldjavac
     private final String[] aliases;     // tickles a bug in oldjavac