Class Charsets


  • public final class Charsets
    extends java.lang.Object
    Charset utility class.
    Author:
    Alexey Stashok
    • Constructor Summary

      Constructors 
      Constructor Description
      Charsets()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void drainAllCharsets()
      Remove all preloaded charsets.
      static java.nio.charset.CharsetDecoder getCharsetDecoder​(java.nio.charset.Charset charset)
      Return the Charset's CharsetDecoder.
      static java.nio.charset.CharsetEncoder getCharsetEncoder​(java.nio.charset.Charset charset)
      Return the Charset's CharsetEncoder.
      static java.nio.charset.Charset lookupCharset​(java.lang.String charsetName)
      Lookup a Charset by name.
      static void preloadAllCharsets()
      Preloads all Charsets available to the JMV, which makes charset searching faster (at the cost of memory).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_CHARACTER_ENCODING

        public static final java.lang.String DEFAULT_CHARACTER_ENCODING
        The default character encoding of this Java virtual machine.
      • ASCII_CHARSET

        public static final java.nio.charset.Charset ASCII_CHARSET
      • UTF8_CHARSET

        public static final java.nio.charset.Charset UTF8_CHARSET
      • DEFAULT_CHARSET

        public static final java.nio.charset.Charset DEFAULT_CHARSET
        Returns the default charset of this Java virtual machine.
        See Also:
        Charset.defaultCharset()
    • Constructor Detail

      • Charsets

        public Charsets()
    • Method Detail

      • lookupCharset

        public static java.nio.charset.Charset lookupCharset​(java.lang.String charsetName)
        Lookup a Charset by name. Fixes Charset concurrency issue (http://paul.vox.com/library/post/the-mysteries-of-java-character-set-performance.html)
        Parameters:
        charsetName -
        Returns:
        Charset
      • preloadAllCharsets

        public static void preloadAllCharsets()
        Preloads all Charsets available to the JMV, which makes charset searching faster (at the cost of memory). The speed gain is most noticable in the case of non-existing charsets as it allows us to avoid an expensive call to Charset.forName(java.lang.String).
      • drainAllCharsets

        public static void drainAllCharsets()
        Remove all preloaded charsets.
      • getCharsetDecoder

        public static java.nio.charset.CharsetDecoder getCharsetDecoder​(java.nio.charset.Charset charset)
        Return the Charset's CharsetDecoder. The Charsets class maintains the CharsetDecoder thread-local cache. Be aware - this shouldn't be used by multiple threads.
        Parameters:
        charset - Charset.
        Returns:
        the Charset's CharsetDecoder.
      • getCharsetEncoder

        public static java.nio.charset.CharsetEncoder getCharsetEncoder​(java.nio.charset.Charset charset)
        Return the Charset's CharsetEncoder. The Charsets class maintains the CharsetEncoder thread-local cache. Be aware - this shouldn't be used by multiple threads.
        Parameters:
        charset - Charset.
        Returns:
        the Charset's CharsetEncoder.