Class Charset

java.lang.Object
java.nio.charset.Charset
All Implemented Interfaces:
Comparable<Charset>

public class Charset extends Object implements Comparable<Charset>
A named character encoding. This runtime has the three charsets every Java platform must: UTF-8, US-ASCII and ISO-8859-1, under their canonical names and the JDK's aliases. Encoding and decoding go through the String constructors and getBytes that take a Charset, and the readers and writers of java.io.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Charset(String canonicalName, String[] aliases)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    final Set<String>
    The aliases of the charset.
    boolean
    Whether the charset can encode; the three this runtime has can.
    final int
    compareTo(Charset another)
    Compares canonical names, ignoring case.
    static Charset
    UTF-8, the default charset of this runtime on every platform.
    The canonical name.
    The canonical name, whatever the locale.
    final boolean
    Whether obj is a charset of the same canonical name.
    static Charset
    forName(String charsetName)
    The charset charsetName names, by canonical name or alias, in any case.
    static Charset
    forName(String charsetName, Charset fallback)
    The charset charsetName names, or fallback when the name is illegal or names a charset this runtime does not have.
    final int
    The hash of the canonical name.
    final boolean
    Whether the charset is registered with IANA: the three this runtime has are, and a name with the reserved X- prefix never is.
    static boolean
    isSupported(String charsetName)
    Whether charsetName names a charset this runtime has.
    final String
    The canonical name.
    final String
    The canonical name.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Charset

      protected Charset(String canonicalName, String[] aliases)
  • Method Details

    • compareTo

      public final int compareTo(Charset another)
      Compares canonical names, ignoring case.
      Specified by:
      compareTo in interface Comparable<Charset>
      Returns:

      a negative integer if this instance is less than another; a positive integer if this instance is greater than another; 0 if this instance has the same order as another.

      Throws
      • ClassCastException: @throws ClassCastException if another cannot be converted into something comparable to this instance.
    • name

      public final String name()
      The canonical name.
    • displayName

      public String displayName()
      The canonical name.
    • displayName

      public String displayName(Locale locale)
      The canonical name, whatever the locale.
    • aliases

      public final Set<String> aliases()
      The aliases of the charset.
    • isRegistered

      public final boolean isRegistered()
      Whether the charset is registered with IANA: the three this runtime has are, and a name with the reserved X- prefix never is.
    • canEncode

      public boolean canEncode()
      Whether the charset can encode; the three this runtime has can.
    • equals

      public final boolean equals(Object obj)
      Whether obj is a charset of the same canonical name.
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      The hash of the canonical name.
      Overrides:
      hashCode in class Object
    • toString

      public final String toString()
      The canonical name.
      Overrides:
      toString in class Object
    • defaultCharset

      public static Charset defaultCharset()
      UTF-8, the default charset of this runtime on every platform.
    • isSupported

      public static boolean isSupported(String charsetName)

      Whether charsetName names a charset this runtime has.

      Throws
      • IllegalCharsetNameException: if the name is not a legal charset name
      • IllegalArgumentException: if it is null
    • forName

      public static Charset forName(String charsetName)

      The charset charsetName names, by canonical name or alias, in any case.

      Throws
      • IllegalCharsetNameException: if the name is not a legal charset name
      • UnsupportedCharsetException: if this runtime has no such charset
      • IllegalArgumentException: if it is null
    • forName

      public static Charset forName(String charsetName, Charset fallback)
      The charset charsetName names, or fallback when the name is illegal or names a charset this runtime does not have.