SelectionKey


SelectionKey java.nio.channels

Java 1.4

A SelectionKey represents the registration of a SelectableChannel with a Selector , and serves to identify a selected channel and the operations that are ready to be performed on that channel. After a call to the select( ) method of a selector, the selectedKeys( ) method of the selector returns a Set of SelectionKey objects to identify the channel or channels that are ready for reading, for writing, or for another operation.

Create a SelectionKey by passing a Selector object to the register( ) method of a SelectableChannel . The channel( ) and selector( ) methods of the returned SelectionKey return the SelectableChannel and Selector objects associated with that key.

When you no longer wish the channel to be registered with the selector, call the cancel( ) method of the SelectionKey . isValid( ) determines whether a SelectionKey is still "valid"it returns true unless the cancel( ) method has been called, the channel has been closed or the selector has been closed.

The main purpose of a SelectionKey is to hold the "interest set" of channel operations that the selector should monitor for the channel, and also the "ready set" of operations that the selector has determined are ready to proceed on the channel. Both sets are represented as integer bitmasks (not java.util.Set objects) formed by OR-ing together any of the OP_ constants defined by this class. Those constants are the following:


OP_READ

In the interest set, this bit specifies an interest in read operations. In the ready set, this bit specifies that the channel has bytes available for reading, has reached the end-of-stream, has been remotely closed, or that an error has occurred.


OP_WRITE

In the interest set, this bit specifies an interest in write operations. In the ready set, this bit specifies that the channel is ready to have bytes written, or has been closed, or that an error has occurred.


OP_CONNECT

In the interest set, this bit specifies an interest in socket connection operations. In the ready set, it indicates that a socket channel is ready to connect, or that an error has occurred.


OP_ACCEPT

In the interest set, this bit specifies an interest in server socket accept operations. In the ready set, it indicates that a server socket channel is ready to accept a connection or that an error has occurred.

The no-argument version of the interestOps( ) method allows you to query the interest set. The inital value of the interest set the bitmask that was passed to the register( ) method of the channel. It can be changed, however, by passing a new bitmask to the one-argument version of interestOps( ) . (Note that the same method name is used to both query and set the interest set.) The current state of the ready set can be queried with readyOps( ) . You can also use the convenience methods isReadable( ) , isWritable( ) isConnectable( ) and isAcceptable( ) to test whether individual operation bits are set in the ready set bitmask. There is no way to explicitly set the state of the ready seteach call to select( ) method updates the ready set for you. Note, however, that you must remove a SelectionKey object from the Set returned by Selector.selectedKeys( ) for the bits of the ready set to be cleared at the start of the next selection operation. If you never remove the SelectionKey from the set of selected keys, the Selector assumes that none of the I/O readyness conditions represented by the ready set have been handled yet, and leaves their bits set.

Use attach( ) to associate an arbitrary object with a SelectionKey , and call attachment( ) to query that object. This ability to associate data with a selection key is often useful when using a Selector with multiple channels: it can provide the context necessary to process a SelectionKey that has been selected.

 public abstract class  SelectionKey  {  // Protected Constructors  protected  SelectionKey  ( );  // Public Constants  public static final int  OP_ACCEPT  ;  =16  public static final int  OP_CONNECT  ;  =8  public static final int  OP_READ  ;  =1  public static final int  OP_WRITE  ;  =4   // Public Instance Methods  public final Object  attach  (Object  ob  );        public final Object  attachment  ( );        public abstract void  cancel  ( );        public abstract SelectableChannel  channel  ( );        public abstract int  interestOps  ( );        public abstract SelectionKey  interestOps  (int  ops  );        public final boolean  isAcceptable  ( );        public final boolean  isConnectable  ( );        public final boolean  isReadable  ( );        public abstract boolean  isValid  ( );        public final boolean  isWritable  ( );        public abstract int  readyOps  ( );        public abstract Selector  selector  ( );   } 

Subclasses

java.nio.channels.spi.AbstractSelectionKey

Returned By

SelectableChannel.{keyFor( ) , register( )} , java.nio.channels.spi.AbstractSelectableChannel.{keyFor( ) , register( )} , java.nio.channels.spi.AbstractSelector.register( )



Java In A Nutshell
Java In A Nutshell, 5th Edition
ISBN: 0596007736
EAN: 2147483647
Year: 2004
Pages: 1220

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