The java.nio.channels Package


The java.nio.channels package defines channels. The channels represent connections to entities that can perform I/O operations. The various entities are files, sockets server socket, and selectors. The entities use the channels to perform I/O operations, such as read and write. In addition, this package helps you create a non-blocking server. A non-blocking server handles multiple client requests without blocking them. A non-blocking server is created using the selector class of NIO. A selector is a selectable channel, which is a special type of channel that can be put in a non-blocking mode. The selectors process all the client requests and send the requests to the server.

Note  

To learn more about java.nio.channels package, see: http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/package-summary.html

Figure 1-2 shows the class diagram for the java.nio.channels package:

click to expand: this figure shows the class associated with the java.nio.channels package.
Figure 1-2: The java.nio.channels Package

Channel

The Channel class contains utility methods to work with channels and streams. The most commonly used methods in the Channel class are:

  • newChannel() : Returns a new readable byte channel value. The newChannel() method constructs a channel that can read bytes from a given stream.

  • newInputStream() : Returns the InputStream object. The newInputStream() method constructs a stream that can read bytes from a given channel.

  • newReader() : Returns the Reader object. The newReader() method constructs a reader that decodes the bytes. The reader uses a decoder to decode bytes from a given channel.

  • newWriter() : Returns the Writer object. The newWriter() method constructs a writer that encodes characters. The writer uses an encoder to encode the characters and writes the resulting bytes to a given channel.

FileChannel

The FileChannel class provides a channel to read, write, map, and manipulate a file. The FileChannel class defines methods that can read and write bytes from a specific location in a file. The most commonly used methods in the FileChannel class are:

  • force() : Updates the files that use the channel. The force() method also writes a back up file to the storage device. The force() method throws the ClosedChannelException exception if the channel is closed. In addition, the force() method throws the IOException exception if some other types of input/output errors occur.

  • lock() : Returns an object of the FileLock class. The lock() method acquires a lock on a specific portion of a file or a complete file that uses the channel.

  • map() : Returns an object of the MappedByteBuffer class. The map() method maps an area of a file of the channel into the memory. The mapping can be read-only, read/write, or private. The method throws the NonReadableChannelException exception if the source channel is not open to read but the mode is READ_ONLY. The method throws the NonWritableChannelException exception if the channel is not open to write and the mode is READ_WRITE or PRIVATE.

  • read() : Returns an integer value that indicates the number of bytes read. The read() method reads bytes from the channel into a given buffer. In addition, the read() method throws the NonReadableChannelException exception if the channel is not open to read. The method may throw the ClosedChannelException exception if the channel is closed.

  • size () : Returns a long value that indicates the size of the file in the channel. The size() method throws the ClosedChannelException exception if the channel is closed. In addition, the size() method throws the IOException exception if some input/output error occurs.

  • transferFrom() : Returns a long type value. The transferFrom() method transfers bytes into a file of the channel. In addition, the transferFrom() method throws a NonReadableChannelException exception if the source channel is not open to read. The method may throw the NonWritableChannelException exception if the channel is not open for performing write operation.

  • transferTo() : Returns a long type value. The transferTo() method transfer bytes from a file of the channel to a given writable byte channel. In addition, the transferTo() method throws the NonReadableChannelException exception if the source channel is not open to read. The method may throw the NonWritableChannelException exception if the channel is not open for performing write operation.

  • write() : Returns an int value that indicates the number of bytes written to the specified file. The bytes are written to the file specified The write() method writes bytes from the channel into a given buffer. In addition, the write() method throws the NonWriteableChannelException exception if the channel is not open to write. The method can also throw the ClosedChannelException exception if the channel is closed.

FileLock

The FileLock class contains methods that perform file lock operations on a file. This class works as a token that represents a lock on a portion of a file. You can acquire a lock on a file in exclusive or shared mode. The most commonly used methods in the FileLock class are:

  • channel() : Returns an object of the FileChannel class.

  • isShared() : Returns a Boolean value that indicates whether or not the type of lock is shared.

  • isValid() : Returns a Boolean value that indicates whether or not the lock is valid.

  • position() : Returns a long value that indicates the first byte of the locked region.

  • release() : Releases a lock from the file.

  • size() : Returns a long value that indicates the total number of bytes in the locked region.

Selector

