The New Input/Output (NIO) API provides the java.nio, java.nio.channels, and java.nio.charset packages for buffer management, socket handling, and text encoding and decoding. The java.nio package contains the ByteBuffer and CharBuffer classes that allow you to read and store the bytes and characters . To read a file, you can use the FileChannel class available in the java.nio.channel package. The java.nio.charset package provides the Charset and CharsetEncoder classes that help set the character sets. These classes encode the text using a specific encoding scheme. To decode an encoded text, you can use the CharsetDecoder class the java.nio.charset package.
An encoding scheme is a standard by which you map the coded character set to octets of eight bit sequence. The coded character set is the assignment of numeric values to a set of characters. The encoding scheme defines how a sequence of character encoding is represented as a sequence of bytes. The numeric value of the character set does not match with the encoded bytes. There are several types of encoding schemes, such as UTF-8, UTF-16, ISO-8859-1, UTF-16BE, or UTF-16LE. For example, UTF-8 encoding scheme encodes the coded character code of value less than 0x80 to a sequence of eight bytes.
This chapter explains how to develop an Encoder/Decoder application using the java.nio, java.nio.channels, and java.nio.charset packages.
The Encoder/Decoder application allows an end user to encode the text using a specific encoding scheme or decode the encoded text into a readable form.
The Encoder/Decoder application uses the following files:
EncoderDecoder.java : Creates a user interface that an end user can use to encode or decode a specified text.
EncodingSchemes.java : Creates a dialog box to select the encoding scheme.
Figure 8-1 shows the architecture of the Encoder/Decoder application:
The EncoderDecoder.java file encodes and decodes a specified text. To select an encoding scheme, the EncoderDecoder.java file calls the EncodingSchemes.java file. The EncodingSchemes.java file returns the encoding scheme name to the EncoderDecoder.java file.