# SelectionKey

public abstract class **SelectionKey** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  

|---|---|---|
| Known Direct Subclasses [AbstractSelectionKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/spi/AbstractSelectionKey.html) |---|---| | [AbstractSelectionKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/spi/AbstractSelectionKey.html) | Base implementation class for selection keys. | |||

A token representing the registration of a `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html` with a
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector.html`.

A selection key is created each time a channel is registered with a
selector. A key remains valid until it is *cancelled* by invoking its
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#cancel()` method, by closing its channel, or by closing its
selector. Cancelling a key does not immediately remove it from its
selector; it is instead added to the selector's [*cancelled-key set*](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector#ks) for removal during the
next selection operation. The validity of a key may be tested by invoking
its `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isValid()` method.


A selection key contains two *operation sets* represented as
integer values. Each bit of an operation set denotes a category of
selectable operations that are supported by the key's channel.

- The *interest set* determines which operation categories will
  be tested for readiness the next time one of the selector's selection
  methods is invoked. The interest set is initialized with the value given
  when the key is created; it may later be changed via the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#interestOps(int)` method.

- The *ready set* identifies the operation categories for which
  the key's channel has been detected to be ready by the key's selector.
  The ready set is initialized to zero when the key is created; it may later
  be updated by the selector during a selection operation, but it cannot be
  updated directly.

That a selection key's ready set indicates that its channel is ready for
some operation category is a hint, but not a guarantee, that an operation in
such a category may be performed by a thread without causing the thread to
block. A ready set is most likely to be accurate immediately after the
completion of a selection operation. It is likely to be made inaccurate by
external events and by I/O operations that are invoked upon the
corresponding channel.

This class defines all known operation-set bits, but precisely which
bits are supported by a given channel depends upon the type of the channel.
Each subclass of `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html` defines an `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html#validOps()` method which returns a set
identifying just those operations that are supported by the channel. An
attempt to set or test an operation-set bit that is not supported by a key's
channel will result in an appropriate run-time exception.

It is often necessary to associate some application-specific data with a
selection key, for example an object that represents the state of a
higher-level protocol and handles readiness notifications in order to
implement that protocol. Selection keys therefore support the
*attachment* of a single arbitrary object to a key. An object can be
attached via the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#attach(java.lang.Object)` method and then later retrieved via
the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#attachment()` method.

Selection keys are safe for use by multiple concurrent threads. The
operations of reading and writing the interest set will, in general, be
synchronized with certain operations of the selector. Exactly how this
synchronization is performed is implementation-dependent: In a naive
implementation, reading or writing the interest set may block indefinitely
if a selection operation is already in progress; in a high-performance
implementation, reading or writing the interest set may block briefly, if at
all. In any case, a selection operation will always use the interest-set
value that was current at the moment that the operation began.

<br />

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector.html`

### Constant Summary

|---|---|---|
| int | [OP_ACCEPT](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#OP_ACCEPT) | Operation-set bit for socket-accept operations. |
| int | [OP_CONNECT](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#OP_CONNECT) | Operation-set bit for socket-connect operations. |
| int | [OP_READ](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#OP_READ) | Operation-set bit for read operations. |
| int | [OP_WRITE](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#OP_WRITE) | Operation-set bit for write operations. |

### Protected Constructor Summary

|---|---|
|   | [SelectionKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#SelectionKey())() Constructs an instance of this class. |

### Public Method Summary

|---|---|
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [attach](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#attach(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) ob) Attaches the given object to this key. |
| final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [attachment](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#attachment())() Retrieves the current attachment. |
| abstract void | [cancel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#cancel())() Requests that the registration of this key's channel with its selector be cancelled. |
| abstract [SelectableChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html) | [channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#channel())() Returns the channel for which this key was created. |
| abstract int | [interestOps](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#interestOps())() Retrieves this key's interest set. |
| abstract [SelectionKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html) | [interestOps](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#interestOps(int))(int ops) Sets this key's interest set to the given value. |
| final boolean | [isAcceptable](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isAcceptable())() Tests whether this key's channel is ready to accept a new socket connection. |
| final boolean | [isConnectable](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isConnectable())() Tests whether this key's channel has either finished, or failed to finish, its socket-connection operation. |
| final boolean | [isReadable](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isReadable())() Tests whether this key's channel is ready for reading. |
| abstract boolean | [isValid](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isValid())() Tells whether or not this key is valid. |
| final boolean | [isWritable](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#isWritable())() Tests whether this key's channel is ready for writing. |
| abstract int | [readyOps](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#readyOps())() Retrieves this key's ready-operation set. |
| abstract [Selector](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector.html) | [selector](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#selector())() Returns the selector for which this key was created. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Constants

#### public static final int
**OP_ACCEPT**

Operation-set bit for socket-accept operations.

Suppose that a selection key's interest set contains
OP_ACCEPT at the start of a [selection operation](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector#selop). If the selector
detects that the corresponding server-socket channel is ready to accept
another connection, or has an error pending, then it will add
OP_ACCEPT to the key's ready set and add the key to its
selected-key set.

<br />

Constant Value: 16

#### public static final int
**OP_CONNECT**

Operation-set bit for socket-connect operations.

Suppose that a selection key's interest set contains
OP_CONNECT at the start of a [selection operation](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector#selop). If the selector
detects that the corresponding socket channel is ready to complete its
connection sequence, or has an error pending, then it will add
OP_CONNECT to the key's ready set and add the key to its
selected-key set.

<br />

Constant Value: 8

#### public static final int
**OP_READ**

Operation-set bit for read operations.

Suppose that a selection key's interest set contains
OP_READ at the start of a [selection operation](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector#selop). If the selector
detects that the corresponding channel is ready for reading, has reached
end-of-stream, has been remotely shut down for further reading, or has
an error pending, then it will add OP_READ to the key's
ready-operation set and add the key to its selected-key set.

<br />

Constant Value: 1

#### public static final int
**OP_WRITE**

Operation-set bit for write operations.

Suppose that a selection key's interest set contains
OP_WRITE at the start of a [selection operation](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector#selop). If the selector
detects that the corresponding channel is ready for writing, has been
remotely shut down for further writing, or has an error pending, then it
will add OP_WRITE to the key's ready set and add the key to its
selected-key set.

<br />

Constant Value: 4

## Protected Constructors

#### protected
**SelectionKey**
()

Constructs an instance of this class.

## Public Methods

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**attach**
([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) ob)

Attaches the given object to this key.

An attached object may later be retrieved via the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html#attachment()` method. Only one object may be attached at a time; invoking
this method causes any previous attachment to be discarded. The current
attachment may be discarded by attaching null.

