Skip to content

Most visited

Recently visited

navigation
Added in API level 1

SelectionKey

public abstract class SelectionKey
extends Object

java.lang.Object
   ↳ java.nio.channels.SelectionKey
Known Direct Subclasses


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 a channel is interested in performing.

Ready set

The ready set is an operation set that shows the operations that a channel is ready to execute.

Summary

Constants

int OP_ACCEPT

Interest set mask bit for socket-accept operations.

int OP_CONNECT

Interest set mask bit for socket-connect operations.

int OP_READ

Interesting operation mask bit for read operations.

int OP_WRITE

Interest set mask bit for write operations.

Protected constructors

SelectionKey()

Constructs a new SelectionKey.

Public methods

final Object attach(Object anObject)

Attaches an object to this key.

final 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.

final boolean isAcceptable()

Indicates whether this key's channel is interested in the accept operation and is ready to accept new connections.

final boolean isConnectable()

Indicates whether this key's channel is interested in the connect operation and is ready to connect.

final 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.

final 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.

Inherited methods

From class java.lang.Object

Constants

OP_ACCEPT

Added in API level 1
int OP_ACCEPT

Interest set mask bit for socket-accept operations.

Constant Value: 16 (0x00000010)

OP_CONNECT

Added in API level 1
int OP_CONNECT

Interest set mask bit for socket-connect operations.

Constant Value: 8 (0x00000008)

OP_READ

Added in API level 1
int OP_READ

Interesting operation mask bit for read operations.

Constant Value: 1 (0x00000001)

OP_WRITE

Added in API level 1
int OP_WRITE

Interest set mask bit for write operations.

Constant Value: 4 (0x00000004)

Protected constructors

SelectionKey

Added in API level 1
SelectionKey ()

Constructs a new SelectionKey.

Public methods

attach

Added in API level 1
Object attach (Object anObject)

Attaches an object to this key. It is acceptable to attach null, this discards the old attachment.

Parameters
anObject Object: the object to attach, or null to discard the current attachment.
Returns
Object the last attached object or null if no object has been attached.

attachment

Added in API level 1
Object attachment ()

Gets the attached object.

Returns
Object the attached object or null if no object has been attached.

cancel

Added in API level 1
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

Added in API level 1
SelectableChannel channel ()

Gets the channel of this key.

Returns
SelectableChannel the channel of this key.

interestOps

Added in API level 1
int interestOps ()

Gets this key's interest set. The returned set has only those bits set that are valid for this key's channel.

Returns
int the interest set of this key.
Throws
CancelledKeyException if the key has already been canceled.

interestOps

Added in API level 1
SelectionKey interestOps (int operations)

Sets the interest set for this key.

Parameters
operations int: the new interest set.
Returns
SelectionKey this key.
Throws
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.

isAcceptable

Added in API level 1
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
boolean true if the channel is interested in the accept operation and is ready to accept new connections, false otherwise.
Throws
CancelledKeyException if the key has already been canceled.

isConnectable

Added in API level 1
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
boolean true if the channel is interested in the connect operation and is ready to connect, false otherwise.
Throws
CancelledKeyException if the key has already been canceled.

isReadable

Added in API level 1
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
boolean true if the channel is interested in the read operation and is ready to read, false otherwise.
Throws
CancelledKeyException if the key has already been canceled.

isValid

Added in API level 1
boolean isValid ()

Indicates whether this key is valid. A key is valid as long as it has not been canceled.

Returns
boolean true if this key has not been canceled, false otherwise.

isWritable

Added in API level 1
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
boolean true if the channel is interested in the write operation and is ready to write, false otherwise.
Throws
CancelledKeyException if the key has already been canceled.

readyOps

Added in API level 1
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
int the operations for which this key's channel is ready.
Throws
CancelledKeyException if the key has already been canceled.

selector

Added in API level 1
Selector selector ()

Gets the selector for which this key's channel is registered.

Returns
Selector the related selector.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.