| |
java.nio.channels |
Defines channels, which represent connections to entities that are capable of
performing I/O operations, such as files and sockets; defines selectors, for
multiplexed, non-blocking I/O operations.
Channels | Description |
{@link java.nio.channels.Channel} |
A nexus for I/O operations |
{@link java.nio.channels.ReadableByteChannel} |
Can read into a buffer |
{@link java.nio.channels.ScatteringByteChannel} |
Can read into a sequence of buffers |
{@link java.nio.channels.WritableByteChannel} |
Can write from a buffer |
{@link java.nio.channels.GatheringByteChannel} |
Can write from a sequence of buffers |
{@link java.nio.channels.ByteChannel} |
Can read/write to/from a buffer |
{@link java.nio.channels.Channels} |
Utility methods for channel/stream interoperation |
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 one or more distinct I/O operations, for example reading
or writing. As specified in the {@link java.nio.channels.Channel} interface,
channels are either open or closed, and they are both asynchronously
closeable and interruptible.
The {@link java.nio.channels.Channel} interface is extended by several
other interfaces, each of which specifies a new I/O operation.
The {@link java.nio.channels.ReadableByteChannel} interface specifies a
{@link java.nio.channels.ReadableByteChannel#read read} method that reads bytes
from the channel into a buffer; similarly, the {@link
java.nio.channels.WritableByteChannel} interface specifies a {@link
java.nio.channels.WritableByteChannel#write write} method that writes bytes
from a buffer to the channel. The {@link java.nio.channels.ByteChannel}
interface unifies these two interfaces for the common case of channels that can
both read and write bytes.
The {@link java.nio.channels.ScatteringByteChannel} and {@link
java.nio.channels.GatheringByteChannel} interfaces extend the {@link
java.nio.channels.ReadableByteChannel} and {@link
java.nio.channels.WritableByteChannel} interfaces, respectively, adding {@link
java.nio.channels.ScatteringByteChannel#read read} and {@link
java.nio.channels.GatheringByteChannel#write write} methods that take a
sequence of buffers rather than a single buffer.
The {@link java.nio.channels.Channels} utility class defines static methods
that support the interoperation of the stream classes of the {@link
java.io} package with the channel classes of this package. An appropriate
channel can be constructed from an {@link java.io.InputStream} or an {@link
java.io.OutputStream}, and conversely an {@link java.io.InputStream} or an
{@link java.io.OutputStream} can be constructed from a channel. A {@link
java.io.Reader} can be constructed that uses a given charset to decode bytes
from a given readable byte channel, and conversely a {@link java.io.Writer} can
be constructed that uses a given charset to encode characters into bytes and
write them to a given writable byte channel.
File channels | Description |
{@link java.nio.channels.FileChannel} |
Reads, writes, maps, and manipulates files |
{@link java.nio.channels.FileLock} |
A lock on a (region of a) file |
{@link java.nio.MappedByteBuffer} |
A direct byte buffer mapped to a region of a file |
The {@link java.nio.channels.FileChannel} class supports the usual
operations of reading bytes from, and writing bytes to, a channel connected to
a file, as well as those of querying and modifying the current file position
and truncating the file to a specific size. It defines methods for acquiring
locks on the whole file or on a specific region of a file; these methods return
instances of the {@link java.nio.channels.FileLock} class. Finally, it defines
methods for forcing updates to the file to be written to the storage device that
contains it, for efficiently transferring bytes between the file and other
channels, and for mapping a region of the file directly into memory. This last
operation creates an instance of the {@link java.nio.MappedByteBuffer}
class, which extends the {@link java.nio.ByteBuffer} class with several
file-related operations.
A getChannel method has been added to each of the {@link
java.io.FileInputStream#getChannel FileInputStream}, {@link
java.io.FileOutputStream#getChannel FileOutputStream}, and {@link
java.io.RandomAccessFile#getChannel RandomAccessFile} classes of the {@link
java.io java.io} package. Invoking this method upon an instance of one of
these classes will return a file channel connected to the underlying file.
Multiplexed, non-blocking I/O | Description |
{@link java.nio.channels.SelectableChannel} |
A channel that can be multiplexed |
{@link java.nio.channels.DatagramChannel} |
A channel for a {@link java.net.DatagramSocket java.net.DatagramSocket} |
{@link java.nio.channels.Pipe.SinkChannel} |
The write end of a pipe |
{@link java.nio.channels.Pipe.SourceChannel} |
The read end of a pipe |
{@link java.nio.channels.ServerSocketChannel} |
A channel for a {@link java.net.ServerSocket java.net.ServerSocket} |
{@link java.nio.channels.SocketChannel} |
A channel for a {@link java.net.Socket java.net.Socket} |
{@link java.nio.channels.Selector} |
A multiplexor of selectable channels |
{@link java.nio.channels.SelectionKey} |
A token representing the registration of a channel
with a selector |
{@link java.nio.channels.Pipe} |
Two channels that form a unidirectional pipe |
Multiplexed, non-blocking I/O, which is much more scalable than
thread-oriented, blocking I/O, is provided by selectors, selectable
channels, and selection keys.
A selector is a multiplexor of selectable channels, which in turn are
a special type of channel that can be put into non-blocking mode. To perform
multiplexed I/O operations, one or more selectable channels are first created,
put into non-blocking mode, and {@link
java.nio.channels.SelectableChannel#register registered}
with a selector. Registering a channel specifies the set of I/O operations
that will be tested for readiness by the selector, and returns a selection key that represents the
registration.
Once some channels have been registered with a selector, a selection operation can be performed in
order to discover which channels, if any, have become ready to perform one or
more of the operations in which interest was previously declared. If a channel
is ready then the key returned when it was registered will be added to the
selector's selected-key set. The key set, and the keys within it, can
be examined in order to determine the operations for which each channel is
ready. From each key one can retrieve the corresponding channel in order to
perform whatever I/O operations are required.
That a selection key indicates that its channel is ready for some operation
is a hint, but not a guarantee, that such an operation can be performed by a
thread without causing the thread to block. It is imperative that code that
performs multiplexed I/O be written so as to ignore these hints when they prove
to be incorrect.
This package defines selectable-channel classes corresponding to the {@link
java.net.DatagramSocket}, {@link java.net.ServerSocket}, and {@link
java.net.Socket} classes defined in the {@link java.net} package.
Minor changes to these classes have been made in order to support sockets that
are associated with channels. This package also defines a simple class that
implements unidirectional pipes. In all cases, a new selectable channel is
created by invoking the static open method of the corresponding class.
If a channel needs an associated socket then a socket will be created as a side
effect of this operation.
The implementation of selectors, selectable channels, and selection keys
can be replaced by "plugging in" an alternative definition or instance of the
{@link java.nio.channels.spi.SelectorProvider} class defined in the {@link
java.nio.channels.spi} package. It is not expected that many developers
will actually make use of this facility; it is provided primarily so that
sophisticated users can take advantage of operating-system-specific
I/O-multiplexing mechanisms when very high performance is required.
Much of the bookkeeping and synchronization required to implement the
multiplexed-I/O abstractions is performed by the {@link
java.nio.channels.spi.AbstractInterruptibleChannel}, {@link
java.nio.channels.spi.AbstractSelectableChannel}, {@link
java.nio.channels.spi.AbstractSelectionKey}, and {@link
java.nio.channels.spi.AbstractSelector} classes in the {@link
java.nio.channels.spi} package. When defining a custom selector provider,
only the {@link java.nio.channels.spi.AbstractSelector} and {@link
java.nio.channels.spi.AbstractSelectionKey} classes should be subclassed
directly; custom channel classes should extend the appropriate {@link
java.nio.channels.SelectableChannel} subclasses defined in this package.
Unless otherwise noted, passing a null argument to a constructor
or method in any class or interface in this package will cause a {@link
java.lang.NullPointerException NullPointerException} to be thrown.
@since 1.4
@version 1.10, 07/05/05
@author Mark Reinhold
@author JSR-51 Expert Group
| Java Source File Name | Type | Comment | ByteChannel.java | Interface | A channel that can read and write bytes. | Channel.java | Interface | A nexus for I/O operations.
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 one or more distinct I/O operations, for example reading or
writing.
A channel is either open or closed. | Channels.java | Class | Utility methods for channels and streams.
This class defines static methods that support the interoperation of the
stream classes of the
java.io package with the channel
classes of this package. | DatagramChannel.java | Class | A selectable channel for datagram-oriented sockets.
Datagram channels are not a complete abstraction of network datagram
sockets. | FileChannel.java | Class | A channel for reading, writing, mapping, and manipulating a file.
A file channel has a current position within its file which can
be both
FileChannel.position() queried and
FileChannel.position(long) modified . | FileLock.java | Class | A token representing a lock on a region of a file.
A file-lock object is created each time a lock is acquired on a file via
one of the
FileChannel.lock(longlongboolean) lock or
FileChannel.tryLock(longlongboolean) tryLock methods of the
FileChannel class.
A file-lock object is initially valid. | GatheringByteChannel.java | Interface | A channel that can write bytes from a sequence of buffers.
A gathering write operation writes, in a single invocation, a
sequence of bytes from one or more of a given sequence of buffers.
Gathering writes are often useful when implementing network protocols or
file formats that, for example, group data into segments consisting of one
or more fixed-length headers followed by a variable-length body. | InterruptibleChannel.java | Interface | A channel that can be asynchronously closed and interrupted.
A channel that implements this interface is asynchronously
closeable: If a thread is blocked in an I/O operation on an
interruptible channel then another thread may invoke the channel's
InterruptibleChannel.close close method. | Pipe.java | Class | A pair of channels that implements a unidirectional pipe.
A pipe consists of a pair of channels: A writable
Pipe.SinkChannel sink channel and a readable
Pipe.SourceChannel source channel. | ReadableByteChannel.java | Interface | A channel that can read bytes.
Only one read operation upon a readable channel may be in progress at
any given time. | ScatteringByteChannel.java | Interface | A channel that can read bytes into a sequence of buffers.
A scattering read operation reads, in a single invocation, a
sequence of bytes into one or more of a given sequence of buffers.
Scattering reads are often useful when implementing network protocols or
file formats that, for example, group data into segments consisting of one
or more fixed-length headers followed by a variable-length body. | SelectableChannel.java | Class | A channel that can be multiplexed via a
Selector .
In order to be used with a selector, an instance of this class must
first be registered via the
SelectableChannel.register(Selector,int,Object)register method. | SelectionKey.java | Class | A token representing the registration of a
SelectableChannel with a
Selector .
A selection key is created each time a channel is registered with a
selector. | Selector.java | Class | A multiplexor of
SelectableChannel objects.
A selector may be created by invoking the
Selector.open open method of
this class, which will use the system's default
java.nio.channels.spi.SelectorProvider selector provider to
create a new selector. | ServerSocketChannel.java | Class | A selectable channel for stream-oriented listening sockets.
Server-socket channels are not a complete abstraction of listening
network sockets. | SocketChannel.java | Class | A selectable channel for stream-oriented connecting sockets.
Socket channels are not a complete abstraction of connecting network
sockets. | WritableByteChannel.java | Interface | A channel that can write bytes.
Only one write operation upon a writable channel may be in progress at
any given time. |
|