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

Print this page




 576             new PrivilegedAction<>() {
 577                 public SortedMap<String,Charset> run() {
 578                     TreeMap<String,Charset> m =
 579                         new TreeMap<>(
 580                             ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
 581                     put(standardProvider.charsets(), m);
 582                     CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
 583                     for (CharsetProvider ecp :ecps) {
 584                         put(ecp.charsets(), m);
 585                     }
 586                     for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
 587                         CharsetProvider cp = i.next();
 588                         put(cp.charsets(), m);
 589                     }
 590                     return Collections.unmodifiableSortedMap(m);
 591                 }
 592             });
 593     }
 594 
 595     private static volatile Charset defaultCharset;

 596 
 597     /**
 598      * Returns the default charset of this Java virtual machine.
 599      *
 600      * <p> The default charset is determined during virtual-machine startup and
 601      * typically depends upon the locale and charset of the underlying
 602      * operating system.
 603      *
 604      * @return  A charset object for the default charset
 605      *
 606      * @since 1.5
 607      */
 608     public static Charset defaultCharset() {
 609         if (defaultCharset == null) {
 610             synchronized (Charset.class) {
 611                 String csn = AccessController.doPrivileged(
 612                     new GetPropertyAction("file.encoding"));
 613                 Charset cs = lookup(csn);
 614                 if (cs != null)
 615                     defaultCharset = cs;
 616                 else
 617                     defaultCharset = forName("UTF-8");
 618             }
 619         }
 620         return defaultCharset;




























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




 576             new PrivilegedAction<>() {
 577                 public SortedMap<String,Charset> run() {
 578                     TreeMap<String,Charset> m =
 579                         new TreeMap<>(
 580                             ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
 581                     put(standardProvider.charsets(), m);
 582                     CharsetProvider[] ecps = ExtendedProviderHolder.extendedProviders;
 583                     for (CharsetProvider ecp :ecps) {
 584                         put(ecp.charsets(), m);
 585                     }
 586                     for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
 587                         CharsetProvider cp = i.next();
 588                         put(cp.charsets(), m);
 589                     }
 590                     return Collections.unmodifiableSortedMap(m);
 591                 }
 592             });
 593     }
 594 
 595     private static volatile Charset defaultCharset;
 596     private static volatile Charset defaultUnicodeCharset;
 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
 602      * typically depends upon the locale and charset of the underlying
 603      * operating system.
 604      *
 605      * @return  A charset object for the default charset
 606      *
 607      * @since 1.5
 608      */
 609     public static Charset defaultCharset() {
 610         if (defaultCharset == null) {
 611             synchronized (Charset.class) {
 612                 String csn = AccessController.doPrivileged(
 613                     new GetPropertyAction("file.encoding"));
 614                 Charset cs = lookup(csn);
 615                 if (cs != null)
 616                     defaultCharset = cs;
 617                 else
 618                     defaultCharset = forName("UTF-8");
 619             }
 620         }
 621         return defaultCharset;
 622     }
 623 
 624     /**
 625      * Returns the default unicode charset of this Java virtual machine.
 626      *
 627      * <p> The default unicode charset is determined during virtual-machine startup
 628      * and depends on command line option "file.encoding.unicode".
 629      * UTF8 is used by default.
 630      *
 631      * @return  A charset object for the default unicode charset
 632      *
 633      */
 634     public static Charset defaultUnicodeCharset() {
 635         if (defaultUnicodeCharset == null) {
 636             synchronized (Charset.class) {
 637                 defaultUnicodeCharset = forName("UTF-8");
 638 
 639                 String csn = AccessController.doPrivileged(
 640                     new GetPropertyAction("file.encoding.unicode"));
 641                 if (csn != null) {
 642                     Charset cs = lookup(csn);
 643                     if (cs != null) {
 644                         defaultUnicodeCharset = cs;
 645                     }
 646                 }
 647             }
 648         }
 649         return defaultUnicodeCharset;
 650     }
 651 
 652 
 653     /* -- Instance fields and methods -- */
 654 
 655     private final String name;          // tickles a bug in oldjavac
 656     private final String[] aliases;     // tickles a bug in oldjavac
 657     private Set<String> aliasSet = null;
 658 
 659     /**
 660      * Initializes a new charset with the given canonical name and alias
 661      * set.
 662      *
 663      * @param  canonicalName
 664      *         The canonical name of this charset
 665      *
 666      * @param  aliases
 667      *         An array of this charset's aliases, or null if it has no aliases
 668      *
 669      * @throws IllegalCharsetNameException