This class also defines static methods for testing whether a particular
charset is supported, for locating charset instances by name, and for
constructing a map that contains every charset for which support is
available in the current Java virtual machine. Support for new charsets can
be added via the service-provider interface defined in the java. class.
All of the methods defined in this class are safe for use by multiple concurrent threads.
Charsets are named by strings composed of the following characters:
'A' through 'Z'
('\u0041' through '\u005a'),
'a' through 'z'
('\u0061' through '\u007a'),
'0' through '9'
('\u0030' through '\u0039'),
'-'
('\u002d', HYPHEN-MINUS),
'+'
('\u002b', PLUS SIGN),
'.'
('\u002e', FULL STOP),
':'
('\u003a', COLON), and
'_'
('\u005f', LOW LINE).
Every charset has a canonical name and may also have one or more
aliases. The canonical name is returned by the name method
of this class. Canonical names are, by convention, usually in upper case.
The aliases of a charset are returned by the aliases
method.
Some charsets have an historical name that is defined for
compatibility with previous versions of the Java platform. A charset's
historical name is either its canonical name or one of its aliases. The
historical name is returned by the getEncoding() methods of the
InputStreamReader and OutputStreamWriter classes.
If a charset listed in the IANA Charset
Registry is supported by an implementation of the Java platform then
its canonical name must be the name listed in the registry. Many charsets
are given more than one name in the registry, in which case the registry
identifies one of the names as MIME-preferred. If a charset has more
than one registry name then its canonical name must be the MIME-preferred
name and the other names in the registry must be valid aliases. If a
supported charset is not listed in the IANA registry then its canonical name
must begin with one of the strings "X-" or "x-".
The IANA charset registry does change over time, and so the canonical name and the aliases of a particular charset may also change over time. To ensure compatibility it is recommended that no alias ever be removed from a charset, and that if the canonical name of a charset is changed then its previous canonical name be made into an alias.
Every implementation of the Java platform is required to support the following standard charsets. Consult the release documentation for your implementation to see if any other charsets are supported. The behavior of such optional charsets may differ between implementations.
Charset Description US-ASCIISeven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character setISO-8859-1ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1UTF-8Eight-bit UCS Transformation Format UTF-16BESixteen-bit UCS Transformation Format, big-endian byte order UTF-16LESixteen-bit UCS Transformation Format, little-endian byte order UTF-16Sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order mark UTF-32BEThirty-two-bit UCS Transformation Format, big-endian byte order UTF-32LEThirty-two-bit UCS Transformation Format, little-endian byte order UTF-32Thirty-two-bit UCS Transformation Format, byte order identified by an optional byte-order mark
The UTF-8 charset is specified by RFC 2279; the
transformation format upon which it is based is specified in
ISO 10646-1 and is also described in the Unicode
Standard.
The UTF-16 charsets are specified by RFC 2781; the
transformation formats upon which they are based are specified in
ISO 10646-1 and are also described in the Unicode
Standard.
The UTF-32 charsets are based upon transformation formats
which are specified in
ISO 10646-1 and are also described in the Unicode
Standard.
The UTF-16 and UTF-32 charsets use sixteen-bit and thirty-two-bit
quantities respectively, and are
therefore sensitive to byte order. In these encodings the byte order of a
stream may be indicated by an initial byte-order mark represented by
the Unicode character U+FEFF. Byte-order marks are handled
as follows:
When decoding, the UTF-16BE, UTF-16LE,
UTF-32BE, and UTF-32LE
charsets interpret the initial byte-order marks as a ZERO-WIDTH
NON-BREAKING SPACE; when encoding, they do not write
byte-order marks.
When decoding, the UTF-16 and UTF-32 charsets interpret the
byte-order mark at the beginning of the input stream to indicate the
byte-order of the stream but defaults to big-endian if there is no
byte-order mark; when encoding, it uses big-endian byte order and writes
a big-endian byte-order mark.
Every instance of the Java virtual machine has a default charset, which
is UTF-8 unless changed in an implementation specific manner. Refer to
defaultCharset() for more detail.
The StandardCharsets class defines constants for each of the
standard charsets.
The name of this class is taken from the terms used in RFC 2278. In that document a charset is defined as the combination of one or more coded character sets and a character-encoding scheme. (This definition is confusing; some other software systems define charset as a synonym for coded character set.)
A coded character set is a mapping between a set of abstract characters and a set of integers. US-ASCII, ISO 8859-1, JIS X 0201, and Unicode are examples of coded character sets.
Some standards have defined a character set to be simply a set of abstract characters without an associated assigned numbering. An alphabet is an example of such a character set. However, the subtle distinction between character set and coded character set is rarely used in practice; the former has become a short form for the latter, including in the Java API specification.
A character-encoding scheme is a mapping between one or more coded character sets and a set of octet (eight-bit byte) sequences. UTF-8, UTF-16, ISO 2022, and EUC are examples of character-encoding schemes. Encoding schemes are often associated with a particular coded character set; UTF-8, for example, is used only to encode Unicode. Some schemes, however, are associated with multiple coded character sets; EUC, for example, can be used to encode characters in a variety of Asian coded character sets.
When a coded character set is used exclusively with a single
character-encoding scheme then the corresponding charset is usually
named for the coded character set; otherwise a charset is usually named
for the encoding scheme and, possibly, the locale of the coded
character sets that it supports. Hence US-ASCII is both the
name of a coded character set and of the charset that encodes it, while
EUC-JP is the name of the charset that encodes the
JIS X 0201, JIS X 0208, and JIS X 0212
coded character sets for the Japanese language.
The native character encoding of the Java programming language is UTF-16. A charset in the Java platform therefore defines a mapping between sequences of sixteen-bit UTF-16 code units (that is, sequences of chars) and sequences of bytes.
CharsetDecoder, CharsetEncoder, java.nio.charset.spi.CharsetProvider , java.lang.Character
| Modifier and Type | Field and Description |
|---|---|
| private final String[] | |
| private final LazyConstant | |
| private static volatile Object[] | |
| private static volatile Object[] | |
| private static final LazyConstant | |
| private static final LazyConstant | |
| private static final ScopedValue | |
| private final String | |
| private static final CharsetProvider | |
| private static final String[] |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public final Set | Returns: An immutable set of this charset's aliasesReturns a set containing this charset's aliases. |
| public static SortedMap | Returns: An immutable, case-insensitive map from canonical charset names to charset objectsConstructs a sorted map from canonical charset names to charset objects. |
| private static void | |
| public boolean | Returns: true if, and only if, this charset supports encodingTells whether or not this charset supports encoding. |
| private static void | |
| public final int | Returns: A negative integer, zero, or a positive integer as this charset is less than, equal to, or greater than the specified charsetThe charset to which this charset is to be compared that)Implements java. Compares this charset to another. |
| public abstract boolean | |
| public final CharBuffer | Returns: A char buffer containing the decoded charactersThe byte buffer to be decoded bb)Convenience method that decodes bytes in this charset into Unicode characters. |
| public static Charset | Returns: A charset object for the default charsetReturns the default charset of this Java virtual machine. |
| private static Charset | |
| public String | Returns: The display name of this charset in the default localeReturns this charset's human-readable name for the default locale. |
| public String | Returns: The display name of this charset in the given localeThe locale for which the display name is to be retrieved locale)Returns this charset's human-readable name for the given locale. |
| public final ByteBuffer | Returns: A byte buffer containing the encoded charactersThe char buffer to be encoded cb)Convenience method that encodes Unicode characters into bytes in this charset. |
| public final ByteBuffer | |
| public final boolean | Returns: true if, and only if, this charset is equal to the
given objectOverrides java. Tells whether or not this object is equal to another. |
| private static List | |
| public static Charset | |
| public static Charset | Returns: A charset object for the named charset, orfallback
in case the charset object for the named charset is not
available or charsetName is not a legal charset nameThe name of the requested charset; may be either
a canonical name or an alias charsetName, Charset fallback charset in case the charset object for the named
charset is not available or fallback)charsetName is not a legal
charset name. May be nullReturns a charset object for the named charset. |
| public final int | Returns: the hashcode for this charsetOverrides java. Returns the hashcode for this charset. |
| public final boolean | Returns: true if, and only if, this charset is known by its
implementor to be registered with the IANATells whether or not this charset is registered in the IANA Charset Registry. |
| public static boolean | Returns: true if, and only if, support for the named charset
is available in the current Java virtual machineThe name of the requested charset; may be either
a canonical name or an alias charsetName)Tells whether the named charset is supported. |
| private static Charset | |
| private static Charset | |
| private static Charset | |
| private static Charset | |
| public final String | |
| public abstract CharsetDecoder | |
| public abstract CharsetEncoder | |
| private static Iterator | |
| private static void | |
| public final String | Returns: a string describing this charsetOverrides java. Returns a string describing this charset. |
| aliases | back to summary |
|---|---|
| private final String[] aliases
| |
| aliasSet | back to summary |
|---|---|
| private final LazyConstant<Set<String>> aliasSet
| |
| cache1 | back to summary |
|---|---|
| private static volatile Object[] cache1 | |
| cache2 | back to summary |
|---|---|
| private static volatile Object[] cache2 | |
| defaultCharset | back to summary |
|---|---|
| private static final LazyConstant<Charset> defaultCharset | |
| EXTENDED_PROVIDERS | back to summary |
|---|---|
| private static final LazyConstant<List<CharsetProvider>> EXTENDED_PROVIDERS | |
| IN_LOOKUP | back to summary |
|---|---|
| private static final ScopedValue<Boolean> IN_LOOKUP | |
| name | back to summary |
|---|---|
| private final String name
| |
| standardProvider | back to summary |
|---|---|
| private static final CharsetProvider standardProvider | |
| zeroAliases | back to summary |
|---|---|
| private static final String[] zeroAliases | |
| Charset | back to summary |
|---|---|
| protected Charset(String canonicalName, String[] aliases) Initializes a new charset with the given canonical name and alias set.
| |
| aliases | back to summary |
|---|---|
| public final Set Returns a set containing this charset's aliases. | |
| availableCharsets | back to summary |
|---|---|
| public static SortedMap Constructs a sorted map from canonical charset names to charset objects. The map returned by this method will have one entry for each charset for which support is available in the current Java virtual machine. If two or more supported charsets have the same canonical name then the resulting map will contain just one of them; which one it will contain is not specified. The invocation of this method, and the subsequent use of the
resulting map, may cause time-consuming disk or network I/O operations
to occur. This method is provided for applications that need to
enumerate all of the available charsets, for example to allow user
charset selection. This method is not used by the This method may return different results at different times if new
charset providers are dynamically made available to the current Java
virtual machine. In the absence of such changes, the charsets returned
by this method are exactly those that can be retrieved via the | |
| cache | back to summary |
|---|---|
| private static void cache(String charsetName, Charset cs) | |
| canEncode | back to summary |
|---|---|
| public boolean canEncode() Tells whether or not this charset supports encoding. Nearly all charsets support encoding. The primary exceptions are
special-purpose auto-detect charsets whose decoders can determine
which of several possible encoding schemes is in use by examining the
input byte sequence. Such charsets do not support encoding because
there is no way to determine which encoding should be used on output.
Implementations of such charsets should override this method to return
| |
| checkName | back to summary |
|---|---|
| private static void checkName(String s) Checks that the given string is a legal charset name.
| |
| compareTo | back to summary |
|---|---|
| public final int compareTo(Charset that) Implements java. Compares this charset to another. Charsets are ordered by their canonical names, without regard to case. | |
| contains | back to summary |
|---|---|
| public abstract boolean contains(Charset cs) Tells whether or not this charset contains the given charset. A charset C is said to contain a charset D if, and only if, every character representable in D is also representable in C. If this relationship holds then it is guaranteed that every string that can be encoded in D can also be encoded in C without performing any replacements. That C contains D does not imply that each character representable in C by a particular byte sequence is represented in D by the same byte sequence, although sometimes this is the case. Every charset contains itself. This method computes an approximation of the containment relation:
If it returns
| |
| decode | back to summary |
|---|---|
| public final CharBuffer decode(ByteBuffer bb) Convenience method that decodes bytes in this charset into Unicode characters. An invocation of this method upon a charset cs.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(bb); This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement byte array. In order
to detect such sequences, use the
| |
| defaultCharset | back to summary |
|---|---|
| public static Charset defaultCharset() Returns the default charset of this Java virtual machine. The default charset is Implementation Note An implementation may override the default charset with
the system property
| |
| defaultCharset0 | back to summary |
|---|---|
| private static Charset defaultCharset0() | |
| displayName | back to summary |
|---|---|
| public String displayName() Returns this charset's human-readable name for the default locale. The default implementation of this method simply returns this charset's canonical name. Concrete subclasses of this class may override this method in order to provide a localized display name.
| |
| displayName | back to summary |
|---|---|
| public String displayName(Locale locale) Returns this charset's human-readable name for the given locale. The default implementation of this method simply returns this charset's canonical name. Concrete subclasses of this class may override this method in order to provide a localized display name. | |
| encode | back to summary |
|---|---|
| public final ByteBuffer encode(CharBuffer cb) Convenience method that encodes Unicode characters into bytes in this charset. An invocation of this method upon a charset cs.newEncoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .encode(bb); This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement string. In order to
detect such sequences, use the
| |
| encode | back to summary |
|---|---|
| public final ByteBuffer encode(String str) Convenience method that encodes a string into bytes in this charset. An invocation of this method upon a charset cs.encode(CharBuffer.wrap(s));
| |
| equals | back to summary |
|---|---|
| public final boolean equals(Object ob) Overrides java. Tells whether or not this object is equal to another. Two charsets are equal if, and only if, they have the same canonical names. A charset is never equal to any other type of object.
| |
| extendedProviders0 | back to summary |
|---|---|
| private static List | |
| forName | back to summary |
|---|---|
| public static Charset forName(String charsetName) Returns a charset object for the named charset.
| |
| forName | back to summary |
|---|---|
| public static Charset forName(String charsetName, Charset fallback) Returns a charset object for the named charset. If the charset object
for the named charset is not available or
| |
| hashCode | back to summary |
|---|---|
| public final int hashCode() Overrides java. Returns the hashcode for this charset.
| |
| isRegistered | back to summary |
|---|---|
| public final boolean isRegistered() Tells whether or not this charset is registered in the IANA Charset Registry.
| |
| isSupported | back to summary |
|---|---|
| public static boolean isSupported(String charsetName) Tells whether the named charset is supported.
| |
| lookup | back to summary |
|---|---|
| private static Charset lookup(String charsetName) | |
| lookup2 | back to summary |
|---|---|
| private static Charset lookup2(String charsetName) | |
| lookupExtendedCharset | back to summary |
|---|---|
| private static Charset lookupExtendedCharset(String charsetName) | |
| lookupViaProviders | back to summary |
|---|---|
| private static Charset lookupViaProviders(final String charsetName) | |
| name | back to summary |
|---|---|
| public final String name() Returns this charset's canonical name.
| |
| newDecoder | back to summary |
|---|---|
| public abstract CharsetDecoder newDecoder() Constructs a new decoder for this charset.
| |
| newEncoder | back to summary |
|---|---|
| public abstract CharsetEncoder newEncoder() Constructs a new encoder for this charset.
| |
| providers | back to summary |
|---|---|
| private static Iterator | |
| put | back to summary |
|---|---|
| private static void put(Iterator<Charset> i, Map<String, Charset> m) | |
| toString | back to summary |
|---|---|
| public final String toString() Overrides java. Returns a string describing this charset. | |