Java Reference
In-Depth Information
CharsetDecoder
•
CoderResult
•
CodingErrorAction
You will still need to use the
Charset
class to represent a character set. A
CharsetEncoder
object lets you encode
characters into a sequence of bytes using its
encode()
method. A sequence of bytes is decoded using the
decode()
method of a
CharsetDecoder
object. The
newEncoder()
method of a
Charset
object returns an instance of the
CharsetEncoder
class whereas its
newDecoder()
method returns an instance of the
CharsetDecoder
class.
•
// Get encoder and decoder objects from a Charset object
Charset cs = Charset.forName("UTF-8");
CharsetEncoder encoder = cs.newEncoder();
CharsetDecoder decoder = cs.newDecoder();
Two buffers, an input buffer and an output buffer, are needed for encoding and decoding. A character buffer
supplies the input characters to the encoding process and receives the decoded characters from the decoding process.
The encoding process writes the encoded result into a byte buffer and the decoding process reads its input from a byte
buffer. The following snippet of code illustrates the few steps in using an encoder and a decoder:
// Encode characters in inputChars buffer.
// outputBytes buffer will receive encoded bytes.
CharBuffer inputChars = get input characters to be encoded;
ByteBuffer outputBytes = get the output buffer for the encoded data;
boolean eoi = true; // Indicates the end of the input
CoderResult result = encoder.encode(inputChars, outputBytes, eoi);
// Decode bytes in inputBytes buffer.
// outputChars buffer will receive the decoded characters.
ByteBuffer inputBytes = get the input bytes to be decoded;
CharBuffer outputChars = get the output buffer for the decoded characters;
boolean eoi = true; // Indicates the end of the input
CoderResult result = encoder.decode(inputBytes, outputChars, eoi);
Consider a situation of encoding 16 characters stored in a character buffer using a 4-byte buffer. The encoding
process cannot encode all characters in one call to the
encode()
method. There must be a way to read all encoded
output repeatedly. You can apply the same argument for the decoding process. You can pass an input to the encoding/
decoding process and receive an output from them in chunks. The encoder's
encode()
method and decoder's
decode()
method return an object of the
CoderResult
class, which contains the status of the encoding/decoding
process. There are two important results that this object can indicate: an
Underflow
or an
Overflow
.
•
Underflow:
It indicates that the process needs more input. You can test for this condition by
using the
isUnderflow()
method of the
CoderResult
object. You can also test this condition
by comparing the return value of the
encode()
or
decode()
method with
CoderResult.UNDERFLOW
object as follows:
CoderResult result = encoder.encode(input, output, eoi);
if (result == CoderResult.UNDERFLOW) {
// Supply some more input
}