< prev index next >

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

Print this page
rev 17470 : 8184665: Skip name and alias checks for standard Charsets
Reviewed-by: sherman, rriggs, forax


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.charset;
  27 





  28 import java.nio.ByteBuffer;
  29 import java.nio.CharBuffer;
  30 import java.nio.charset.spi.CharsetProvider;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.Arrays;
  34 import java.util.Collections;
  35 import java.util.HashSet;
  36 import java.util.Iterator;
  37 import java.util.Locale;
  38 import java.util.Map;
  39 import java.util.NoSuchElementException;
  40 import java.util.Objects;
  41 import java.util.Set;
  42 import java.util.ServiceLoader;
  43 import java.util.ServiceConfigurationError;


  44 import java.util.SortedMap;
  45 import java.util.TreeMap;
  46 import jdk.internal.misc.VM;
  47 import sun.nio.cs.StandardCharsets;
  48 import sun.nio.cs.ThreadLocalCoders;
  49 import sun.security.action.GetPropertyAction;
  50 
  51 
  52 /**
  53  * A named mapping between sequences of sixteen-bit Unicode <a
  54  * href="../../lang/Character.html#unicode">code units</a> and sequences of
  55  * bytes.  This class defines methods for creating decoders and encoders and
  56  * for retrieving the various names associated with a charset.  Instances of
  57  * this class are immutable.
  58  *
  59  * <p> This class also defines static methods for testing whether a particular
  60  * charset is supported, for locating charset instances by name, and for
  61  * constructing a map that contains every charset for which support is
  62  * available in the current Java virtual machine.  Support for new charsets can
  63  * be added via the service-provider interface defined in the {@link
  64  * java.nio.charset.spi.CharsetProvider} class.
  65  *
  66  * <p> All of the methods defined in this class are safe for use by multiple
  67  * concurrent threads.
  68  *
  69  *


 618     /* -- Instance fields and methods -- */
 619 
 620     private final String name;          // tickles a bug in oldjavac
 621     private final String[] aliases;     // tickles a bug in oldjavac
 622     private Set<String> aliasSet = null;
 623 
 624     /**
 625      * Initializes a new charset with the given canonical name and alias
 626      * set.
 627      *
 628      * @param  canonicalName
 629      *         The canonical name of this charset
 630      *
 631      * @param  aliases
 632      *         An array of this charset's aliases, or null if it has no aliases
 633      *
 634      * @throws IllegalCharsetNameException
 635      *         If the canonical name or any of the aliases are illegal
 636      */
 637     protected Charset(String canonicalName, String[] aliases) {
 638         checkName(canonicalName);
 639         String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
 640         for (int i = 0; i < as.length; i++)








 641             checkName(as[i]);


 642         this.name = canonicalName;
 643         this.aliases = as;
 644     }
 645 
 646     /**
 647      * Returns this charset's canonical name.
 648      *
 649      * @return  The canonical name of this charset
 650      */
 651     public final String name() {
 652         return name;
 653     }
 654 
 655     /**
 656      * Returns a set containing this charset's aliases.
 657      *
 658      * @return  An immutable set of this charset's aliases
 659      */
 660     public final Set<String> aliases() {
 661         if (aliasSet != null)




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.nio.charset;
  27 
  28 import jdk.internal.misc.VM;
  29 import sun.nio.cs.StandardCharsets;
  30 import sun.nio.cs.ThreadLocalCoders;
  31 import sun.security.action.GetPropertyAction;
  32 
  33 import java.nio.ByteBuffer;
  34 import java.nio.CharBuffer;
  35 import java.nio.charset.spi.CharsetProvider;
  36 import java.security.AccessController;
  37 import java.security.PrivilegedAction;
  38 import java.util.Arrays;
  39 import java.util.Collections;
  40 import java.util.HashSet;
  41 import java.util.Iterator;
  42 import java.util.Locale;
  43 import java.util.Map;
  44 import java.util.NoSuchElementException;
  45 import java.util.Objects;


  46 import java.util.ServiceConfigurationError;
  47 import java.util.ServiceLoader;
  48 import java.util.Set;
  49 import java.util.SortedMap;
  50 import java.util.TreeMap;




  51 
  52 
  53 /**
  54  * A named mapping between sequences of sixteen-bit Unicode <a
  55  * href="../../lang/Character.html#unicode">code units</a> and sequences of
  56  * bytes.  This class defines methods for creating decoders and encoders and
  57  * for retrieving the various names associated with a charset.  Instances of
  58  * this class are immutable.
  59  *
  60  * <p> This class also defines static methods for testing whether a particular
  61  * charset is supported, for locating charset instances by name, and for
  62  * constructing a map that contains every charset for which support is
  63  * available in the current Java virtual machine.  Support for new charsets can
  64  * be added via the service-provider interface defined in the {@link
  65  * java.nio.charset.spi.CharsetProvider} class.
  66  *
  67  * <p> All of the methods defined in this class are safe for use by multiple
  68  * concurrent threads.
  69  *
  70  *


 619     /* -- Instance fields and methods -- */
 620 
 621     private final String name;          // tickles a bug in oldjavac
 622     private final String[] aliases;     // tickles a bug in oldjavac
 623     private Set<String> aliasSet = null;
 624 
 625     /**
 626      * Initializes a new charset with the given canonical name and alias
 627      * set.
 628      *
 629      * @param  canonicalName
 630      *         The canonical name of this charset
 631      *
 632      * @param  aliases
 633      *         An array of this charset's aliases, or null if it has no aliases
 634      *
 635      * @throws IllegalCharsetNameException
 636      *         If the canonical name or any of the aliases are illegal
 637      */
 638     protected Charset(String canonicalName, String[] aliases) {

 639         String[] as = Objects.requireNonNullElse(aliases, zeroAliases);
 640 
 641         // Skip checks for the standard, built-in Charsets we always load
 642         // during initialization.  Use of identity is intentional to be
 643         // consistent with sun.nio.cs.StandardCharsets
 644         if (canonicalName != StandardCharsets.ISO_8859_1
 645                 && canonicalName != StandardCharsets.US_ASCII
 646                 && canonicalName != StandardCharsets.UTF_8) {
 647             checkName(canonicalName);
 648             for (int i = 0; i < as.length; i++) {
 649                 checkName(as[i]);
 650             }
 651         }
 652         this.name = canonicalName;
 653         this.aliases = as;
 654     }
 655 
 656     /**
 657      * Returns this charset's canonical name.
 658      *
 659      * @return  The canonical name of this charset
 660      */
 661     public final String name() {
 662         return name;
 663     }
 664 
 665     /**
 666      * Returns a set containing this charset's aliases.
 667      *
 668      * @return  An immutable set of this charset's aliases
 669      */
 670     public final Set<String> aliases() {
 671         if (aliasSet != null)


< prev index next >