9 Java NIO

The Java NIO (New Input/Output) API defines buffers, which are containers for data, and other structures and mechanisms to support buffers.

The java.nio package contains classes that support the following:

  • Buffers: They are containers for a fixed amount of data of a specific primitive type. See the java.nio package.
  • 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 Uncode characters. See the java.nio.charset package.
  • 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.
  • Multiplexed, non-blocking I/O: Multiplexing is the ability to process multiple I/O operations in one channel. Selectable channels support this. 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. See the SelectableChannel class.