NIO Concepts: Difference between revisions
Line 24: | Line 24: | ||
A stream-oriented I/O system deals with data one byte at a time: an input stream produces a byte of data and an output stream consumes a byte of data. Stream-oriented API allow data to be easily filtered. They also allow multiple streams to be easily chained. However, moving data this way is rather slow. A block-oriented I/O system deals with data in blocks - each operation produced or consumes a block of data in one step. This could move data faster, but the block-oriented APIs lack the elegance and simplicity of the stream-oriented APIs. | A stream-oriented I/O system deals with data one byte at a time: an input stream produces a byte of data and an output stream consumes a byte of data. Stream-oriented API allow data to be easily filtered. They also allow multiple streams to be easily chained. However, moving data this way is rather slow. A block-oriented I/O system deals with data in blocks - each operation produced or consumes a block of data in one step. This could move data faster, but the block-oriented APIs lack the elegance and simplicity of the stream-oriented APIs. | ||
=Multiplexed Non- | =Multiplexed Non-Blocking I/O Facility= | ||
[[#Selector|Selectors]], [[#Selection_Key|selection keys]] and [[#Selectable_Channel|selectable channels]] work together to provide a multiplexed, non-blocking I/O facility, whose main advantage is that is much more scalable than thread-oriented blocking I/O. | [[#Selector|Selectors]], [[#Selection_Key|selection keys]] and [[#Selectable_Channel|selectable channels]] work together to provide a multiplexed, non-blocking I/O facility, whose main advantage is that is much more scalable than thread-oriented blocking I/O. |
Revision as of 21:59, 2 July 2018
Internal
Overview
NIO (Non-blocking IO) was introduced in Java 4 and enhanced with new File operations as NIO.2 in Java 7.
The major improvement introduced by NIO was to allow non-blocking, block-oriented I/O operations from Java programs.
Until NIO, all that was available for I/O were Streams (java.io.*), and all operations with Streams are blocking: a thread waits until there is data to read from the Stream instance or until it can write to the Stream instance, so simultaneously handling multiple sources of data (concurrent network connections, for example) required multiple threads that would usually spend most of their time blocked waiting on I/O events. This is the I/O and threading model Tomcat is built on. NIO (java.nio.*) offers access to underlying O/S non-blocking I/O facilities and offers a selectors mechanism through which a single thread could be notified and process I/O events arriving from multiple sources.
Also, because the underlying operating system I/O facilities are block-oriented for efficiency - hardware usually transfers data in blocks rather than byte by byte - NIO exposes block-level access via Channels and Buffers. Data can be read and written in blocks via Buffers into and from Channels. Streams only offer sequential access - once a byte has been read, it is gone from the Stream and it is the application's responsibility to cache it locally if it needs it later. With Buffer, the caching is already done, efficiently, by the O/S.
Channels are also sources of asynchronous I/O events that are routed by selectors into the application. The data transfer between Channels and Buffers is done transparently, without requiring application threads to move bytes around. An application thread is notified once the transfer had completed via an I/O event. The NIO API does not do anything that the Stream API can't do - essentially reading and writing data from/to I/O devices - but it does it faster and using less threads.
Block-access and non-blocking I/O read and write allow Java applications to implement high-speed I/O without having to write native code that would access O/S specific facilities.
Somewhat unrelated, NIO offers new features such as file locking and characters sets, and NIO.2 comes with a new file system access API.
Stream-Oriented vs. Block-Oriented I/O Operations
A stream-oriented I/O system deals with data one byte at a time: an input stream produces a byte of data and an output stream consumes a byte of data. Stream-oriented API allow data to be easily filtered. They also allow multiple streams to be easily chained. However, moving data this way is rather slow. A block-oriented I/O system deals with data in blocks - each operation produced or consumes a block of data in one step. This could move data faster, but the block-oriented APIs lack the elegance and simplicity of the stream-oriented APIs.
Multiplexed Non-Blocking I/O Facility
Selectors, selection keys and selectable channels work together to provide a multiplexed, non-blocking I/O facility, whose main advantage is that is much more scalable than thread-oriented blocking I/O.
Selector
Selection Key
Selectable Channel
Channel
A Channel represents 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 I/O operations. The Channel is essentially a source of I/O events. The application does not read or write data from/to the Channel directly, it does so via Buffers, after being notified of data availability via a selector. For more details se Channel/Buffer Interaction below.
For more details about Channels see:
Buffer
java.nio.Buffer is a container for a fixed amount of data. More details on Buffers is available in:
Channel/Buffer Interaction
Reading from a Channel
To read from a channel use:
ReadableByteChannel channel = ...; ByteBuffer buffer = ...; channel.read(buffer);
The invocation of the read() method initiates an attempt to transfer data from the channel into the buffer. The read operation might not fill the buffer, and it fact might not read any bytes at all - if none are available on the channel. In the best case, it fills all the space available in the buffer. The result is the number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream. The buffer's internal accounting variables are modified correspondingly.
If the channel is in blocking mode, the method will block until at least one byte is read.
Also see https://docs.oracle.com/javase/8/docs/api/java/nio/channels/ReadableByteChannel.html