9 Java NIO

The Java NIO (New Input/Output) API defines buffers, which are containers for data, and other structures, such as charsets, channels, and selectable channels. Charsets are mappings between bytes and Unicode characters. Channels represent connections to entities capable of performing I/O operations. Selectable channels are those that can be multiplexed, which means that they can process multiple I/O operations in one channel.

Buffers

They are containers for a fixed amount of data of a specific primitive type. See the java.nio package and Table 9-1.

Table 9-1 Buffer Classes

Buffer Class Description
Buffer Base class for buffer classes.
ByteBuffer Buffer for bytes.
MappedByteBuffer Buffer for bytes that is mapped to a file.
CharBuffer Buffer for the char data type.
DoubleBuffer Buffer for the double data type.
FloatBuffer Buffer for the float data type.
IntBuffer Buffer for the int data type.
LongBuffer Buffer for the long data type.
ShortBuffer Buffer for the short data type.

Charsets

They are named mappings between sequences of 16-bit Unicode characters and sequences of bytes. Support for charsets include decoders and encoders, which translate between bytes and Unicode characters. See the java.nio.charset package and Table 9-2.

Table 9-2 Charset Classes

Charset Class Description
Charset Named mapping between characters and bytes, for example, US-ASCII and UTF-8.
CharsetDecoder Decodes bytes into characters.
CharsetEncoder Encodes characters into bytes.
CoderResult Describes the result state of an decoder or encoder.
CodingErrorAction Describes actions to take when coding errors are detected.

Channels

They represent an open connection to an entity such as a hardware device, a file, a network socket, or a program component that is capable of performing one or more distinct I/O operations, for example reading or writing. See the java.nio.channels package and Table 9-3.

Table 9-3 Channel Interfaces and Classes

Channel Interface or Class Description
Channel Base interface for channel interfaces and classes.
ReadableByteChannel A channel that can read bytes.
ScatteringByteChannel A channel that can read bytes into a sequence of buffers. A scattering read operation reads, in a single invocation, a sequence of bytes into one or more of a given sequence of buffers.
WritableByteChannel A channel that can write bytes.
GatheringByteChannel A channel that can write bytes from a sequence of buffers. A gathering write operation writes, in a single invocation, a sequence of bytes from one or more of a given sequence of buffers.
ByteChannel A channel that can read and write bytes. It unifies ReadableByteChannel and WritableByteChannel.
SeekableByteChannel A byte channel that maintains a current position and allows the position to be changed. A seekable byte channel is connected to an entity, typically a file, that contains a variable-length sequence of bytes that can be read and written.
AsynchronousChannel A channel that supports asynchronous I/O operations.
AsynchronousByteChannel An asynchronous channel that can read and write bytes.
NetworkChannel A channel to a network socket.
MulticastChannel A network channel that supports Internet Protocol (IP) multicasting. IP multicasting is the transmission of IP datagrams to members of a group that is zero or more hosts identified by a single destination address.
FileChannel A channel for reading, writing, mapping, and manipulating a file. It's a SeekableByteChannel that is connected to a file.
SelectableChannel

A channel that can be multiplexed through a Selector.

Multiplexing is the ability to process multiple I/O operations in one channel. A selectable channel can be put into blocking or non-blocking mode. In blocking mode, every I/O operation invoked upon the channel will block until it completes. In non-blocking mode, an I/O operation will never block and may transfer fewer bytes than were requested or possibly no bytes at all.

DatagramChannel

A selectable channel that can send and receive UDP (User Datagram Protocol) packets.

You can create datagram channels with different protocol families:

  • Create channels for Internet Protocol sockets with the INET or INET6 protocol families. These channels support network communication using TCL and UDP. Their addresses are of type InetSocketAddress, which encapsulates an IP address and port number.
  • Create channels for UNIX Domain sockets with the UNIX protocol family. These sockets support local interprocess communication on the same host. Their addresses are of type UnixDomainSocketAddress, which encapsulate a file system path name on the local system.
Pipe.SinkChannel A channel representing the writable end of a pipe. A Pipe is a pair of channels: A writable sink channel and a readable source channel.
Pipe.SourceChannel A channel representing the readable end of a pipe.
ServerSocketChannel

A selectable channel for stream-oriented listening sockets.

Like datagram channels, you can create server socket channels that are for Internet Protocol sockets or Unix Domain sockets.

SocketChannel

A selectable channel for stream-oriented connecting sockets.

Like datagram channels, you can create socket channels that are for Internet Protocol sockets or Unix Domain sockets.

AsynchronousFileChannel An asynchronous channel for reading, writing, and manipulating a file.
AsynchronousSocketChannel An asynchronous channel for stream-oriented connecting sockets.
AsynchronousServerSocketChannel An asynchronous channel for stream-oriented listening sockets.