Movatterモバイル変換


[0]ホーム

URL:


Skip navigation links
Java™ Platform
Standard Ed. 8

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.

See: Description

Package java.nio.channels Description

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
ChannelA nexus for I/O operations
  ReadableByteChannelCan read into a buffer
    ScatteringByteChannel  Can read into a sequence of buffers
  WritableByteChannelCan write from a buffer
    GatheringByteChannelCan write from a sequence of buffers
  ByteChannelCan read/write to/from a buffer
    SeekableByteChannelAByteChannel connected to an entity that contains a variable-length sequence of bytes
  AsynchronousChannelSupports asynchronous I/O operations.
    AsynchronousByteChannelCan read and write bytes asynchronously
  NetworkChannelA channel to a network socket
    MulticastChannelCan join Internet Protocol (IP) multicast groups
ChannelsUtility methods for channel/stream interoperation

Achannel 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 theChannel interface, channels are either open or closed, and they are bothasynchronously closeable andinterruptible.

TheChannel interface is extended by several other interfaces.

TheReadableByteChannel interface specifies aread method that reads bytes from the channel into a buffer; similarly, theWritableByteChannel interface specifies awrite method that writes bytes from a buffer to the channel. TheByteChannel interface unifies these two interfaces for the common case of channels that can both read and write bytes. TheSeekableByteChannel interface extends theByteChannel interface with methods toquery andmodify the channel's current position, and itssize.

TheScatteringByteChannel andGatheringByteChannel interfaces extend theReadableByteChannel andWritableByteChannel interfaces, respectively, addingread andwrite methods that take a sequence of buffers rather than a single buffer.

TheNetworkChannel interface specifies methods tobind the channel's socket, obtain the address to which the socket is bound, and methods toget andset socket options. TheMulticastChannel interface specifies methods to join Internet Protocol (IP) multicast groups.

TheChannels utility class defines static methods that support the interoperation of the stream classes of thejava.io package with the channel classes of this package. An appropriate channel can be constructed from anInputStream or anOutputStream, and conversely anInputStream or anOutputStream can be constructed from a channel. AReader can be constructed that uses a given charset to decode bytes from a given readable byte channel, and conversely aWriter can be constructed that uses a given charset to encode characters into bytes and write them to a given writable byte channel.

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

TheFileChannel 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 theFileLock 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.

AFileChannel is created by invoking one of its staticopen methods, or by invoking thegetChannel method of aFileInputStream,FileOutputStream, orRandomAccessFile to return a file channel connected to the same underlying file as thejava.io class.

Multiplexed, non-blocking I/O

Description

SelectableChannelA channel that can be multiplexed
  DatagramChannelA channel to a datagram-oriented socket
  Pipe.SinkChannelThe write end of a pipe
  Pipe.SourceChannelThe read end of a pipe
  ServerSocketChannel  A channel to a stream-oriented listening socket
  SocketChannelA channel for a stream-oriented connecting socket
SelectorA multiplexor of selectable channels
SelectionKeyA token representing the registration
of a channel with a selector
PipeTwo channels that form a unidirectional pipe

Multiplexed, non-blocking I/O, which is much more scalable than thread-oriented, blocking I/O, is provided byselectors,selectable channels, andselection keys.

Aselector is a multiplexor ofselectable channels, which in turn are a special type of channel that can be put intonon-blocking mode. To perform multiplexed I/O operations, one or more selectable channels are first created, put into non-blocking mode, andregistered with a selector. Registering a channel specifies the set of I/O operations that will be tested for readiness by the selector, and returns aselection key that represents the registration.

Once some channels have been registered with a selector, aselection 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'sselected-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 theDatagramSocket,ServerSocket, andSocket classes defined in thejava.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 staticopen 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 theSelectorProvider class defined in thejava.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 theAbstractInterruptibleChannel,AbstractSelectableChannel,AbstractSelectionKey, andAbstractSelector classes in thejava.nio.channels.spi package. When defining a custom selector provider, only theAbstractSelector andAbstractSelectionKey classes should be subclassed directly; custom channel classes should extend the appropriateSelectableChannel subclasses defined in this package.

Asynchronous I/ODescription
AsynchronousFileChannelAn asynchronous channel for reading, writing, and manipulating a file
AsynchronousSocketChannelAn asynchronous channel to a stream-oriented connecting socket
AsynchronousServerSocketChannel  An asynchronous channel to a stream-oriented listening socket
CompletionHandlerA handler for consuming the result of an asynchronous operation
AsynchronousChannelGroupA 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 aFuture representing the pending result of each operation. TheFuture can be used to poll or wait for the result of the operation. Asynchronous I/O operations can also specify aCompletionHandler 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 theAsynchronousFileChannel class for asynchronous reading, writing, and manipulating a file. As with theFileChannel 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 theFileChannel it does not define methods for mapping a region of the file directly into memory. Where memory mapped I/O is required, then aFileChannel can be used.

Asynchronous channels are bound to an asynchronous channel group for the purpose of resource sharing. A group has an associatedExecutorService 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 adefault group. Sophisticated users may wish to create their own asynchronous channel groups or configure theExecutorService 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 theAsynchronousChannelProvider class defined in thejava.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 anull argument to a constructor or method in any class or interface in this package will cause aNullPointerException to be thrown.

Since:
1.4
Skip navigation links
Java™ Platform
Standard Ed. 8


[8]ページ先頭

©2009-2025 Movatter.jp