NIO Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 12: Line 12:
{{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}}


=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=



Revision as of 22:15, 25 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. 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 File API

NIO 2 File API