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

Print this page
rev 13834 : 8151384: Examine sun.misc.ASCIICaseInsensitiveComparator
Reviewed-by: shade, sherman


  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.misc.ASCIICaseInsensitiveComparator;
  48 import sun.nio.cs.StandardCharsets;
  49 import sun.nio.cs.ThreadLocalCoders;
  50 import sun.security.action.GetPropertyAction;
  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


 562      * enumerate all of the available charsets, for example to allow user
 563      * charset selection.  This method is not used by the {@link #forName
 564      * forName} method, which instead employs an efficient incremental lookup
 565      * algorithm.
 566      *
 567      * <p> This method may return different results at different times if new
 568      * charset providers are dynamically made available to the current Java
 569      * virtual machine.  In the absence of such changes, the charsets returned
 570      * by this method are exactly those that can be retrieved via the {@link
 571      * #forName forName} method.  </p>
 572      *
 573      * @return An immutable, case-insensitive map from canonical charset names
 574      *         to charset objects
 575      */
 576     public static SortedMap<String,Charset> availableCharsets() {
 577         return AccessController.doPrivileged(
 578             new PrivilegedAction<>() {
 579                 public SortedMap<String,Charset> run() {
 580                     TreeMap<String,Charset> m =
 581                         new TreeMap<>(
 582                             ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
 583                     put(standardProvider.charsets(), m);
 584                     CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
 585                     for (CharsetProvider ecp :ecps) {
 586                         put(ecp.charsets(), m);
 587                     }
 588                     for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
 589                         CharsetProvider cp = i.next();
 590                         put(cp.charsets(), m);
 591                     }
 592                     return Collections.unmodifiableSortedMap(m);
 593                 }
 594             });
 595     }
 596 
 597     private static volatile Charset defaultCharset;
 598 
 599     /**
 600      * Returns the default charset of this Java virtual machine.
 601      *
 602      * <p> The default charset is determined during virtual-machine startup and




  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


 561      * enumerate all of the available charsets, for example to allow user
 562      * charset selection.  This method is not used by the {@link #forName
 563      * forName} method, which instead employs an efficient incremental lookup
 564      * algorithm.
 565      *
 566      * <p> This method may return different results at different times if new
 567      * charset providers are dynamically made available to the current Java
 568      * virtual machine.  In the absence of such changes, the charsets returned
 569      * by this method are exactly those that can be retrieved via the {@link
 570      * #forName forName} method.  </p>
 571      *
 572      * @return An immutable, case-insensitive map from canonical charset names
 573      *         to charset objects
 574      */
 575     public static SortedMap<String,Charset> availableCharsets() {
 576         return AccessController.doPrivileged(
 577             new PrivilegedAction<>() {
 578                 public SortedMap<String,Charset> run() {
 579                     TreeMap<String,Charset> m =
 580                         new TreeMap<>(
 581                             String.CASE_INSENSITIVE_ORDER);
 582                     put(standardProvider.charsets(), m);
 583                     CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
 584                     for (CharsetProvider ecp :ecps) {
 585                         put(ecp.charsets(), m);
 586                     }
 587                     for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
 588                         CharsetProvider cp = i.next();
 589                         put(cp.charsets(), m);
 590                     }
 591                     return Collections.unmodifiableSortedMap(m);
 592                 }
 593             });
 594     }
 595 
 596     private static volatile Charset defaultCharset;
 597 
 598     /**
 599      * Returns the default charset of this Java virtual machine.
 600      *
 601      * <p> The default charset is determined during virtual-machine startup and