public abstract class SelectionKey extends Object
SelectionKey represents the relationship between a channel and a
selector for which the channel is registered.
OP_ACCEPT), connecting with a socket (OP_CONNECT),
reading (OP_READ) and writing (OP_WRITE).
channel is interested in performing.
channel is ready to execute.| Modifier and Type | Field and Description |
|---|---|
static int |
OP_ACCEPT
Interest set mask bit for socket-accept operations.
|
static int |
OP_CONNECT
Interest set mask bit for socket-connect operations.
|
static int |
OP_READ
Interesting operation mask bit for read operations.
|
static int |
OP_WRITE
Interest set mask bit for write operations.
|
| Modifier | Constructor and Description |
|---|---|
protected |
SelectionKey()
Constructs a new
SelectionKey. |
| Modifier and Type | Method and Description |
|---|---|
Object |
attach(Object anObject)
Attaches an object to this key.
|
Object |
attachment()
Gets the attached object.
|
abstract void |
cancel()
Cancels this key.
|
abstract SelectableChannel |
channel()
Gets the channel of this key.
|
abstract int |
interestOps()
Gets this key's
interest set. |
abstract SelectionKey |
interestOps(int operations)
Sets the
interest set for this key. |
boolean |
isAcceptable()
Indicates whether this key's channel is interested in the accept
operation and is ready to accept new connections.
|
boolean |
isConnectable()
Indicates whether this key's channel is interested in the connect
operation and is ready to connect.
|
boolean |
isReadable()
Indicates whether this key's channel is interested in the read operation
and is ready to read.
|
abstract boolean |
isValid()
Indicates whether this key is valid.
|
boolean |
isWritable()
Indicates whether this key's channel is interested in the write operation
and is ready to write.
|
abstract int |
readyOps()
Gets the set of operations that are ready.
|
abstract Selector |
selector()
Gets the selector for which this key's channel is registered.
|
public static final int OP_ACCEPT
public static final int OP_CONNECT
public static final int OP_READ
public static final int OP_WRITE
public final Object attach(Object anObject)
null,
this discards the old attachment.anObject - the object to attach, or null to discard the current
attachment.null if no object has been
attached.public final Object attachment()
null if no object has been
attached.public abstract void cancel()
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.
public abstract SelectableChannel channel()
public abstract int interestOps()
interest set. The returned set has
only those bits set that are valid for this key's channel.CancelledKeyException - if the key has already been canceled.public abstract SelectionKey interestOps(int operations)
interest set for this key.operations - the new interest set.IllegalArgumentException - if a bit in operations is not in the set of
valid operations of this
key's channel.CancelledKeyException - if the key has already been canceled.public final boolean isAcceptable()
(readyOps() & OP_ACCEPT) == OP_ACCEPT.true if the channel is interested in the accept operation
and is ready to accept new connections, false otherwise.CancelledKeyException - if the key has already been canceled.public final boolean isConnectable()
(readyOps() & OP_CONNECT) == OP_CONNECT.true if the channel is interested in the connect
operation and is ready to connect, false otherwise.CancelledKeyException - if the key has already been canceled.public final boolean isReadable()
(readyOps() & OP_READ) == OP_READ.true if the channel is interested in the read operation
and is ready to read, false otherwise.CancelledKeyException - if the key has already been canceled.public abstract boolean isValid()
true if this key has not been canceled, false
otherwise.public final boolean isWritable()
(readyOps() & OP_WRITE) == OP_WRITE.true if the channel is interested in the write operation
and is ready to write, false otherwise.CancelledKeyException - if the key has already been canceled.public abstract int readyOps()
CancelledKeyException - if the key has already been canceled.public abstract Selector selector()