Channels | Description |
---|---|
java. |
A nexus for I/O operations |
java. |
Can read into a buffer |
java. |
Can read into a sequence of buffers |
java. |
Can write from a buffer |
java. |
Can write from a sequence of buffers |
java. |
Can read/write to/from a buffer |
java. |
A ByteChannel connected to an entity that contains a variable-length
sequence of bytes |
java. |
Supports asynchronous I/O operations. |
java. |
Can read and write bytes asynchronously |
java. |
A channel to a network socket |
java. |
Can join Internet Protocol (IP) multicast groups |
java. |
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 java.
interface,
channels are either open or closed, and they are both asynchronously
closeable and interruptible.
The java.
interface is extended by several
other interfaces.
The java.
interface specifies a
read
method that reads bytes
from the channel into a buffer; similarly, the java.
interface specifies a write
method that writes bytes
from a buffer to the channel. The java.
interface unifies these two interfaces for the common case of channels that can
both read and write bytes. The java.
interface extends the ByteChannel
interface with methods to query
and modify
the channel's
current position, and its size
.
The java.
and java.
interfaces extend the java.
and java.
interfaces, respectively, adding read
and write
methods that take a
sequence of buffers rather than a single buffer.
The java.
interface specifies methods
to bind
the channel's socket,
obtain the address to which the socket is bound, and methods to get
and set
socket options. The java.
interface specifies methods to join
Internet Protocol (IP) multicast groups.
The java.
utility class defines static methods
that support the interoperation of the stream classes of the java.
package with the channel classes of this package. An appropriate
channel can be constructed from an java.
or an java.
, and conversely an java.
or an
java.
can be constructed from a channel. A java.
can be constructed that uses a given charset to decode bytes
from a given readable byte channel, and conversely a java.
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 |
---|---|
java. |
Reads, writes, maps, and manipulates files |
java. |
A lock on a (region of a) file |
java. |
A direct byte buffer mapped to a region of a file |
The java.
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 java.
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 open
methods, or by invoking the getChannel
method of a java.
, java.
, or java.
to return a
file channel connected to the same underlying file as the java.
class.
Multiplexed, non-blocking I/O | Description |
---|---|
java. |
A channel that can be multiplexed |
java. |
A channel to a datagram-oriented socket |
java. |
The write end of a pipe |
java. |
The read end of a pipe |
java. |
A channel to a stream-oriented listening socket |
java. |
A channel for a stream-oriented connecting socket |
java. |
A multiplexor of selectable channels |
java. |
A token representing the registration of a channel with a selector |
java. |
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 java.
, java.
, and java.
classes defined in the java.
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.
java.
,
java.
and
java.
s can be created
with different protocol families
. The standard
family types are specified in java.
.
Channels for Internet Protocol sockets are created using the
INET
or INET6
protocol families. Internet
Protocol sockets support network communication using TCP and UDP and are
addressed using java.
es which encapsulate an IP
address and port number. Internet Protocol sockets are also the default
type created, when a protocol family is not specified in the channel factory
creation method.
Channels for Unix Domain sockets are created
using the UNIX
protocol family.
Unix Domain sockets support local inter-process
communication on the same host, and are addressed using java.
es which encapsulate a filesystem pathname
on the local system.
The implementation of selectors, selectable channels, and selection keys
can be replaced by "plugging in" an alternative definition or instance of the
java.
class defined in the java.
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 java.
, java.
, java.
, and java.
classes in the java.
package. When defining a custom selector provider,
only the java.
and java.
classes should be subclassed
directly; custom channel classes should extend the appropriate java.
subclasses defined in this package.
Asynchronous I/O | Description |
---|---|
java. |
An asynchronous channel for reading, writing, and manipulating a file |
java. |
An asynchronous channel to a stream-oriented connecting socket |
java. |
An asynchronous channel to a stream-oriented listening socket |
java. |
A handler for consuming the result of an asynchronous operation |
java. |
A grouping of asynchronous channels for the purpose of resource sharing |
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 java.
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 java.
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 java.
class
for asynchronous reading, writing, and manipulating a file. As with the java.
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 java.
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 java.
class defined in the
java.
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.
Modifier and Type | Interface and Description |
---|---|
public interface | AsynchronousByteChannel
An asynchronous channel that can read and write bytes. |
public interface | AsynchronousChannel
A channel that supports asynchronous I/O operations. |
public interface | ByteChannel
A channel that can read and write bytes. |
public interface | Channel
A nexus for I/O operations. |
public interface | CompletionHandler<
The result type of the I/O operation V, The type of the object attached to the I/O operation A>A handler for consuming the result of an asynchronous I/O operation. |
public interface | GatheringByteChannel
A channel that can write bytes from a sequence of buffers. |
public interface | InterruptibleChannel
A channel that can be asynchronously closed and interrupted. |
public interface | MulticastChannel
A network channel that supports Internet Protocol (IP) multicasting. |
public interface | NetworkChannel
A channel to a network socket. |
public interface | ReadableByteChannel
A channel that can read bytes. |
public interface | ScatteringByteChannel
A channel that can read bytes into a sequence of buffers. |
public interface | SeekableByteChannel
A byte channel that maintains a current position and allows the position to be changed. |
public interface | WritableByteChannel
A channel that can write bytes. |
Modifier and Type | Class and Description |
---|---|
public class | 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. |
public class | AlreadyBoundException
Unchecked exception thrown when an attempt is made to bind the socket a network oriented channel that is already bound. |
public class | AlreadyConnectedException
Unchecked exception thrown when an attempt is made to connect a |
public abstract class | AsynchronousChannelGroup
A grouping of asynchronous channels for the purpose of resource sharing. |
public class | 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. |
public abstract class | AsynchronousFileChannel
An asynchronous channel for reading, writing, and manipulating a file. |
public abstract class | AsynchronousServerSocketChannel
An asynchronous channel for stream-oriented listening sockets. |
public abstract class | AsynchronousSocketChannel
An asynchronous channel for stream-oriented connecting sockets. |
public class | CancelledKeyException
Unchecked exception thrown when an attempt is made to use a selection key that is no longer valid. |
public class | Channels
Utility methods for channels and streams. |
public class | ClosedByInterruptException
Checked exception received by a thread when another thread interrupts it while it is blocked in an I/O operation upon a channel. |
public class | 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. |
public class | ClosedSelectorException
Unchecked exception thrown when an attempt is made to invoke an I/O operation upon a closed selector. |
public class | ConnectionPendingException
Unchecked exception thrown when an attempt is made to connect a |
public abstract class | DatagramChannel
A selectable channel for datagram-oriented sockets. |
public abstract class | FileChannel
A channel for reading, writing, mapping, and manipulating a file. |
public abstract class | FileLock
A token representing a lock on a region of a file. |
public class | FileLockInterruptionException
Checked exception received by a thread when another thread interrupts it while it is waiting to acquire a file lock. |
public class | IllegalBlockingModeException
Unchecked exception thrown when a blocking-mode-specific operation is invoked upon a channel in the incorrect blocking mode. |
public class | IllegalChannelGroupException
Unchecked exception thrown when an attempt is made to open a channel in a group that was not created by the same provider. |
public class | 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. |
public class | InterruptedByTimeoutException
Checked exception received by a thread when a timeout elapses before an asynchronous operation completes. |
public abstract class | MembershipKey
A token representing the membership of an Internet Protocol (IP) multicast group. |
public class | NoConnectionPendingException
Unchecked exception thrown when the |
public class | NonReadableChannelException
Unchecked exception thrown when an attempt is made to read from a channel that was not originally opened for reading. |
public class | NonWritableChannelException
Unchecked exception thrown when an attempt is made to write to a channel that was not originally opened for writing. |
public class | 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. |
public class | NotYetConnectedException
Unchecked exception thrown when an attempt is made to invoke an I/O operation upon a socket channel that is not yet connected. |
public class | 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. |
public abstract class | Pipe
A pair of channels that implements a unidirectional pipe. |
public class | ReadPendingException
Unchecked exception thrown when an attempt is made to read from an asynchronous socket channel and a previous read has not completed. |
public abstract class | SelectableChannel
A channel that can be multiplexed via a |
public abstract class | SelectionKey
A token representing the registration of a |
public abstract class | Selector
A multiplexor of |
public abstract class | ServerSocketChannel
A selectable channel for stream-oriented listening sockets. |
public class | 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. |
public abstract class | SocketChannel
A selectable channel for stream-oriented connecting sockets. |
public class | UnresolvedAddressException
Unchecked exception thrown when an attempt is made to invoke a network operation upon an unresolved socket address. |
public class | UnsupportedAddressTypeException
Unchecked exception thrown when an attempt is made to bind or connect to a socket address of a type that is not supported. |
public class | WritePendingException
Unchecked exception thrown when an attempt is made to write to an asynchronous socket channel and a previous write has not completed. |