8184665: Skip name and alias checks for standard Charsets
authorredestad
Wed, 19 Jul 2017 14:40:50 +0200
changeset 45894 995421c69f66
parent 45893 2678b360eecd
child 45895 d875751d58e8
8184665: Skip name and alias checks for standard Charsets Reviewed-by: sherman, rriggs, forax
jdk/src/java.base/share/classes/java/nio/charset/Charset.java
jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java
jdk/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template
jdk/src/java.base/share/classes/sun/nio/cs/US_ASCII.java
jdk/src/java.base/share/classes/sun/nio/cs/UTF_8.java
jdk/test/java/nio/charset/Charset/IllegalCharsetName.java
--- a/jdk/src/java.base/share/classes/java/nio/charset/Charset.java	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/src/java.base/share/classes/java/nio/charset/Charset.java	Wed Jul 19 14:40:50 2017 +0200
@@ -25,6 +25,11 @@
 
 package java.nio.charset;
 
+import jdk.internal.misc.VM;
+import sun.nio.cs.StandardCharsets;
+import sun.nio.cs.ThreadLocalCoders;
+import sun.security.action.GetPropertyAction;
+
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.spi.CharsetProvider;
@@ -38,15 +43,11 @@
 import java.util.Map;
 import java.util.NoSuchElementException;
 import java.util.Objects;
-import java.util.Set;
+import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
-import java.util.ServiceConfigurationError;
+import java.util.Set;
 import java.util.SortedMap;
 import java.util.TreeMap;
-import jdk.internal.misc.VM;
-import sun.nio.cs.StandardCharsets;
-import sun.nio.cs.ThreadLocalCoders;
-import sun.security.action.GetPropertyAction;
 
 
 /**
@@ -635,10 +636,19 @@
      *         If the canonical name or any of the aliases are illegal
      */
     protected Charset(String canonicalName, String[] aliases) {
-        checkName(canonicalName);
         String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
-        for (int i = 0; i < as.length; i++)
-            checkName(as[i]);
+
+        // Skip checks for the standard, built-in Charsets we always load
+        // during initialization.  Use of identity is intentional to be
+        // consistent with sun.nio.cs.StandardCharsets
+        if (canonicalName != StandardCharsets.ISO_8859_1
+                && canonicalName != StandardCharsets.US_ASCII
+                && canonicalName != StandardCharsets.UTF_8) {
+            checkName(canonicalName);
+            for (int i = 0; i < as.length; i++) {
+                checkName(as[i]);
+            }
+        }
         this.name = canonicalName;
         this.aliases = as;
     }
--- a/jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/src/java.base/share/classes/sun/nio/cs/ISO_8859_1.java	Wed Jul 19 14:40:50 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,7 +42,7 @@
 {
 
     public ISO_8859_1() {
-        super("ISO-8859-1", StandardCharsets.aliases_ISO_8859_1);
+        super(StandardCharsets.ISO_8859_1, StandardCharsets.aliases_ISO_8859_1);
     }
 
     public String historicalName() {
--- a/jdk/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/src/java.base/share/classes/sun/nio/cs/StandardCharsets.java.template	Wed Jul 19 14:40:50 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
@@ -51,6 +51,12 @@
 
     private static final String packagePrefix = "sun.nio.cs";
 
+    public static final String US_ASCII = "US-ASCII";
+
+    public static final String ISO_8859_1 = "ISO-8859-1";
+
+    public static final String UTF_8 = "UTF-8";
+
     public StandardCharsets() {
         this.aliasMap = new Aliases();
         this.classMap = new Classes();
@@ -103,13 +109,13 @@
 
         // As all charset class names added to classMap are string literals we
         // can check identity here as an optimization
-        if (cln == "US_ASCII") {
+        if (cln == US_ASCII) {
             return cache(csn, new US_ASCII());
         }
-        if (cln == "ISO_8859_1") {
+        if (cln == ISO_8859_1) {
             return cache(csn, new ISO_8859_1());
         }
-        if (cln == "UTF_8") {
+        if (cln == UTF_8) {
             return cache(csn, new UTF_8());
         }
 
--- a/jdk/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/src/java.base/share/classes/sun/nio/cs/US_ASCII.java	Wed Jul 19 14:40:50 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2004, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -38,7 +38,7 @@
 {
 
     public US_ASCII() {
-        super("US-ASCII", StandardCharsets.aliases_US_ASCII);
+        super(StandardCharsets.US_ASCII, StandardCharsets.aliases_US_ASCII);
     }
 
     public String historicalName() {
--- a/jdk/src/java.base/share/classes/sun/nio/cs/UTF_8.java	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/src/java.base/share/classes/sun/nio/cs/UTF_8.java	Wed Jul 19 14:40:50 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -57,7 +57,7 @@
 class UTF_8 extends Unicode
 {
     public UTF_8() {
-        super("UTF-8", StandardCharsets.aliases_UTF_8);
+        super(StandardCharsets.UTF_8, StandardCharsets.aliases_UTF_8);
     }
 
     public String historicalName() {
--- a/jdk/test/java/nio/charset/Charset/IllegalCharsetName.java	Tue Jul 18 17:56:52 2017 -0700
+++ b/jdk/test/java/nio/charset/Charset/IllegalCharsetName.java	Wed Jul 19 14:40:50 2017 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,15 +22,12 @@
  */
 
 /* @test
- * @bug 6330020
+ * @bug 6330020 8184665
  * @summary Ensure Charset.forName/isSupport throws the correct exception
  *          if the charset names passed in are illegal.
  */
 
-import java.io.*;
-import java.nio.*;
 import java.nio.charset.*;
-import java.util.*;
 
 public class IllegalCharsetName {
     public static void main(String[] args) throws Exception {
@@ -59,5 +56,18 @@
             } catch (IllegalCharsetNameException x) { //expected
             }
         }
+
+        // Standard charsets may bypass alias checking during startup, test that
+        // they're all well-behaved as a sanity test
+        checkAliases(StandardCharsets.ISO_8859_1);
+        checkAliases(StandardCharsets.US_ASCII);
+        checkAliases(StandardCharsets.UTF_8);
+    }
+
+    private static void checkAliases(Charset cs) {
+        for (String alias : cs.aliases()) {
+            Charset.forName(alias);
+            Charset.isSupported(alias);
+        }
     }
 }