java.nio.charset
Stay organized with collections
Save and categorize content based on your preferences.
Defines charsets, decoders, and encoders, for translating between bytes and
Unicode characters.
A charset is named mapping between sequences of sixteen-bit Unicode
characters and sequences of bytes, in the sense defined in RFC 2278. A
decoder is an engine which transforms bytes in a specific charset into
characters, and an encoder is an engine which transforms characters into
bytes. Encoders and decoders operate on byte and character buffers. They are
collectively referred to as coders.
The Charset
class defines methods for creating
coders for a given charset and for retrieving the various names associated with
a charset. It 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.
Most users will not use these classes directly; instead they will use the
existing charset-related constructors and methods in the String
class, together with the existing InputStreamReader
and OutputStreamWriter
classes, all
of whose implementations have been reworked to make use of the charset
facilities defined in this package. A small number of changes have been made
to the InputStreamReader
and OutputStreamWriter
classes in order to allow explicit charset objects to be specified in the
construction of instances of those classes.
Support for new charsets can be made available via the interface defined in
the CharsetProvider
class in the java.nio.charset.spi
package.
Unless otherwise noted, passing a null argument to a constructor
or method in any class or interface in this package will cause a NullPointerException
to be thrown.
Classes
Charset |
A named mapping between sequences of sixteen-bit Unicode code units and sequences of
bytes. |
CharsetDecoder |
An engine that can transform a sequence of bytes in a specific charset into a sequence of
sixteen-bit Unicode characters. |
CharsetEncoder |
An engine that can transform a sequence of sixteen-bit Unicode characters into a sequence of
bytes in a specific charset. |
CoderResult |
A description of the result state of a coder. |
CodingErrorAction |
A typesafe enumeration for coding-error actions. |
StandardCharsets |
Constant definitions for the standard Charsets . |
Exceptions
Errors
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003eThe \u003ccode\u003ejava.nio.charset\u003c/code\u003e package provides classes for translating between bytes and Unicode characters using charsets, decoders, and encoders.\u003c/p\u003e\n"],["\u003cp\u003eA charset is a named mapping between Unicode characters and bytes, with decoders transforming bytes to characters and encoders performing the reverse.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eCharset\u003c/code\u003e class offers methods for creating coders, retrieving charset names, and checking for charset support within the Java virtual machine.\u003c/p\u003e\n"],["\u003cp\u003eWhile the package provides low-level control, many users will interact with charsets through existing classes like \u003ccode\u003eString\u003c/code\u003e, \u003ccode\u003eInputStreamReader\u003c/code\u003e, and \u003ccode\u003eOutputStreamWriter\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eSupport for new charsets can be extended using the \u003ccode\u003eCharsetProvider\u003c/code\u003e interface in the \u003ccode\u003ejava.nio.charset.spi\u003c/code\u003e package.\u003c/p\u003e\n"]]],["Charsets define the mapping between Unicode characters and bytes. Decoders convert bytes to characters, while encoders convert characters to bytes; both operate on buffers. The `Charset` class manages coders and charset names. Most users will indirectly utilize these via `String`, `InputStreamReader`, and `OutputStreamWriter` classes. New charset support is enabled through the `CharsetProvider` interface. Null arguments will trigger a `NullPointerException`. The `CoderResult` and `CodingErrorAction` describe the results and actions regarding errors.\n"],null,["# java.nio.charset\n\nDefines charsets, decoders, and encoders, for translating between bytes and Unicode characters.\n\u003e\n\u003e | Class name | Description |\n\u003e |------------------------------------------------------------------------------------|-----------------------------------------------------------|\n\u003e | [Charset](../../../../reference/java/nio/charset/Charset.html) | A named mapping between characters and bytes |\n\u003e | [CharsetDecoder](../../../../reference/java/nio/charset/CharsetDecoder.html) | Decodes bytes into characters |\n\u003e | [CharsetEncoder](../../../../reference/java/nio/charset/CharsetEncoder.html) | Encodes characters into bytes |\n\u003e | [CoderResult](../../../../reference/java/nio/charset/CoderResult.html) | Describes coder results |\n\u003e | [CodingErrorAction](../../../../reference/java/nio/charset/CodingErrorAction.html) | Describes actions to take when coding errors are detected |\n\nA *charset* is named mapping between sequences of sixteen-bit Unicode\ncharacters and sequences of bytes, in the sense defined in [*RFC 2278*](http://www.ietf.org/rfc/rfc2278.txt). A\n*decoder* is an engine which transforms bytes in a specific charset into\ncharacters, and an *encoder* is an engine which transforms characters into\nbytes. Encoders and decoders operate on byte and character buffers. They are\ncollectively referred to as *coders*.\n\nThe [Charset](../../../../reference/java/nio/charset/Charset.html) class defines methods for creating\ncoders for a given charset and for retrieving the various names associated with\na charset. It also defines static methods for testing whether a particular\ncharset is supported, for locating charset instances by name, and for\nconstructing a map that contains every charset for which support is available\nin the current Java virtual machine.\n\nMost users will not use these classes directly; instead they will use the\nexisting charset-related constructors and methods in the [String](../../../../reference/java/lang/String.html) class, together with the existing [InputStreamReader](../../../../reference/java/io/InputStreamReader.html) and [OutputStreamWriter](../../../../reference/java/io/OutputStreamWriter.html) classes, all\nof whose implementations have been reworked to make use of the charset\nfacilities defined in this package. A small number of changes have been made\nto the [InputStreamReader](../../../../reference/java/io/InputStreamReader.html) and [OutputStreamWriter](../../../../reference/java/io/OutputStreamWriter.html)\nclasses in order to allow explicit charset objects to be specified in the\nconstruction of instances of those classes.\n\nSupport for new charsets can be made available via the interface defined in\nthe [CharsetProvider](../../../../reference/java/nio/charset/spi/CharsetProvider.html) class in the [java.nio.charset.spi](../../../../reference/java/nio/charset/spi/package-summary.html) package.\n\nUnless otherwise noted, passing a null argument to a constructor\nor method in any class or interface in this package will cause a [NullPointerException](../../../../reference/java/lang/NullPointerException.html) to be thrown.\n\n### Classes\n\n|------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------|\n| [Charset](../../../../reference/java/nio/charset/Charset.html) | A named mapping between sequences of sixteen-bit Unicode [code units](../../lang/Character.html#unicode) and sequences of bytes. |\n| [CharsetDecoder](../../../../reference/java/nio/charset/CharsetDecoder.html) | An engine that can transform a sequence of bytes in a specific charset into a sequence of sixteen-bit Unicode characters. |\n| [CharsetEncoder](../../../../reference/java/nio/charset/CharsetEncoder.html) | An engine that can transform a sequence of sixteen-bit Unicode characters into a sequence of bytes in a specific charset. |\n| [CoderResult](../../../../reference/java/nio/charset/CoderResult.html) | A description of the result state of a coder. |\n| [CodingErrorAction](../../../../reference/java/nio/charset/CodingErrorAction.html) | A typesafe enumeration for coding-error actions. |\n| [StandardCharsets](../../../../reference/java/nio/charset/StandardCharsets.html) | Constant definitions for the standard [Charsets](../../../../reference/java/nio/charset/Charset.html). |\n\n### Exceptions\n\n|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [CharacterCodingException](../../../../reference/java/nio/charset/CharacterCodingException.html) | Checked exception thrown when a character encoding or decoding error occurs. |\n| [IllegalCharsetNameException](../../../../reference/java/nio/charset/IllegalCharsetNameException.html) | Unchecked exception thrown when a string that is not a [legal charset name](/j2objc/javadoc/jre/reference/java/nio/charset/Charset#names) is used as such. |\n| [MalformedInputException](../../../../reference/java/nio/charset/MalformedInputException.html) | Checked exception thrown when an input byte sequence is not legal for given charset, or an input character sequence is not a legal sixteen-bit Unicode sequence. |\n| [UnmappableCharacterException](../../../../reference/java/nio/charset/UnmappableCharacterException.html) | Checked exception thrown when an input character (or byte) sequence is valid but cannot be mapped to an output byte (or character) sequence. |\n| [UnsupportedCharsetException](../../../../reference/java/nio/charset/UnsupportedCharsetException.html) | Unchecked exception thrown when no support is available for a requested charset. |\n\n### Errors\n\n|--------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [CoderMalfunctionError](../../../../reference/java/nio/charset/CoderMalfunctionError.html) | Error thrown when the [decodeLoop](../../../../reference/java/nio/charset/CharsetDecoder.html#decodeLoop(java.nio.ByteBuffer,%20java.nio.CharBuffer)) method of a [CharsetDecoder](../../../../reference/java/nio/charset/CharsetDecoder.html), or the [encodeLoop](../../../../reference/java/nio/charset/CharsetEncoder.html#encodeLoop(java.nio.CharBuffer,%20java.nio.ByteBuffer)) method of a [CharsetEncoder](../../../../reference/java/nio/charset/CharsetEncoder.html), throws an unexpected exception. |"]]