8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
authormartin
Wed, 28 Mar 2018 21:14:06 -0700
changeset 49443 e5679a6661d6
parent 49442 d6d1c06becda
child 49444 ae873285a39c
8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets Reviewed-by: sherman, ulfzibis
src/java.base/share/classes/java/lang/StringCoding.java
src/java.base/share/classes/java/nio/charset/Charset.java
src/java.base/share/classes/java/nio/charset/StandardCharsets.java
src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java
src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template
src/java.base/share/classes/sun/nio/cs/US_ASCII.java
src/java.base/share/classes/sun/nio/cs/UTF_16.java
src/java.base/share/classes/sun/nio/cs/UTF_16BE.java
src/java.base/share/classes/sun/nio/cs/UTF_16LE.java
src/java.base/share/classes/sun/nio/cs/UTF_8.java
test/jdk/java/nio/charset/StandardCharsets/Standard.java
--- a/src/java.base/share/classes/java/lang/StringCoding.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/java/lang/StringCoding.java	Wed Mar 28 21:14:06 2018 -0700
@@ -52,6 +52,9 @@
 import static java.lang.Character.lowSurrogate;
 import static java.lang.Character.isSupplementaryCodePoint;
 import static java.lang.StringUTF16.putChar;
+import static java.nio.charset.StandardCharsets.ISO_8859_1;
+import static java.nio.charset.StandardCharsets.US_ASCII;
+import static java.nio.charset.StandardCharsets.UTF_8;
 
 /**
  * Utility class for string encoding and decoding.
@@ -67,10 +70,6 @@
     private static final ThreadLocal<SoftReference<StringEncoder>> encoder =
         new ThreadLocal<>();
 
-    private static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
-    private static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
-    private static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
-
     private static <T> T deref(ThreadLocal<SoftReference<T>> tl) {
         SoftReference<T> sr = tl.get();
         if (sr == null)
--- a/src/java.base/share/classes/java/nio/charset/Charset.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/java/nio/charset/Charset.java	Wed Mar 28 21:14:06 2018 -0700
@@ -26,7 +26,6 @@
 package java.nio.charset;
 
 import jdk.internal.misc.VM;
-import sun.nio.cs.StandardCharsets;
 import sun.nio.cs.ThreadLocalCoders;
 import sun.security.action.GetPropertyAction;
 
@@ -311,7 +310,8 @@
     }
 
     /* The standard set of charsets */
-    private static final CharsetProvider standardProvider = new StandardCharsets();
+    private static final CharsetProvider standardProvider
+        = new sun.nio.cs.StandardCharsets();
 
     private static final String[] zeroAliases = new String[0];
 
@@ -609,7 +609,7 @@
                 if (cs != null)
                     defaultCharset = cs;
                 else
-                    defaultCharset = sun.nio.cs.UTF_8.INSTANCE;
+                    defaultCharset = StandardCharsets.UTF_8;
             }
         }
         return defaultCharset;
--- a/src/java.base/share/classes/java/nio/charset/StandardCharsets.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/java/nio/charset/StandardCharsets.java	Wed Mar 28 21:14:06 2018 -0700
@@ -41,26 +41,26 @@
      * Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the
      * Unicode character set
      */
-    public static final Charset US_ASCII = sun.nio.cs.US_ASCII.INSTANCE;
+    public static final Charset US_ASCII = new sun.nio.cs.US_ASCII();
     /**
      * ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
      */
-    public static final Charset ISO_8859_1 = sun.nio.cs.ISO_8859_1.INSTANCE;
+    public static final Charset ISO_8859_1 = new sun.nio.cs.ISO_8859_1();
     /**
      * Eight-bit UCS Transformation Format
      */
-    public static final Charset UTF_8 = sun.nio.cs.UTF_8.INSTANCE;
+    public static final Charset UTF_8 = new sun.nio.cs.UTF_8();
     /**
      * Sixteen-bit UCS Transformation Format, big-endian byte order
      */
-    public static final Charset UTF_16BE = Charset.forName("UTF-16BE");
+    public static final Charset UTF_16BE = new sun.nio.cs.UTF_16BE();
     /**
      * Sixteen-bit UCS Transformation Format, little-endian byte order
      */
-    public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
+    public static final Charset UTF_16LE = new sun.nio.cs.UTF_16LE();
     /**
      * Sixteen-bit UCS Transformation Format, byte order identified by an
      * optional byte-order mark
      */
-    public static final Charset UTF_16 = Charset.forName("UTF-16");
+    public static final Charset UTF_16 = new sun.nio.cs.UTF_16();
 }
--- a/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Wed Mar 28 21:14:06 2018 -0700
@@ -39,9 +39,6 @@
     extends Charset
     implements HistoricallyNamedCharset
 {
-
-    public static final ISO_8859_1 INSTANCE = new ISO_8859_1();
-
     public ISO_8859_1() {
         super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1());
     }
