Java Doc for SelectionKey.java in  » 6.0-JDK-Core » io-nio » java » nio » channels » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.nio.channels 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   java.nio.channels.SelectionKey

All known Subclasses:   java.nio.channels.spi.AbstractSelectionKey,
SelectionKey
abstract public class SelectionKey (Code)
A token representing the registration of a SelectableChannel with a Selector .

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 SelectionKey.cancel 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 for removal during the next selection operation. The validity of a key may be tested by invoking its SelectionKey.isValid 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.

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 SelectableChannel defines an SelectableChannel.validOps 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 SelectionKey.attach attach method and then later retrieved via the SelectionKey.attachment() 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.


author:
   Mark Reinhold
author:
   JSR-51 Expert Group
version:
   1.33, 07/05/05
since:
   1.4
See Also:   SelectableChannel
See Also:   Selector


Field Summary
final public static  intOP_ACCEPT
     Operation-set bit for socket-accept operations.
final public static  intOP_CONNECT
     Operation-set bit for socket-connect operations.
final public static  intOP_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.

final public static  intOP_WRITE
     Operation-set bit for write operations.

Constructor Summary
protected  SelectionKey()
     Constructs an instance of this class.

Method Summary
final public  Objectattach(Object ob)
     Attaches the given object to this key.

An attached object may later be retrieved via the SelectionKey.attachment()attachment method.

final public  Objectattachment()
     Retrieves the current attachment.
abstract public  voidcancel()
     Requests that the registration of this key's channel with its selector be cancelled.
abstract public  SelectableChannelchannel()
     Returns the channel for which this key was created.
abstract public  intinterestOps()
     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.

abstract public  SelectionKeyinterestOps(int ops)
     Sets this key's interest set to the given value.

This method may be invoked at any time.

final public  booleanisAcceptable()
     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.

final public  booleanisConnectable()
     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.

final public  booleanisReadable()
     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.

abstract public  booleanisValid()
     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.

final public  booleanisWritable()
     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.

abstract public  intreadyOps()
     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.

abstract public  Selectorselector()
     Returns the selector for which this key was created.

Field Detail
OP_ACCEPT
final public static int OP_ACCEPT(Code)
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. 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.




OP_CONNECT
final public static int OP_CONNECT(Code)
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. 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.




OP_READ
final public static int OP_READ(Code)
Operation-set bit for read operations.

Suppose that a selection key's interest set contains OP_READ at the start of a selection operation. 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.




OP_WRITE
final public static int OP_WRITE(Code)
Operation-set bit for write operations.

Suppose that a selection key's interest set contains OP_WRITE at the start of a selection operation. 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.





Constructor Detail
SelectionKey
protected SelectionKey()(Code)
Constructs an instance of this class.




Method Detail
attach
final public Object attach(Object ob)(Code)
Attaches the given object to this key.

An attached object may later be retrieved via the SelectionKey.attachment()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.


Parameters:
  ob - The object to be attached; may be null The previously-attached object, if any,otherwise null



attachment
final public Object attachment()(Code)
Retrieves the current attachment.

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



cancel
abstract public void cancel()(Code)
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.




channel
abstract public SelectableChannel channel()(Code)
Returns the channel for which this key was created. This method will continue to return the channel even after the key is cancelled.

This key's channel



interestOps
abstract public int interestOps()(Code)
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.

This key's interest set
throws:
  CancelledKeyException - If this key has been cancelled



interestOps
abstract public SelectionKey interestOps(int ops)(Code)
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.


Parameters:
  ops - The new interest set This selection key
throws:
  IllegalArgumentException - If a bit in the set does not correspond to an operation thatis supported by this key's channel, that is, ifset & ~(channel().validOps()) != 0
throws:
  CancelledKeyException - If this key has been cancelled



isAcceptable
final public boolean isAcceptable()(Code)
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.

true if, and only if,readyOps() & OP_ACCEPTis nonzero
throws:
  CancelledKeyException - If this key has been cancelled



isConnectable
final public boolean isConnectable()(Code)
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.

true if, and only if,readyOps() & OP_CONNECTis nonzero
throws:
  CancelledKeyException - If this key has been cancelled



isReadable
final public boolean isReadable()(Code)
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.

true if, and only if,readyOps() & OP_READ isnonzero
throws:
  CancelledKeyException - If this key has been cancelled



isValid
abstract public boolean isValid()(Code)
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.

true if, and only if, this key is valid



isWritable
final public boolean isWritable()(Code)
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.

true if, and only if,readyOps() & OP_WRITEis nonzero
throws:
  CancelledKeyException - If this key has been cancelled



readyOps
abstract public int readyOps()(Code)
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.

This key's ready-operation set
throws:
  CancelledKeyException - If this key has been cancelled



selector
abstract public Selector selector()(Code)
Returns the selector for which this key was created. This method will continue to return the selector even after the key is cancelled.

This key's selector



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.