Java InstantCode. Developing Applications Using Java NIO

The java.nio.charset package contains various classes and methods for character set conversion, regular expression matching, and for encoding and decoding data. This package provides the charset, encoder, and decoder classes to convert data between bytes and coded characters . The character data is encoded for transmission over a network or for storage in a file.

A character set is a set of characters, such as alphabets from A to Z, a to z, and special characters. The coded character set is an assignment of numeric value to each character in the character set using the standard encoding scheme. The encoding scheme is a process of mapping the coded character set to a sequence of Octets. A collection of 8 bytes is known as Octet.

The charset are the combination of coded character and encoding schemes. The various standard charsets are US-ASCII, ISO-8859-1, UTF-8, UTF-16BE, UTF-16LE, and UTF-16.

Note  

To learn more about java.nio.charset package, see: http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/package-summary.html

Figure 1-3 shows the class diagram for the java.nio.charset package:

Figure 1-3: The java.nio.charset Package

Charset

The Charset class defines methods to retrieve various names associated with a character set. In addition, the Charset class defines methods to create encoders and decoders. The most commonly used methods of the Charset class are:

CharsetDecoder

The CharsetDecoder class defines methods that transform a sequence of bytes into a sequence of 16-bit Unicode characters. The source buffer is a byte buffer and the resultant buffer is a character buffer. The most commonly used methods in the CharsetDecoder class are:

CharsetEncoder

The CharsetEncoder class defines methods that transform a sequence of 16-bit Unicode characters into a sequence of bytes. The source buffer is a character buffer, and the resultant buffer is a byte buffer. The most commonly used methods in the CharsetDecoder class are:

CoderResult

The CoderResult class defines the result state of a coder. A coder consumes bytes or characters from an input buffer, translates them, and writes the resulting characters or bytes to an output buffer. The translation process can be stopped due to various reasons, such as underflow, overflow, malformed -input error, and unmappable-character error. A coder can be an encoder or decoder. The most commonly used methods in the CoderResult class are:

Категории