Package java.nio.channels
Class SelectionKey
java.lang.Object
java.nio.channels.SelectionKey
- Direct Known Subclasses:
AbstractSelectionKey
public abstract class SelectionKey extends Object
A
SelectionKey represents the relationship between a channel and a
selector for which the channel is registered.
Operation set
An operation set is represented by an integer value. The bits of an operation set represent categories of operations for a key's channel: Accepting socket connections (OP_ACCEPT), connecting with a socket (OP_CONNECT),
reading (OP_READ) and writing (OP_WRITE).
Interest set
The interest set is an operation set that defines the operations that achannel is interested in performing.
Ready set
The ready set is an operation set that shows the operations that achannel is ready to execute.-
Field Summary
Fields Modifier and Type Field Description static intOP_ACCEPTInterest set mask bit for socket-accept operations.static intOP_CONNECTInterest set mask bit for socket-connect operations.static intOP_READInteresting operation mask bit for read operations.static intOP_WRITEInterest set mask bit for write operations. -
Constructor Summary
Constructors Modifier Constructor Description protectedSelectionKey()Constructs a newSelectionKey. -
Method Summary
Modifier and Type Method Description Objectattach(Object anObject)Attaches an object to this key.Objectattachment()Gets the attached object.abstract voidcancel()Cancels this key.abstract SelectableChannelchannel()Gets the channel of this key.abstract intinterestOps()Gets this key'sinterest set.abstract SelectionKeyinterestOps(int operations)Sets theinterest setfor this key.booleanisAcceptable()Indicates whether this key's channel is interested in the accept operation and is ready to accept new connections.booleanisConnectable()Indicates whether this key's channel is interested in the connect operation and is ready to connect.booleanisReadable()Indicates whether this key's channel is interested in the read operation and is ready to read.abstract booleanisValid()Indicates whether this key is valid.booleanisWritable()Indicates whether this key's channel is interested in the write operation and is ready to write.abstract intreadyOps()Gets the set of operations that are ready.abstract Selectorselector()Gets the selector for which this key's channel is registered.
-
Field Details
-
OP_ACCEPT
public static final int OP_ACCEPTInterest set mask bit for socket-accept operations.- See Also:
- Constant Field Values
-
OP_CONNECT
public static final int OP_CONNECTInterest set mask bit for socket-connect operations.- See Also:
- Constant Field Values
-
OP_READ
public static final int OP_READInteresting operation mask bit for read operations.- See Also:
- Constant Field Values
-
OP_WRITE
public static final int OP_WRITEInterest set mask bit for write operations.- See Also:
- Constant Field Values
-
-
Constructor Details
-
SelectionKey
protected SelectionKey()Constructs a newSelectionKey.
-
-
Method Details
-
attach
Attaches an object to this key. It is acceptable to attachnull, this discards the old attachment.- Parameters:
anObject- the object to attach, ornullto discard the current attachment.- Returns:
- the last attached object or
nullif no object has been attached.
-
attachment
Gets the attached object.- Returns:
- the attached object or
nullif no object has been attached.
-
cancel
public abstract void cancel()Cancels this key.A key that has been canceled is no longer valid. Calling this method on an already canceled key does nothing.
Calling this method is safe at any time. The call might block until another ongoing call to a method of this selector has finished. The reason is that it is synchronizing on the key set of the selector. After this call finishes, the key will have been added to the selectors canceled-keys set and will not be included in any future selects of this selector.
-
channel
Gets the channel of this key.- Returns:
- the channel of this key.
-
interestOps
public abstract int interestOps()Gets this key'sinterest set. The returned set has only those bits set that are valid for this key's channel.- Returns:
- the interest set of this key.
- Throws:
CancelledKeyException- if the key has already been canceled.
-
interestOps
Sets theinterest setfor this key.- Parameters:
operations- the new interest set.- Returns:
- this key.
- Throws:
IllegalArgumentException- if a bit inoperationsis not in the set ofvalid operationsof this key's channel.CancelledKeyException- if the key has already been canceled.
-
isAcceptable
public final boolean isAcceptable()Indicates whether this key's channel is interested in the accept operation and is ready to accept new connections. A call to this method is equal to executing(readyOps() & OP_ACCEPT) == OP_ACCEPT.- Returns:
trueif the channel is interested in the accept operation and is ready to accept new connections,falseotherwise.- Throws:
CancelledKeyException- if the key has already been canceled.
-
isConnectable
public final boolean isConnectable()Indicates whether this key's channel is interested in the connect operation and is ready to connect. A call to this method is equal to executing(readyOps() & OP_CONNECT) == OP_CONNECT.- Returns:
trueif the channel is interested in the connect operation and is ready to connect,falseotherwise.- Throws:
CancelledKeyException- if the key has already been canceled.
-
isReadable
public final boolean isReadable()Indicates whether this key's channel is interested in the read operation and is ready to read. A call to this method is equal to executing(readyOps() & OP_READ) == OP_READ.- Returns:
trueif the channel is interested in the read operation and is ready to read,falseotherwise.- Throws:
CancelledKeyException- if the key has already been canceled.
-
isValid
public abstract boolean isValid()Indicates whether this key is valid. A key is valid as long as it has not been canceled.- Returns:
trueif this key has not been canceled,falseotherwise.
-
isWritable
public final boolean isWritable()Indicates whether this key's channel is interested in the write operation and is ready to write. A call to this method is equal to executing(readyOps() & OP_WRITE) == OP_WRITE.- Returns:
trueif the channel is interested in the write operation and is ready to write,falseotherwise.- Throws:
CancelledKeyException- if the key has already been canceled.
-
readyOps
public abstract int readyOps()Gets the set of operations that are ready. The returned set has only those bits set that are valid for this key's channel.- Returns:
- the operations for which this key's channel is ready.
- Throws:
CancelledKeyException- if the key has already been canceled.
-
selector
Gets the selector for which this key's channel is registered.- Returns:
- the related selector.
-