- All Implemented Interfaces:
Comparable<Charset>
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 CharsetProvider
class.
All of the methods defined in this class are safe for use by multiple concurrent threads.
Charset names
Charsets are named by strings composed of the following characters:
- The uppercase letters
'A'through'Z'('\u0041'through'\u005a'), - The lowercase letters
'a'through'z'('\u0061'through'\u007a'), - The digits
'0'through'9'('\u0030'through'\u0039'), - The dash character
'-'('\u002d', HYPHEN-MINUS ), - The plus character
'+'('\u002b', PLUS SIGN ), - The period character
'.'('\u002e', FULL STOP ), - The colon character
':'('\u003a', COLON ), and - The underscore character
'_'('\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.
Standard charsets
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
The UTF-8
charset is specified by RFC 2279; the transformation format upon which it is based is specified in Amendment 2 of 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 Amendment 1 of ISO 10646-1 and are also described in the Unicode Standard.
The UTF-16
charsets use sixteen-bit quantities 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 '\uFEFF'
. Byte-order marks are handled as follows:
When decoding, the
UTF-16BEandUTF-16LEcharsets 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-16charset interprets 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.
Terminology
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.
- Since:
- 1.4
- External Specifications
- See Also:
Constructors
- ✓protected Charset(java.lang.String arg0, java.lang.String[] arg1)
Methods
- ✓public final java.util.Set<java.lang.String> aliases()
- ✓public static java.util.SortedMap<java.lang.String, java.nio.charset.Charset> availableCharsets()
- ✓public boolean canEncode()
- ✓public final int compareTo(java.nio.charset.Charset arg0)
- ✓public abstract boolean contains(java.nio.charset.Charset arg0)
- ✗public final java.nio.CharBuffer decode(java.nio.ByteBuffer arg0)Comparing jdk-20-ga and jdk-21+35
decode
Convenience method that decodes bytes in this charset into Unicode characters.An invocation of this method upon a charset
csreturns the same result as the expression Moved out of a preformatted block.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.cs.newDecoder()Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..onMalformedInput(CodingErrorAction.REPLACE)Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..onUnmappableCharacter(CodingErrorAction.REPLACE)Moved out of a paragraph. Moved out of a paragraph.except that it is potentially more efficient because it can cache decoders between successive invocations.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..decode(bb);Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.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
CharsetDecoder.decode(java.nio.ByteBuffer)method directly.- Parameters:
bb- The byte buffer to be decoded- Returns:
- A char buffer containing the decoded characters
- ✓public static java.nio.charset.Charset defaultCharset()
- ✓public java.lang.String displayName()
- ✓public java.lang.String displayName(java.util.Locale arg0)
- ✗public final java.nio.ByteBuffer encode(java.lang.String arg0)Comparing jdk-20-ga and jdk-21+35
encode
Convenience method that encodes a string into bytes in this charset.An invocation of this method upon a charset
csreturns the same result as the expression Moved out of a preformatted block.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.cs.encode(CharBuffer.wrap(s));Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.- Parameters:
str- The string to be encoded- Returns:
- A byte buffer containing the encoded characters
- ✗public final java.nio.ByteBuffer encode(java.nio.CharBuffer arg0)Comparing jdk-20-ga and jdk-21+35
encode
Convenience method that encodes Unicode characters into bytes in this charset.An invocation of this method upon a charset
csreturns the same result as the expression Moved out of a preformatted block.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java.cs.newEncoder()Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..onMalformedInput(CodingErrorAction.REPLACE)Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..onUnmappableCharacter(CodingErrorAction.REPLACE)Moved out of a paragraph. Moved out of a paragraph.except that it is potentially more efficient because it can cache encoders between successive invocations.Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.Moved to a computer code block with class language-java..encode(bb);Moved out of a paragraph.Moved out of a preformatted block.Moved to a division with class snippet-container.Moved to a preformatted block with class snippet.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
CharsetEncoder.encode(java.nio.CharBuffer)method directly.- Parameters:
cb- The char buffer to be encoded- Returns:
- A byte buffer containing the encoded characters
- ✓public final boolean equals(java.lang.Object arg0)
- ✓public static java.nio.charset.Charset forName(java.lang.String arg0)
- ✓public static java.nio.charset.Charset forName(java.lang.String arg0, java.nio.charset.Charset arg1)
- ✓public final int hashCode()
- ✗public final boolean isRegistered()Comparing jdk-20-ga and jdk-21+35
isRegistered
public final boolean isRegistered()Tells whether or not this charset is registered in the IANA Charset Registry.- Returns:
trueif, and only if, this charset is known by its implementor to be registered with the IANA- External Specifications
- ✓public static boolean isSupported(java.lang.String arg0)
- ✓public final java.lang.String name()
- ✓public abstract java.nio.charset.CharsetDecoder newDecoder()
- ✓public abstract java.nio.charset.CharsetEncoder newEncoder()
- ✓public final java.lang.String toString()
Summary
| Elements | Comments | Descriptions | Total | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Added | Changed | Removed | Added | Changed | Removed | Added | Changed | Removed | ||
| Charset | 3 | 3 | ||||||||
| decode(ByteBuffer) | 3 | 2 | 4 | 9 | ||||||
| encode(String) | 3 | 1 | 1 | 5 | ||||||
| encode(CharBuffer) | 3 | 2 | 4 | 9 | ||||||
| isRegistered() | 4 | 4 | ||||||||
| Total | 16 | 5 | 9 | 30 | ||||||