NIO Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 8: Line 8:
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_I.2FO|non-blocking]], block-oriented I/O operations from Java programs. Somewhat unrelated, NIO offers new features such as [[#File_Locking|file locking]] and [[#Character_Sets|characters sets]], and NIO.2 comes with a new [[#NIO_2_File_API|file system access API]].
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_I.2FO|non-blocking]], block-oriented I/O operations from Java programs. Somewhat unrelated, NIO offers new features such as [[#File_Locking|file locking]] and [[#Character_Sets|characters sets]], and NIO.2 comes with a new [[#NIO_2_File_API|file system access API]].


=Non-Blocking I/O=
=<span id='Multiplexed_Non-Blocking_I.2FO_Facility'></span><span id='Selector'></span><span id='Selector_Key'></span><span id='Selectable_Channel'></span><span id='Stream-Oriented_vs._Block-Oriented_I.2FO_Operations'></span><span id='Channel'></span><span id='Buffer'></span><span id='Channel.2FBuffer_Interaction'></span>Non-Blocking I/O=


{{Internal|Java Non-Blocking I/O Concepts#Overview|Java Non-Blocking I/O Concepts}}
{{Internal|Java Non-Blocking I/O Concepts#Overview|Java Non-Blocking I/O Concepts}}


=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.
=<span id='Selector'></span><span id='Selector_Key'></span><span id='Selectable_Channel'></span>Multiplexed Non-Blocking I/O Facility=
{{External|[https://docs.oracle.com/javase/10/docs/api/java/nio/channels/package-summary.html java.nio.channels package summary]}}
[[#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.
The facility works as follows: a '''Selector instance''' is created using the [https://docs.oracle.com/javase/10/docs/api/java/nio/channels/Selector.html#open() Selector.open()] static method. The Selector instance is then used to register '''selectable channels''', so the Selector instance acts as a multiplexor of selectable channels. A selectable channel is a special type of [[#Channel|channel]] that can be put into non-blocking mode, and which has to be put in blocking mode if it is to be multiplexed under a selector. 
The set of '''channel I/O operations to be tested for readiness by the selector''', also referred to as the "interest set",  are also specified during the registration procedure.
The procedure returns a '''selection key''' that represents that registration.
After the registration, a '''selection operation''' can be performed to discover which channels, if any, have become ready to perform one or more of the operations in which interest was previously declared. The underlying operating system is queried for an update on registered channels' readiness. If a channel is ready, the key returned when it was registered will be added to the selector's '''selected-key set'''. The set is returned by Selector.selectedKeys(). The keys of the selected-key set can be examined to determine the operations for which each channel is ready. The key also gives access to the channel instance, which then can be used to perform the I/O operation.
<tt>java.nio.channels</tt> provides selectable channel classes corresponding to [https://docs.oracle.com/javase/10/docs/api/java/net/DatagramSocket.html DatagramSocket], [https://docs.oracle.com/javase/10/docs/api/java/net/ServerSocket.html ServerSocket] and [https://docs.oracle.com/javase/10/docs/api/java/net/Socket.html Socket]. If the channel needs an associated socket, the socket will be created as a side effect on this operation.
Channels cannot be deregistered directly, instead, the key representing their registration must be cancelled.
=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 [[#Buffer|Buffers]], after being notified of data availability via a [[#Selector|selector]]. For more details se [[#Channel.2FBuffer_Interaction|Channel/Buffer Interaction]] below.
For more details about Channels see:
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[NIO Channels]]
</blockquote>
=Buffer=
<tt>java.nio.Buffer</tt> is a container for a fixed amount of data. More details on Buffers is available in:
{{Internal|NIO Buffer Mechanics|NIO Buffer Mechanics}}
=Channel/Buffer Interaction=
==Reading from a Channel==
To read from a channel use:
<pre>
ReadableByteChannel channel = ...;
ByteBuffer buffer = ...;
channel.read(buffer);
</pre>
The invocation of the <tt>read()</tt> 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
=File Locking=
=File Locking=


Line 79: Line 24:
</blockquote>
</blockquote>


=NIO 2 File API=
=NIO 2=
 
{{Internal|Java NIO 2|Java NIO 2}}
<blockquote style="background-color: #f9f9f9; border: solid thin lightgrey;">
:[[NIO 2 File API]]
</blockquote>

Latest revision as of 22:03, 18 June 2020

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. Somewhat unrelated, NIO offers new features such as file locking and characters sets, and NIO.2 comes with a new file system access API.

Non-Blocking I/O

Java Non-Blocking I/O Concepts

File Locking

NIO File Locking

Character Sets

NIO Character Sets

NIO 2

Java NIO 2