java.nio.channels - JRE Emulation | JRE Emulation
package

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.
ChannelsDescription
Channel A nexus for I/O operations
  ReadableByteChannel Can read into a buffer
    ScatteringByteChannel   Can read into a sequence of buffers
  WritableByteChannel Can write from a buffer
    GatheringByteChannel Can write from a sequence of buffers
  ByteChannel Can read/write to/from a buffer
    SeekableByteChannel A ByteChannel connected to an entity that contains a variable-length sequence of bytes
  ERROR(/java.nio.channels.AsynchronousChannel) Supports asynchronous I/O operations.
    ERROR(/java.nio.channels.AsynchronousByteChannel) Can read and write bytes asynchronously
  NetworkChannel A channel to a network socket
    ERROR(/java.nio.channels.MulticastChannel) Can join Internet Protocol (IP) multicast groups
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 Channel interface, channels are either open or closed, and they are both asynchronously closeable and interruptible.

The Channel interface is extended by several other interfaces.

The ReadableByteChannel interface specifies a read method that reads bytes from the channel into a buffer; similarly, the WritableByteChannel interface specifies a write method that writes bytes from a buffer to the channel. The ByteChannel interface unifies these two interfaces for the common case of channels that can both read and write bytes. The SeekableByteChannel interface extends the ByteChannel interface with methods to query and modify the channel's current position, and its size.

The ScatteringByteChannel and GatheringByteChannel interfaces extend the ReadableByteChannel and WritableByteChannel interfaces, respectively, adding read and write methods that take a sequence of buffers rather than a single buffer.