The Selector class represents a multiplexor of the SelectableChannel class object. Moreover, the Selector class manages the information about the registered channels and their respective states. When a channel registers with the selector, the selector is responsible to update the state of a channel. The Selector class uses the service provider classes of the java.nio.channels.spi package to create a new selector. The most commonly used methods of the Selector class are:

  • isOpen() : Returns a Boolean value that indicates whether or not the selector is open. The isOpen() method returns true if the selector is open.

  • keys() : Returns an object of the Set interface that indicates the key set of the selector. The Set interface represents a collection that contains no duplicate elements. This method throws the ClosedSelectorException exception. This exception occurs when the selector on which you perform I/O operation is closed.

  • open() : Returns an object of the Selector class. The open() method opens a selector. In addition, the open() method throws the IOException exception if an input/output error occurs.

  • provider() : Returns an object of the SelectorProvider class. The SelectorProvider class is a provider that creates the channel.

SocketChannel

The SocketChannel class represents a selectable channel. The selectable channel is a type of channel that connects a selector to a stream-oriented socket using TCP/IP. The SocketChannel class contains methods to work with the socket channel. The most commonly used methods in the SocketChannel class are:

  • connect() : Returns a Boolean value that indicates whether or not a connection is established. In addition, the connect() method returns true if a connection exists, otherwise the method returns false. The connect() method throws the ClosedChannelException exception if the channel is closed. In addition, the connect() method throws the AlreadyConnectedException exception if the channel is already connected.

  • finishConnect() : Returns a Boolean value that indicates whether or not the process to connect a socket channel is complete. In addition, the finishConnect() method returns true if the socket of the channel is connected. The method throws a ClosedChannelException exception if the channel is closed. In addition, the finishConnect() method throws an IOException exception if any input/output error occurs.

  • isConnected() : Returns a Boolean value that indicates whether or not the network socket of a channel is connected to the server. In addition, the isConnected() method returns true if the network socket of a channel is connected, otherwise the method returns false.

  • open() : Returns an object of the SocketChannel class. In addition, the connect() method opens a socket channel. This method also throws an IOException exception, if any input/output error occurs.

  • read() : Returns an int value that indicates the number of bytes read. The read() method reads a series of bytes from the channel in a buffer. This method also throws an IOException exception.

  • socket() : Returns an object of the Socket class. The socket() method retrieves a socket from the associated channel.

  • write() : Returns an int value that indicates the number of bytes written. The write() method writes a series of bytes to the channel from a given buffer. In addition, the write() method throws the NotYetConnectedException exception if the channel is not connected. The write() method throws the IOException exception if any input/output error occurs.

SelectionKey

The SelectionKey class creates and manages a selection key. A selection key is a token that represents the registration of the SelectableChannel class with the Selector class. This token is created each time the objects of a channel get registered with a selector. The most commonly used methods in the SelectionKey class are:

  • attach() : Returns an object of the Object class. The attach() method attaches an object to the selection key.

  • attachment() : Returns an object of the Object class. The attachment() method retrieves the object that is currently attached to the selection key.

  • channel() : Returns an object of the SelectChannel() class that indicates a selector for the selection key.

  • isAcceptable() : Returns a Boolean value that indicates whether or not the selection key is ready to accept a new socket connection. The isAcceptable() method returns true if the readyOps() method and OP_ACCEPT field are greater than zero.

  • isReadable() : Returns a Boolean value that indicates whether or not the selection key is ready to read. The isReadable() method returns true if readyOps() method and OP_READ field are greater than zero.

  • isValid() : Returns a Boolean value that indicates whether or not the selection key is valid. The isValid() method returns true if the selection key is valid.

  • isWritable() : Returns a Boolean value that indicates whether or not the selection key is ready to write. The isWritable() method returns true if readyOps() method and OP_WRITE field are greater than zero.

The SelectionKey class also defines four static variables :

  • OP_ACCEPT : Represents the operation-set bit for socket-accept operations.

  • OP_CONNECT : Represents the operation-set bit for socket-connect operations.

  • OP_READ : Represents the operation-set bit for read operations.

  • OP_WRITE : Represents the operation-set bit for write operations.




Java InstantCode. Developing Applications Using Java NIO
Java InstantCode. Developing Applications Using Java NIO
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 55

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net