Home » Encode and Decode in Java NIO

Encode and Decode in Java NIO

by Online Tutorials Library

Encode and Decode in Java NIO

Using Java NIO API encode and decode operation can be perform from one Charset to another Charset. The two classes’ CharsetEncoder and CharsetDecoder play an important role in encoding and decoding between the ByteBuffer and CharBuffer.

The reactor keeps the track of arrived events and dispatches only when the handler is available. Let’s see the architecture of encoding and decoding operation performed in reactor:

Java Nio tutorial17

CharsetEncoder in Java NIO

CharsetEncoder is used for encoding the Unicode characters into the sequence of bytes. It also returns the CoderResult that provides the information of any error.

CharsetDecoder in Java NIO

CharsetDecoder is used for decoding the sequence of arrays or bytes into the Unicode characters. During decoding from ByteBuffer to CharBuffer, the CoderResult is obtained. The CoderResult

Charset.newEncoder() in Java NIO

In CharsetEncoder, Charset.newEncoder() is used for the creation of an object of Charset and then by the newEncoder() method, we can get an object of CharsetEncoder.

Charset.newDecoder() in Java NIO

In CharsetDecoder, Charset.newDecoder() is used for the creation of an object of Charset and then by the newDecoder() method, we can get an object of CharsetDecoder


Basic Encode and Decode Example

Output:

You may also like