< prev index next >

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

Print this page
rev 14210 : 8154231: Simplify access to System properties from JDK code
Reviewed-by: rriggs


 266  *
 267  * @see CharsetDecoder
 268  * @see CharsetEncoder
 269  * @see java.nio.charset.spi.CharsetProvider
 270  * @see java.lang.Character
 271  */
 272 
 273 public abstract class Charset
 274     implements Comparable<Charset>
 275 {
 276 
 277     /* -- Static methods -- */
 278 
 279     private static volatile String bugLevel;
 280 
 281     static boolean atBugLevel(String bl) {              // package-private
 282         String level = bugLevel;
 283         if (level == null) {
 284             if (!VM.isBooted())
 285                 return false;
 286             bugLevel = level = AccessController.doPrivileged(
 287                 new GetPropertyAction("sun.nio.cs.bugLevel", ""));
 288         }
 289         return level.equals(bl);
 290     }
 291 
 292     /**
 293      * Checks that the given string is a legal charset name. </p>
 294      *
 295      * @param  s
 296      *         A purported charset name
 297      *
 298      * @throws  IllegalCharsetNameException
 299      *          If the given name is not a legal charset name
 300      */
 301     private static void checkName(String s) {
 302         int n = s.length();
 303         if (!atBugLevel("1.4")) {
 304             if (n == 0)
 305                 throw new IllegalCharsetNameException(s);
 306         }
 307         for (int i = 0; i < n; i++) {


 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
 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     /* -- Instance fields and methods -- */
 626 
 627     private final String name;          // tickles a bug in oldjavac
 628     private final String[] aliases;     // tickles a bug in oldjavac
 629     private final String[] zeroAliases = new String[0];
 630     private Set<String> aliasSet = null;
 631 
 632     /**
 633      * Initializes a new charset with the given canonical name and alias




 266  *
 267  * @see CharsetDecoder
 268  * @see CharsetEncoder
 269  * @see java.nio.charset.spi.CharsetProvider
 270  * @see java.lang.Character
 271  */
 272 
 273 public abstract class Charset
 274     implements Comparable<Charset>
 275 {
 276 
 277     /* -- Static methods -- */
 278 
 279     private static volatile String bugLevel;
 280 
 281     static boolean atBugLevel(String bl) {              // package-private
 282         String level = bugLevel;
 283         if (level == null) {
 284             if (!VM.isBooted())
 285                 return false;
 286             bugLevel = level =
 287                     GetPropertyAction.getProperty("sun.nio.cs.bugLevel", "");
 288         }
 289         return level.equals(bl);
 290     }
 291 
 292     /**
 293      * Checks that the given string is a legal charset name. </p>
 294      *
 295      * @param  s
 296      *         A purported charset name
 297      *
 298      * @throws  IllegalCharsetNameException
 299      *          If the given name is not a legal charset name
 300      */
 301     private static void checkName(String s) {
 302         int n = s.length();
 303         if (!atBugLevel("1.4")) {
 304             if (n == 0)
 305                 throw new IllegalCharsetNameException(s);
 306         }
 307         for (int i = 0; i < n; i++) {


 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
 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 = GetPropertyAction.getProperty("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 final String[] zeroAliases = new String[0];
 629     private Set<String> aliasSet = null;
 630 
 631     /**
 632      * Initializes a new charset with the given canonical name and alias


< prev index next >