Enum UnicodeCharset

  • All Implemented Interfaces:
    Serializable, Comparable<UnicodeCharset>

    public enum UnicodeCharset
    extends Enum<UnicodeCharset>
    A thin wrapper around Charset for the Unicode charsets. Notes:

    Java inserts a BOM when a string is encoded into a byte array with the generic (neither LE or BE) UTF-16 charset. Java does not do that for the generic UTF-32, or UTF-8. As a result, the generic UTF-16 and UTF-32 are avoided by other classes in this package, opting instead for the more consistent LE/BE variants that never insert a BOM (and, upon decoding, ignore one if present).

    Since Java 5, strings are internally stored in UTF-16, not UCS-2, which means that they can represent code points above 0xFFFF.

    UTF-32 encodings may not be supported on all platforms.

    Since:
    0.6.0
    Version:
    $Id: UnicodeCharset.java 17757 2018-11-13 20:41:13Z colin $
    Author:
    tlerios@marketcetera.com
    • Field Detail

      • mName

        private final String mName
      • mCharset

        private final Charset mCharset
    • Constructor Detail

      • UnicodeCharset

        private UnicodeCharset​(String name)
        Creates a new charset with the given name.
        Parameters:
        name - The charset name as understood by {@link Charset.forName(String)}.
    • Method Detail

      • values

        public static UnicodeCharset[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (UnicodeCharset c : UnicodeCharset.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static UnicodeCharset valueOf​(String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        IllegalArgumentException - if this enum type has no constant with the specified name
        NullPointerException - if the argument is null
      • getName

        public String getName()
        Returns the receiver's name.
        Returns:
        The name.
      • getCharset

        public Charset getCharset()
        Returns the receiver's wrapped Java charset.
        Returns:
        The charset. It is null if the JVM does not support this charset.
      • isSupported

        public boolean isSupported()
        Checks whether the JVM supports the receiver's charset.
        Returns:
        True if so.
      • assertSupported

        public void assertSupported()
                             throws I18NException
        Asserts that the JVM supports the receiver's charset.
        Throws:
        I18NException - Thrown if it does not.
      • decode

        public String decode​(byte[] data,
                             int offset,
                             int length)
                      throws I18NException
        Decodes the given portion of the given byte array using the receiver's charset, and returns the result.
        Parameters:
        data - The byte array, which may be null.
        offset - The starting point for decoding.
        length - The number of bytes to decode.
        Returns:
        The decoded string; it is null if the given byte array is null.
        Throws:
        I18NException - Thrown if the receiver is not a supported JVM charset.
      • decode

        public String decode​(byte[] data)
                      throws I18NException
        Decodes the given byte array using the receiver's charset, and returns the result.
        Parameters:
        data - The byte array, which may be null.
        Returns:
        The decoded string; it is null if the given byte array is null.
        Throws:
        I18NException - Thrown if the receiver is not a supported JVM charset.
      • encode

        public byte[] encode​(String data)
                      throws I18NException
        Encodes the given string using the receiver's charset, and returns the result.
        Parameters:
        data - The string, which may be null.
        Returns:
        The encoded byte array; it is null if the given string is null.
        Throws:
        I18NException - Thrown if the receiver is not a supported JVM charset.