FileDocCategorySizeDatePackage
SelectionKey.javaAPI DocAndroid 1.5 API8447Wed May 06 22:41:04 BST 2009java.nio.channels

SelectionKey

public abstract class SelectionKey extends Object
A {@code 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 ({@code OP_ACCEPT}), connecting with a socket ({@code OP_CONNECT}), reading ({@code OP_READ}) and writing ({@code OP_WRITE}).

Interest set

The interest set is an operation set that defines the operations that a {@link SelectableChannel channel} is interested in performing.

Ready set

The ready set is an operation set that shows the operations that a {@code channel} is ready to execute.
since
Android 1.0

Fields Summary
public static final int
OP_ACCEPT
Interest set mask bit for socket-accept operations.
public static final int
OP_CONNECT
Interest set mask bit for socket-connect operations.
public static final int
OP_READ
Interesting operation mask bit for read operations.
public static final int
OP_WRITE
Interest set mask bit for write operations.
private volatile Object
attachment
Constructors Summary
protected SelectionKey()
Constructs a new {@code SelectionKey}.

since
Android 1.0


                  
      
        super();
    
Methods Summary
public final java.lang.Objectattach(java.lang.Object anObject)
Attaches an object to this key. It is acceptable to attach {@code null}, this discards the old attachment.

param
anObject the object to attach, or {@code null} to discard the current attachment.
return
the last attached object or {@code null} if no object has been attached.
since
Android 1.0

        Object oldAttachment = attachment;
        attachment = anObject;
        return oldAttachment;
    
public final java.lang.Objectattachment()
Gets the attached object.

return
the attached object or {@code null} if no object has been attached.
since
Android 1.0

        return attachment;
    
public abstract voidcancel()
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.

since
Android 1.0

public abstract java.nio.channels.SelectableChannelchannel()
Gets the channel of this key.

return
the channel of this key.
since
Android 1.0

public abstract intinterestOps()
Gets this key's {@link SelectionKey interest set}. The returned set has only those bits set that are valid for this key's channel.

return
the interest set of this key.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

public abstract java.nio.channels.SelectionKeyinterestOps(int operations)
Sets the {@link SelectionKey interest set} for this key.

param
operations the new interest set.
return
this key.
throws
IllegalArgumentException if a bit in {@code operations} is not in the set of {@link SelectableChannel#validOps() valid operations} of this key's channel.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

public final booleanisAcceptable()
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 {@code (readyOps() & OP_ACCEPT) == OP_ACCEPT}.

return
{@code true} if the channel is interested in the accept operation and is ready to accept new connections, {@code false} otherwise.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

        return (readyOps() & OP_ACCEPT) == OP_ACCEPT;
    
public final booleanisConnectable()
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 {@code (readyOps() & OP_CONNECT) == OP_CONNECT}.

return
{@code true} if the channel is interested in the connect operation and is ready to connect, {@code false} otherwise.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

        return (readyOps() & OP_CONNECT) == OP_CONNECT;
    
public final booleanisReadable()
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 {@code (readyOps() & OP_READ) == OP_READ}.

return
{@code true} if the channel is interested in the read operation and is ready to read, {@code false} otherwise.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

        return (readyOps() & OP_READ) == OP_READ;
    
public abstract booleanisValid()
Indicates whether this key is valid. A key is valid as long as it has not been canceled.

return
{@code true} if this key has not been canceled, {@code false} otherwise.
since
Android 1.0

public final booleanisWritable()
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 {@code (readyOps() & OP_WRITE) == OP_WRITE}.

return
{@code true} if the channel is interested in the wrie operation and is ready to write, {@code false} otherwise.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

        return (readyOps() & OP_WRITE) == OP_WRITE;
    
public abstract intreadyOps()
Gets the set of operations that are ready. The returned set has only those bits set that are valid for this key's channel.

return
the operations for which this key's channel is ready.
throws
CancelledKeyException if the key has already been canceled.
since
Android 1.0

public abstract java.nio.channels.Selectorselector()
Gets the selector for which this key's channel is registered.

return
the related selector.
since
Android 1.0