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.
Java NIO Examples
The following code examples demonstrate the Java NIO API:
- Grep NIO Example
- Checksum NIO Example
- Time Query NIO Example
- Time Server NIO Example
- Non-Blocking Time Server NIO Example
- Internet Protocol and UNIX Domain Sockets NIO Example
- File NIO examples:
Table 9-1 Buffer Classes
|Buffer||Base class for buffer classes.|
|ByteBuffer||Buffer for bytes.|
|MappedByteBuffer||Buffer for bytes that is mapped to a file.|
|CharBuffer||Buffer for the
|DoubleBuffer||Buffer for the
|FloatBuffer||Buffer for the
|IntBuffer||Buffer for the
|LongBuffer||Buffer for the
|ShortBuffer||Buffer for the
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||Named mapping between characters and bytes, for example,
|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.|
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.|
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.
A selectable channel that can send and receive UDP (User Datagram Protocol) packets.
You can create datagram channels with different protocol families:
|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.|
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.
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.|