< prev index next >

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

Print this page
8200310: Avoid charset lookup machinery in java.nio.charset.StandardCharsets
Reviewed-by: sherman


   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;


 294     private static void checkName(String s) {
 295         int n = s.length();
 296         if (n == 0) {
 297             throw new IllegalCharsetNameException(s);
 298         }
 299         for (int i = 0; i < n; i++) {
 300             char c = s.charAt(i);
 301             if (c >= 'A' && c <= 'Z') continue;
 302             if (c >= 'a' && c <= 'z') continue;
 303             if (c >= '0' && c <= '9') continue;
 304             if (c == '-' && i != 0) continue;
 305             if (c == '+' && i != 0) continue;
 306             if (c == ':' && i != 0) continue;
 307             if (c == '_' && i != 0) continue;
 308             if (c == '.' && i != 0) continue;
 309             throw new IllegalCharsetNameException(s);
 310         }
 311     }
 312 
 313     /* The standard set of charsets */
 314     private static final CharsetProvider standardProvider = new StandardCharsets();

 315 
 316     private static final String[] zeroAliases = new String[0];
 317 
 318     // Cache of the most-recently-returned charsets,
 319     // along with the names that were used to find them
 320     //
 321     private static volatile Object[] cache1; // "Level 1" cache
 322     private static volatile Object[] cache2; // "Level 2" cache
 323 
 324     private static void cache(String charsetName, Charset cs) {
 325         cache2 = cache1;
 326         cache1 = new Object[] { charsetName, cs };
 327     }
 328 
 329     // Creates an iterator that walks over the available providers, ignoring
 330     // those whose lookup or instantiation causes a security exception to be
 331     // thrown.  Should be invoked with full privileges.
 332     //
 333     private static Iterator<CharsetProvider> providers() {
 334         return new Iterator<>() {


 592     /**
 593      * Returns the default charset of this Java virtual machine.
 594      *
 595      * <p> The default charset is determined during virtual-machine startup and
 596      * typically depends upon the locale and charset of the underlying
 597      * operating system.
 598      *
 599      * @return  A charset object for the default charset
 600      *
 601      * @since 1.5
 602      */
 603     public static Charset defaultCharset() {
 604         if (defaultCharset == null) {
 605             synchronized (Charset.class) {
 606                 String csn = GetPropertyAction
 607                         .privilegedGetProperty("file.encoding");
 608                 Charset cs = lookup(csn);
 609                 if (cs != null)
 610                     defaultCharset = cs;
 611                 else
 612                     defaultCharset = sun.nio.cs.UTF_8.INSTANCE;
 613             }
 614         }
 615         return defaultCharset;
 616     }
 617 
 618 
 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




   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.ThreadLocalCoders;
  30 import sun.security.action.GetPropertyAction;
  31 
  32 import java.nio.ByteBuffer;
  33 import java.nio.CharBuffer;
  34 import java.nio.charset.spi.CharsetProvider;
  35 import java.security.AccessController;
  36 import java.security.PrivilegedAction;
  37 import java.util.Arrays;
  38 import java.util.Collections;
  39 import java.util.HashSet;
  40 import java.util.Iterator;
  41 import java.util.Locale;
  42 import java.util.Map;
  43 import java.util.NoSuchElementException;
  44 import java.util.Objects;
  45 import java.util.ServiceConfigurationError;
  46 import java.util.ServiceLoader;
  47 import java.util.Set;
  48 import java.util.SortedMap;


 293     private static void checkName(String s) {
 294         int n = s.length();
 295         if (n == 0) {
 296             throw new IllegalCharsetNameException(s);
 297         }
 298         for (int i = 0; i < n; i++) {
 299             char c = s.charAt(i);
 300             if (c >= 'A' && c <= 'Z') continue;
 301             if (c >= 'a' && c <= 'z') continue;
 302             if (c >= '0' && c <= '9') continue;
 303             if (c == '-' && i != 0) continue;
 304             if (c == '+' && i != 0) continue;
 305             if (c == ':' && i != 0) continue;
 306             if (c == '_' && i != 0) continue;
 307             if (c == '.' && i != 0) continue;
 308             throw new IllegalCharsetNameException(s);
 309         }
 310     }
 311 
 312     /* The standard set of charsets */
 313     private static final CharsetProvider standardProvider
 314         = new sun.nio.cs.StandardCharsets();
 315 
 316     private static final String[] zeroAliases = new String[0];
 317 
 318     // Cache of the most-recently-returned charsets,
 319     // along with the names that were used to find them
 320     //
 321     private static volatile Object[] cache1; // "Level 1" cache
 322     private static volatile Object[] cache2; // "Level 2" cache
 323 
 324     private static void cache(String charsetName, Charset cs) {
 325         cache2 = cache1;
 326         cache1 = new Object[] { charsetName, cs };
 327     }
 328 
 329     // Creates an iterator that walks over the available providers, ignoring
 330     // those whose lookup or instantiation causes a security exception to be
 331     // thrown.  Should be invoked with full privileges.
 332     //
 333     private static Iterator<CharsetProvider> providers() {
 334         return new Iterator<>() {


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


< prev index next >