--- a/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Wed Mar 28 21:14:06 2018 -0700
@@ -83,9 +83,12 @@
         Map<String,Charset> map = cache;
         if (map == null) {
             map = new Cache();
-            map.put("utf-8", UTF_8.INSTANCE);
-            map.put("iso-8859-1", ISO_8859_1.INSTANCE);
-            map.put("us-ascii", US_ASCII.INSTANCE);
+            map.put("utf-8", java.nio.charset.StandardCharsets.UTF_8);
+            map.put("iso-8859-1", java.nio.charset.StandardCharsets.ISO_8859_1);
+            map.put("us-ascii", java.nio.charset.StandardCharsets.US_ASCII);
+            map.put("utf-16", java.nio.charset.StandardCharsets.UTF_16);
+            map.put("utf-16be", java.nio.charset.StandardCharsets.UTF_16BE);
+            map.put("utf-16le", java.nio.charset.StandardCharsets.UTF_16LE);
             cache = map;
         }
         return map;
@@ -123,11 +126,11 @@
         // Classes eagerly during bootstrap
         String csn;
         if (charsetName.equals("UTF-8")) {
-            return UTF_8.INSTANCE;
+            return java.nio.charset.StandardCharsets.UTF_8;
         } else if (charsetName.equals("US-ASCII")) {
-            return US_ASCII.INSTANCE;
+            return java.nio.charset.StandardCharsets.US_ASCII;
         } else if (charsetName.equals("ISO-8859-1")) {
-            return ISO_8859_1.INSTANCE;
+            return java.nio.charset.StandardCharsets.ISO_8859_1;
         } else {
             csn = canonicalize(toLower(charsetName));
         }
--- a/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Wed Mar 28 21:14:06 2018 -0700
@@ -36,8 +36,6 @@
     extends Charset
     implements HistoricallyNamedCharset
 {
-    public static final US_ASCII INSTANCE = new US_ASCII();
-
     public US_ASCII() {
         super("US-ASCII", StandardCharsets.aliases_US_ASCII());
     }
--- a/src/java.base/share/classes/sun/nio/cs/UTF_16.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/UTF_16.java	Wed Mar 28 21:14:06 2018 -0700
@@ -29,7 +29,7 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 
-class UTF_16 extends Unicode
+public class UTF_16 extends Unicode
 {
 
     public UTF_16() {
--- a/src/java.base/share/classes/sun/nio/cs/UTF_16BE.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/UTF_16BE.java	Wed Mar 28 21:14:06 2018 -0700
@@ -29,7 +29,7 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 
-class UTF_16BE extends Unicode
+public class UTF_16BE extends Unicode
 {
 
     public UTF_16BE() {
--- a/src/java.base/share/classes/sun/nio/cs/UTF_16LE.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/UTF_16LE.java	Wed Mar 28 21:14:06 2018 -0700
@@ -29,7 +29,7 @@
 import java.nio.charset.CharsetDecoder;
 import java.nio.charset.CharsetEncoder;
 
-class UTF_16LE extends Unicode
+public class UTF_16LE extends Unicode
 {
 
     public UTF_16LE() {
--- a/src/java.base/share/classes/sun/nio/cs/UTF_8.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/src/java.base/share/classes/sun/nio/cs/UTF_8.java	Wed Mar 28 21:14:06 2018 -0700
@@ -55,9 +55,6 @@
  */
 
 public final class UTF_8 extends Unicode {
-
-    public static final UTF_8 INSTANCE = new UTF_8();
-
     public UTF_8() {
         super("UTF-8", StandardCharsets.aliases_UTF_8());
     }
--- a/test/jdk/java/nio/charset/StandardCharsets/Standard.java	Wed Mar 28 21:14:03 2018 -0700
+++ b/test/jdk/java/nio/charset/StandardCharsets/Standard.java	Wed Mar 28 21:14:06 2018 -0700
@@ -58,6 +58,13 @@
         check("UTF-16LE".equals(StandardCharsets.UTF_16LE.name()));
         check("UTF-16".equals(StandardCharsets.UTF_16.name()));
 
+        check(Charset.forName("US-ASCII") == StandardCharsets.US_ASCII);
+        check(Charset.forName("ISO-8859-1") == StandardCharsets.ISO_8859_1);
+        check(Charset.forName("UTF-8") == StandardCharsets.UTF_8);
+        check(Charset.forName("UTF-16BE") == StandardCharsets.UTF_16BE);
+        check(Charset.forName("UTF-16LE") == StandardCharsets.UTF_16LE);
+        check(Charset.forName("UTF-16") == StandardCharsets.UTF_16);
+
         Set<String> charsets = new HashSet<>();
         Field standardCharsetFields[] = StandardCharsets.class.getFields();