318
319 /* The standard set of charsets */
320 private static CharsetProvider standardProvider = new StandardCharsets();
321
322 // Cache of the most-recently-returned charsets,
323 // along with the names that were used to find them
324 //
325 private static volatile Object[] cache1 = null; // "Level 1" cache
326 private static volatile Object[] cache2 = null; // "Level 2" cache
327
328 private static void cache(String charsetName, Charset cs) {
329 cache2 = cache1;
330 cache1 = new Object[] { charsetName, cs };
331 }
332
333 // Creates an iterator that walks over the available providers, ignoring
334 // those whose lookup or instantiation causes a security exception to be
335 // thrown. Should be invoked with full privileges.
336 //
337 private static Iterator<CharsetProvider> providers() {
338 return new Iterator<CharsetProvider>() {
339
340 ClassLoader cl = ClassLoader.getSystemClassLoader();
341 ServiceLoader<CharsetProvider> sl =
342 ServiceLoader.load(CharsetProvider.class, cl);
343 Iterator<CharsetProvider> i = sl.iterator();
344
345 CharsetProvider next = null;
346
347 private boolean getNext() {
348 while (next == null) {
349 try {
350 if (!i.hasNext())
351 return false;
352 next = i.next();
353 } catch (ServiceConfigurationError sce) {
354 if (sce.getCause() instanceof SecurityException) {
355 // Ignore security exceptions
356 continue;
357 }
358 throw sce;
387 private static Charset lookupViaProviders(final String charsetName) {
388
389 // The runtime startup sequence looks up standard charsets as a
390 // consequence of the VM's invocation of System.initializeSystemClass
391 // in order to, e.g., set system properties and encode filenames. At
392 // that point the application class loader has not been initialized,
393 // however, so we can't look for providers because doing so will cause
394 // that loader to be prematurely initialized with incomplete
395 // information.
396 //
397 if (!sun.misc.VM.isBooted())
398 return null;
399
400 if (gate.get() != null)
401 // Avoid recursive provider lookups
402 return null;
403 try {
404 gate.set(gate);
405
406 return AccessController.doPrivileged(
407 new PrivilegedAction<Charset>() {
408 public Charset run() {
409 for (Iterator<CharsetProvider> i = providers();
410 i.hasNext();) {
411 CharsetProvider cp = i.next();
412 Charset cs = cp.charsetForName(charsetName);
413 if (cs != null)
414 return cs;
415 }
416 return null;
417 }
418 });
419
420 } finally {
421 gate.set(null);
422 }
423 }
424
425 /* The extended set of charsets */
426 private static class ExtendedProviderHolder {
427 static final CharsetProvider extendedProvider = extendedProvider();
428 // returns ExtendedProvider, if installed
429 private static CharsetProvider extendedProvider() {
430 return AccessController.doPrivileged(
431 new PrivilegedAction<CharsetProvider>() {
432 public CharsetProvider run() {
433 try {
434 Class<?> epc
435 = Class.forName("sun.nio.cs.ext.ExtendedCharsets");
436 return (CharsetProvider)epc.newInstance();
437 } catch (ClassNotFoundException x) {
438 // Extended charsets not available
439 // (charsets.jar not present)
440 } catch (InstantiationException |
441 IllegalAccessException x) {
442 throw new Error(x);
443 }
444 return null;
445 }
446 });
447 }
448 }
449
450 private static Charset lookupExtendedCharset(String charsetName) {
451 CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
553 *
554 * <p> The invocation of this method, and the subsequent use of the
555 * resulting map, may cause time-consuming disk or network I/O operations
556 * to occur. This method is provided for applications that need to
557 * enumerate all of the available charsets, for example to allow user
558 * charset selection. This method is not used by the {@link #forName
559 * forName} method, which instead employs an efficient incremental lookup
560 * algorithm.
561 *
562 * <p> This method may return different results at different times if new
563 * charset providers are dynamically made available to the current Java
564 * virtual machine. In the absence of such changes, the charsets returned
565 * by this method are exactly those that can be retrieved via the {@link
566 * #forName forName} method. </p>
567 *
568 * @return An immutable, case-insensitive map from canonical charset names
569 * to charset objects
570 */
571 public static SortedMap<String,Charset> availableCharsets() {
572 return AccessController.doPrivileged(
573 new PrivilegedAction<SortedMap<String,Charset>>() {
574 public SortedMap<String,Charset> run() {
575 TreeMap<String,Charset> m =
576 new TreeMap<String,Charset>(
577 ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
578 put(standardProvider.charsets(), m);
579 CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
580 if (ecp != null)
581 put(ecp.charsets(), m);
582 for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
583 CharsetProvider cp = i.next();
584 put(cp.charsets(), m);
585 }
586 return Collections.unmodifiableSortedMap(m);
587 }
588 });
589 }
590
591 private static volatile Charset defaultCharset;
592
593 /**
594 * Returns the default charset of this Java virtual machine.
595 *
596 * <p> The default charset is determined during virtual-machine startup and
646 }
647
648 /**
649 * Returns this charset's canonical name.
650 *
651 * @return The canonical name of this charset
652 */
653 public final String name() {
654 return name;
655 }
656
657 /**
658 * Returns a set containing this charset's aliases.
659 *
660 * @return An immutable set of this charset's aliases
661 */
662 public final Set<String> aliases() {
663 if (aliasSet != null)
664 return aliasSet;
665 int n = aliases.length;
666 HashSet<String> hs = new HashSet<String>(n);
667 for (int i = 0; i < n; i++)
668 hs.add(aliases[i]);
669 aliasSet = Collections.unmodifiableSet(hs);
670 return aliasSet;
671 }
672
673 /**
674 * Returns this charset's human-readable name for the default locale.
675 *
676 * <p> The default implementation of this method simply returns this
677 * charset's canonical name. Concrete subclasses of this class may
678 * override this method in order to provide a localized display name. </p>
679 *
680 * @return The display name of this charset in the default locale
681 */
682 public String displayName() {
683 return name;
684 }
685
686 /**
|
318
319 /* The standard set of charsets */
320 private static CharsetProvider standardProvider = new StandardCharsets();
321
322 // Cache of the most-recently-returned charsets,
323 // along with the names that were used to find them
324 //
325 private static volatile Object[] cache1 = null; // "Level 1" cache
326 private static volatile Object[] cache2 = null; // "Level 2" cache
327
328 private static void cache(String charsetName, Charset cs) {
329 cache2 = cache1;
330 cache1 = new Object[] { charsetName, cs };
331 }
332
333 // Creates an iterator that walks over the available providers, ignoring
334 // those whose lookup or instantiation causes a security exception to be
335 // thrown. Should be invoked with full privileges.
336 //
337 private static Iterator<CharsetProvider> providers() {
338 return new Iterator<>() {
339
340 ClassLoader cl = ClassLoader.getSystemClassLoader();
341 ServiceLoader<CharsetProvider> sl =
342 ServiceLoader.load(CharsetProvider.class, cl);
343 Iterator<CharsetProvider> i = sl.iterator();
344
345 CharsetProvider next = null;
346
347 private boolean getNext() {
348 while (next == null) {
349 try {
350 if (!i.hasNext())
351 return false;
352 next = i.next();
353 } catch (ServiceConfigurationError sce) {
354 if (sce.getCause() instanceof SecurityException) {
355 // Ignore security exceptions
356 continue;
357 }
358 throw sce;
387 private static Charset lookupViaProviders(final String charsetName) {
388
389 // The runtime startup sequence looks up standard charsets as a
390 // consequence of the VM's invocation of System.initializeSystemClass
391 // in order to, e.g., set system properties and encode filenames. At
392 // that point the application class loader has not been initialized,
393 // however, so we can't look for providers because doing so will cause
394 // that loader to be prematurely initialized with incomplete
395 // information.
396 //
397 if (!sun.misc.VM.isBooted())
398 return null;
399
400 if (gate.get() != null)
401 // Avoid recursive provider lookups
402 return null;
403 try {
404 gate.set(gate);
405
406 return AccessController.doPrivileged(
407 new PrivilegedAction<>() {
408 public Charset run() {
409 for (Iterator<CharsetProvider> i = providers();
410 i.hasNext();) {
411 CharsetProvider cp = i.next();
412 Charset cs = cp.charsetForName(charsetName);
413 if (cs != null)
414 return cs;
415 }
416 return null;
417 }
418 });
419
420 } finally {
421 gate.set(null);
422 }
423 }
424
425 /* The extended set of charsets */
426 private static class ExtendedProviderHolder {
427 static final CharsetProvider extendedProvider = extendedProvider();
428 // returns ExtendedProvider, if installed
429 private static CharsetProvider extendedProvider() {
430 return AccessController.doPrivileged(
431 new PrivilegedAction<>() {
432 public CharsetProvider run() {
433 try {
434 Class<?> epc
435 = Class.forName("sun.nio.cs.ext.ExtendedCharsets");
436 return (CharsetProvider)epc.newInstance();
437 } catch (ClassNotFoundException x) {
438 // Extended charsets not available
439 // (charsets.jar not present)
440 } catch (InstantiationException |
441 IllegalAccessException x) {
442 throw new Error(x);
443 }
444 return null;
445 }
446 });
447 }
448 }
449
450 private static Charset lookupExtendedCharset(String charsetName) {
451 CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
553 *
554 * <p> The invocation of this method, and the subsequent use of the
555 * resulting map, may cause time-consuming disk or network I/O operations
556 * to occur. This method is provided for applications that need to
557 * enumerate all of the available charsets, for example to allow user
558 * charset selection. This method is not used by the {@link #forName
559 * forName} method, which instead employs an efficient incremental lookup
560 * algorithm.
561 *
562 * <p> This method may return different results at different times if new
563 * charset providers are dynamically made available to the current Java
564 * virtual machine. In the absence of such changes, the charsets returned
565 * by this method are exactly those that can be retrieved via the {@link
566 * #forName forName} method. </p>
567 *
568 * @return An immutable, case-insensitive map from canonical charset names
569 * to charset objects
570 */
571 public static SortedMap<String,Charset> availableCharsets() {
572 return AccessController.doPrivileged(
573 new PrivilegedAction<>() {
574 public SortedMap<String,Charset> run() {
575 TreeMap<String,Charset> m =
576 new TreeMap<>(
577 ASCIICaseInsensitiveComparator.CASE_INSENSITIVE_ORDER);
578 put(standardProvider.charsets(), m);
579 CharsetProvider ecp = ExtendedProviderHolder.extendedProvider;
580 if (ecp != null)
581 put(ecp.charsets(), m);
582 for (Iterator<CharsetProvider> i = providers(); i.hasNext();) {
583 CharsetProvider cp = i.next();
584 put(cp.charsets(), m);
585 }
586 return Collections.unmodifiableSortedMap(m);
587 }
588 });
589 }
590
591 private static volatile Charset defaultCharset;
592
593 /**
594 * Returns the default charset of this Java virtual machine.
595 *
596 * <p> The default charset is determined during virtual-machine startup and
646 }
647
648 /**
649 * Returns this charset's canonical name.
650 *
651 * @return The canonical name of this charset
652 */
653 public final String name() {
654 return name;
655 }
656
657 /**
658 * Returns a set containing this charset's aliases.
659 *
660 * @return An immutable set of this charset's aliases
661 */
662 public final Set<String> aliases() {
663 if (aliasSet != null)
664 return aliasSet;
665 int n = aliases.length;
666 HashSet<String> hs = new HashSet<>(n);
667 for (int i = 0; i < n; i++)
668 hs.add(aliases[i]);
669 aliasSet = Collections.unmodifiableSet(hs);
670 return aliasSet;
671 }
672
673 /**
674 * Returns this charset's human-readable name for the default locale.
675 *
676 * <p> The default implementation of this method simply returns this
677 * charset's canonical name. Concrete subclasses of this class may
678 * override this method in order to provide a localized display name. </p>
679 *
680 * @return The display name of this charset in the default locale
681 */
682 public String displayName() {
683 return name;
684 }
685
686 /**
|