<br />

##### Parameters

| ob | The object to be attached; may be null |
|---|---|

##### Returns

- The previously-attached object, if any, otherwise null

#### public final [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)
**attachment**
()

Retrieves the current attachment.

##### Returns

- The object currently attached to this key, or null if there is no attachment

#### public abstract void
**cancel**
()

Requests that the registration of this key's channel with its selector
be cancelled. Upon return the key will be invalid and will have been
added to its selector's cancelled-key set. The key will be removed from
all of the selector's key sets during the next selection operation.

If this key has already been cancelled then invoking this method has
no effect. Once cancelled, a key remains forever invalid.

This method may be invoked at any time. It synchronizes on the
selector's cancelled-key set, and therefore may block briefly if invoked
concurrently with a cancellation or selection operation involving the
same selector.

<br />

#### public abstract [SelectableChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectableChannel.html)
**channel**
()

Returns the channel for which this key was created. This method will
continue to return the channel even after the key is cancelled.

##### Returns

- This key's channel

#### public abstract int
**interestOps**
()

Retrieves this key's interest set.

It is guaranteed that the returned set will only contain operation
bits that are valid for this key's channel.

This method may be invoked at any time. Whether or not it blocks,
and for how long, is implementation-dependent.

<br />

##### Returns

- This key's interest set

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public abstract [SelectionKey](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/SelectionKey.html)
**interestOps**
(int ops)

Sets this key's interest set to the given value.

This method may be invoked at any time. Whether or not it blocks,
and for how long, is implementation-dependent.

<br />

##### Parameters

| ops | The new interest set |
|---|---|

##### Returns

- This selection key

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If a bit in the set does not correspond to an operation that is supported by this key's channel, that is, if `(ops & ~channel().validOps()) != 0` |
| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public final boolean
**isAcceptable**
()

Tests whether this key's channel is ready to accept a new socket
connection.

An invocation of this method of the form k.isAcceptable()
behaves in exactly the same way as the expression

>     k.readyOps() & OP_ACCEPT != 0
>      
If this key's channel does not support socket-accept operations then
this method always returns false.

<br />

##### Returns

- true if, and only if, `readyOps() & OP_ACCEPT` is nonzero

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public final boolean
**isConnectable**
()

Tests whether this key's channel has either finished, or failed to
finish, its socket-connection operation.

An invocation of this method of the form k.isConnectable()
behaves in exactly the same way as the expression

>     k.readyOps() & OP_CONNECT != 0
>      
If this key's channel does not support socket-connect operations
then this method always returns false.

<br />

##### Returns

- true if, and only if, `readyOps() & OP_CONNECT` is nonzero

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public final boolean
**isReadable**
()

Tests whether this key's channel is ready for reading.

An invocation of this method of the form k.isReadable()
behaves in exactly the same way as the expression

>     k.readyOps() & OP_READ != 0
>      
If this key's channel does not support read operations then this
method always returns false.

<br />

##### Returns

- true if, and only if, `readyOps() & OP_READ` is nonzero

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public abstract boolean
**isValid**
()

Tells whether or not this key is valid.

A key is valid upon creation and remains so until it is cancelled,
its channel is closed, or its selector is closed.

<br />

##### Returns

- true if, and only if, this key is valid

#### public final boolean
**isWritable**
()

Tests whether this key's channel is ready for writing.

An invocation of this method of the form k.isWritable()
behaves in exactly the same way as the expression

>     k.readyOps() & OP_WRITE != 0
>      
If this key's channel does not support write operations then this
method always returns false.

<br />

##### Returns

- true if, and only if, `readyOps() & OP_WRITE` is nonzero

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public abstract int
**readyOps**
()

Retrieves this key's ready-operation set.

It is guaranteed that the returned set will only contain operation
bits that are valid for this key's channel.

<br />

##### Returns

- This key's ready-operation set

##### Throws

| [CancelledKeyException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CancelledKeyException.html) | If this key has been cancelled |
|---|---|

#### public abstract [Selector](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Selector.html)
**selector**
()

Returns the selector for which this key was created. This method will
continue to return the selector even after the key is cancelled.

##### Returns

- This key's selector