The NetworkChannel interface specifies methods to ERROR(bind/java.nio.channels.NetworkChannel#bind bind) the channel's socket, obtain the address to which the socket is bound, and methods to ERROR(get/java.nio.channels.NetworkChannel#getOption get) and ERROR(set/java.nio.channels.NetworkChannel#setOption set) socket options. The ERROR(/java.nio.channels.MulticastChannel) interface specifies methods to join Internet Protocol (IP) multicast groups.

The Channels utility class defines static methods that support the interoperation of the stream classes of the java.io package with the channel classes of this package. An appropriate channel can be constructed from an InputStream or an OutputStream, and conversely an InputStream or an OutputStream can be constructed from a channel. A Reader can be constructed that uses a given charset to decode bytes from a given readable byte channel, and conversely a Writer can be constructed that uses a given charset to encode characters into bytes and write them to a given writable byte channel.

File channelsDescription
FileChannel Reads, writes, maps, and manipulates files
FileLock A lock on a (region of a) file
MappedByteBuffer   A direct byte buffer mapped to a region of a file

The 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 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.

A FileChannel is created by invoking one of its static ERROR(open/java.nio.channels.FileChannel#open open) methods, or by invoking the getChannel method of a FileInputStream, FileOutputStream, or RandomAccessFile to return a file channel connected to the same underlying file as the java.io class.

Multiplexed, non-blocking I/O

Description

SelectableChannel A channel that can be multiplexed
  DatagramChannel A channel to a datagram-oriented socket
  Pipe.SinkChannel The write end of a pipe
  Pipe.SourceChannel The read end of a pipe
  ServerSocketChannel   A channel to a stream-oriented listening socket
  SocketChannel A channel for a stream-oriented connecting socket
Selector A multiplexor of selectable channels
SelectionKey A token representing the registration
of a channel with a selector
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 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 DatagramSocket, ServerSocket, and Socket classes defined in the 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 SelectorProvider class defined in the 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 AbstractInterruptibleChannel, AbstractSelectableChannel, AbstractSelectionKey, and AbstractSelector classes in the java.nio.channels.spi package. When defining a custom selector provider, only the AbstractSelector and AbstractSelectionKey classes should be subclassed directly; custom channel classes should extend the appropriate SelectableChannel subclasses defined in this package.

Asynchronous I/ODescription
ERROR(/java.nio.channels.AsynchronousFileChannel) An asynchronous channel for reading, writing, and manipulating a file
ERROR(/java.nio.channels.AsynchronousSocketChannel) An asynchronous channel to a stream-oriented connecting socket
ERROR(/java.nio.channels.AsynchronousServerSocketChannel)   An asynchronous channel to a stream-oriented listening socket
ERROR(/java.nio.channels.CompletionHandler) A handler for consuming the result of an asynchronous operation
ERROR(/java.nio.channels.AsynchronousChannelGroup) A grouping of asynchronous channels for the purpose of resource sharing

ERROR(Asynchronous channels/java.nio.channels.AsynchronousChannel Asynchronous channels) are a special type of channel capable of asynchronous I/O operations. Asynchronous channels are non-blocking and define methods to initiate asynchronous operations, returning a Future representing the pending result of each operation. The Future can be used to poll or wait for the result of the operation. Asynchronous I/O operations can also specify a ERROR(/java.nio.channels.CompletionHandler) to invoke when the operation completes. A completion handler is user provided code that is executed to consume the result of I/O operation.

This package defines asynchronous-channel classes that are connected to a stream-oriented connecting or listening socket, or a datagram-oriented socket. It also defines the ERROR(/java.nio.channels.AsynchronousFileChannel) class for asynchronous reading, writing, and manipulating a file. As with the FileChannel it supports operations to truncate the file to a specific size, force updates to the file to be written to the storage device, or acquire locks on the whole file or on a specific region of the file. Unlike the FileChannel it does not define methods for mapping a region of the file directly into memory. Where memory mapped I/O is required, then a FileChannel can be used.

Asynchronous channels are bound to an asynchronous channel group for the purpose of resource sharing. A group has an associated ExecutorService to which tasks are submitted to handle I/O events and dispatch to completion handlers that consume the result of asynchronous operations performed on channels in the group. The group can optionally be specified when creating the channel or the channel can be bound to a default group. Sophisticated users may wish to create their own asynchronous channel groups or configure the ExecutorService that will be used for the default group.

As with selectors, the implementation of asynchronous channels can be replaced by "plugging in" an alternative definition or instance of the ERROR(/java.nio.channels.spi.AsynchronousChannelProvider) class defined in the 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 asynchronous I/O mechanisms when very high performance is required.


Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown.

Interfaces

ByteChannel A ByteChannel is both readable and writable. 
Channel A channel is a conduit to I/O services covering such items as files, sockets, hardware devices, I/O ports or some software component. 
GatheringByteChannel The interface for channels that can write a set of buffers in a single operation. 
InterruptibleChannel Channels that implement this interface can be asynchronously closed and interrupted. 
ReadableByteChannel A ReadableByteChannel is a type of Channel that can read bytes. 
ScatteringByteChannel The interface for channels that can read data into a set of buffers in a single operation. 
WritableByteChannel A WritableByteChannel is a type of Channel that can write bytes. 

Classes

Channels This class provides several utilities to get I/O streams from channels. 
DatagramChannel A DatagramChannel is a selectable channel that represents a partial abstraction of a datagram socket. 
FileChannel An abstract channel type for interaction with a platform file. 
FileChannel.MapMode MapMode defines file mapping mode constants. 
FileLock A FileLock represents a locked region of a file. 
Pipe A pipe contains two channels, forming a unidirectional pipe. 
Pipe.SinkChannel Writable sink channel used to write to a pipe. 
Pipe.SourceChannel Readable source channel used to read from a pipe. 
SelectableChannel A channel that can be used with a Selector
SelectionKey A SelectionKey represents the relationship between a channel and a selector for which the channel is registered. 
Selector A controller for the selection of SelectableChannel objects. 
ServerSocketChannel A ServerSocketChannel is a partial abstraction of a selectable, stream-oriented listening socket. 
SocketChannel A SocketChannel is a selectable channel that provides a partial abstraction of stream connecting socket. 

Exceptions

AcceptPendingException Unchecked exception thrown when an attempt is made to initiate an accept operation on a channel and a previous accept operation has not completed. 
AlreadyBoundException Unchecked exception thrown when an attempt is made to bind the socket a network oriented channel that is already bound. 
AlreadyConnectedException Unchecked exception thrown when an attempt is made to connect a SocketChannel that is already connected. 
AsynchronousCloseException Checked exception received by a thread when another thread closes the channel or the part of the channel upon which it is blocked in an I/O operation. 
CancelledKeyException Unchecked exception thrown when an attempt is made to use a selection key that is no longer valid. 
ClosedByInterruptException Checked exception received by a thread when another thread interrupts it while it is blocked in an I/O operation upon a channel. 
ClosedChannelException Checked exception thrown when an attempt is made to invoke or complete an I/O operation upon channel that is closed, or at least closed to that operation. 
ClosedSelectorException Unchecked exception thrown when an attempt is made to invoke an I/O operation upon a closed selector. 
ConnectionPendingException Unchecked exception thrown when an attempt is made to connect a SocketChannel for which a non-blocking connection operation is already in progress. 
FileLockInterruptionException Checked exception received by a thread when another thread interrupts it while it is waiting to acquire a file lock. 
IllegalBlockingModeException Unchecked exception thrown when a blocking-mode-specific operation is invoked upon a channel in the incorrect blocking mode. 
IllegalChannelGroupException Unchecked exception thrown when an attempt is made to open a channel in a group that was not created by the same provider. 
IllegalSelectorException Unchecked exception thrown when an attempt is made to register a channel with a selector that was not created by the provider that created the channel. 
InterruptedByTimeoutException Checked exception received by a thread when a timeout elapses before an asynchronous operation completes. 
NoConnectionPendingException Unchecked exception thrown when the finishConnect method of a SocketChannel is invoked without first successfully invoking its connect method. 
NonReadableChannelException Unchecked exception thrown when an attempt is made to read from a channel that was not originally opened for reading. 
NonWritableChannelException Unchecked exception thrown when an attempt is made to write to a channel that was not originally opened for writing. 
NotYetBoundException Unchecked exception thrown when an attempt is made to invoke an I/O operation upon a server socket channel that is not yet bound. 
NotYetConnectedException Unchecked exception thrown when an attempt is made to invoke an I/O operation upon a socket channel that is not yet connected. 
OverlappingFileLockException Unchecked exception thrown when an attempt is made to acquire a lock on a region of a file that overlaps a region already locked by the same Java virtual machine, or when another thread is already waiting to lock an overlapping region of the same file. 
ReadPendingException Unchecked exception thrown when an attempt is made to read from an asynchronous socket channel and a previous read has not completed. 
ShutdownChannelGroupException Unchecked exception thrown when an attempt is made to construct a channel in a group that is shutdown or the completion handler for an I/O operation cannot be invoked because the channel group has terminated. 
UnresolvedAddressException Unchecked exception thrown when an attempt is made to invoke a network operation upon an unresolved socket address. 
UnsupportedAddressTypeException Unchecked exception thrown when an attempt is made to bind or connect to a socket address of a type that is not supported. 
WritePendingException Unchecked exception thrown when an attempt is made to write to an asynchronous socket channel and a previous write has not completed.