Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 422
نمايش فراداده

Package java.nio.channels

Java 1.4

This package is at the heart of the NIO API. A

channel is a communication channel for transferring bytes from or to a java.nio.ByteBuffer. Channels serve a similar purpose to the InputStream and OutputStream classes of the java.io package, but are completely unrelated to those classes, and provide important features not available with the java.io API. The Channels class defines methods that bridge the java.io and java.nio.channels APIs, by returning channels based on streams and streams based on channels.

The Channel interface simply defines methods for testing whether a channel is open and for closing a channel. The other interfaces in the package extend Channel and define read( ) and write( ) methods for reading bytes from the channel into one or more byte buffers and for writing bytes from one or more byte buffers to the channel.

The FileChannel class defines an channel-based API for reading and writing from files (and also provides other important file functionality such as file locking and memory mapping that is not available through the java.io package). SocketChannel, ServerSocketChannel, and DatagramChannel are channels for communication over a network, and Pipe defines two inner classes that use the channel abstraction for communication between threads.

The network and pipe channels are all subclasses of the SelectableChannel class, and may be put into nonblocking mode, in which calls to read( ) and write( ) return immediately, even if the channel is not ready for reading or writing. nonblocking IO and networking is not possible using the stream abstraction of the java.io and java.net packages, and is perhaps the most important new feature of the java.nio API. The Selector class is crucial to the efficient use of nonblocking channels: it allows a program to register interested in I/O operations on several different channels at once. A call to the select( ) method of a Selector will block until one of those channels becomes ready for I/O, and will then wake up. This technique is important for writing scalable high-performance network servers. See Selector and SelectionKey for details.

Finally, this package allows for very fine-grained error handling by defining a large number of exception classes, several of which may be thrown by only a single method within the java.nio API.

Interfaces

public interface

ByteChannel extends ReadableByteChannel, WritableByteChannel; public interface

Channel extends java.io.Closeable; public interface

GatheringByteChannel extends WritableByteChannel; public interface

InterruptibleChannel extends Channel; public interface

ReadableByteChannel extends Channel; public interface

ScatteringByteChannel extends ReadableByteChannel; public interface

WritableByteChannel extends Channel;

Classes

public final class

Channels ; public abstract class

DatagramChannel extends java.nio.channels.spi. AbstractSelectableChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel; public abstract class

FileChannel extends java.nio.channels.spi. AbstractInterruptibleChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel; public static class

FileChannel.MapMode ; public abstract class

FileLock ; public abstract class

Pipe ; public abstract static class

Pipe.SinkChannel extends java.nio.channels.spi. AbstractSelectableChannel implements GatheringByteChannel, WritableByteChannel; public abstract static class

Pipe.SourceChannel extends java.nio.channels.spi. AbstractSelectableChannel implements ReadableByteChannel, ScatteringByteChannel; public abstract class

SelectableChannel extends java.nio.channels.spi. AbstractInterruptibleChannel implements Channel; public abstract class

SelectionKey ; public abstract class

Selector ; public abstract class

ServerSocketChannel extends java.nio.channels.spi. AbstractSelectableChannel; public abstract class

SocketChannel extends java.nio.channels.spi. AbstractSelectableChannel implements ByteChannel, GatheringByteChannel, ScatteringByteChannel;

Exceptions

public class

AlreadyConnectedException extends IllegalStateException; public class

CancelledKeyException extends IllegalStateException; public class

ClosedChannelException extends java.io.IOException; public class

AsynchronousCloseException extends ClosedChannelException; public class

ClosedByInterruptException extends AsynchronousCloseException; public class

ClosedSelectorException extends IllegalStateException; public class

ConnectionPendingException extends IllegalStateException; public class

FileLockInterruptionException extends java.io.IOException; public class

IllegalBlockingModeException extends IllegalStateException; public class

IllegalSelectorException extends IllegalArgumentException; public class

NoConnectionPendingException extends IllegalStateException; public class

NonReadableChannelException extends IllegalStateException; public class

NonWritableChannelException extends IllegalStateException; public class

NotYetBoundException extends IllegalStateException; public class

NotYetConnectedException extends IllegalStateException; public class

OverlappingFileLockException extends IllegalStateException; public class

UnresolvedAddressException extends IllegalArgumentException; public class

UnsupportedAddressTypeException extends IllegalArgumentException;