Skip to content

Java NIO components

Java NIO (New Input/Output) introduces a distinct architecture for I/O operations, differing from the traditional stream-based I/O by relying on buffers, channels, and selectors.^[600-developer__java__java-io__bio-nio-aio.md]

Core Components

Buffer

The Buffer is a container object that holds a fixed amount of data; all data in NIO is processed through buffers.^[600-developer__java__java-io__bio-nio-aio.md] A buffer is defined by three core properties that manage its state:

  • Capacity: The maximum amount of data the buffer can hold^[600-developer__java__java-io__bio-nio-aio.md].
  • Limit: The index of the first element that should not be read or written^[600-developer__java__java-io__bio-nio-aio.md].
  • Position: The current index for the next read or write operation^[600-developer__java__java-io__bio-nio-aio.md].

Channel

A Channel represents an open connection to an I/O device, such as a file or a socket.^[600-developer__java__java-io__bio-nio-aio.md] Unlike streams, which are typically one-way (input or output), channels are bidirectional, allowing reads and writes to occur on the same object^[600-developer__java__java-io__bio-nio-aio.md].

Selector

The Selector (often associated with select or epoll mechanisms) is a component that allows a single thread to manage multiple I/O channels.^[600-developer__java__java-io__bio-nio-aio.md] It monitors multiple channels for events (like "connection opened" or "data ready"), enabling non-blocking I/O operations where a thread does not need to be suspended while waiting for data^[600-developer__java__java-io__bio-nio-aio.md].

Sources

^[600-developer__java__java-io__bio-nio-aio.md]