1. /*
  2. * @(#)SelectionKey.java 1.23 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.nio.channels;
  8. import java.io.IOException;
  9. /**
  10. * A token representing the registration of a {@link SelectableChannel} with a
  11. * {@link Selector}.
  12. *
  13. * <p> A selection key is created each time a channel is registered with a
  14. * selector. A key remains valid until it is <i>cancelled</i> by invoking its
  15. * {@link #cancel cancel} method, by closing its channel, or by closing its
  16. * selector. Cancelling a key does not immediately remove it from its
  17. * selector; it is instead added to the selector's <a
  18. * href="Selector.html#ks"><i>cancelled-key set</i></a> for removal during the
  19. * next selection operation. The validity of a key may be tested by invoking
  20. * its {@link #isValid isValid} method.
  21. *
  22. * <a name="opsets">
  23. *
  24. * <p> A selection key contains two <i>operation sets</i> represented as
  25. * integer values. Each bit of an operation set denotes a category of
  26. * selectable operations that are supported by the key's channel.
  27. *
  28. * <ul>
  29. *
  30. * <li><p> The <i>interest set</i> determines which operation categories will
  31. * be tested for readiness the next time one of the selector's selection
  32. * methods is invoked. The interest set is initialized with the value given
  33. * when the key is created; it may later be changed via the {@link
  34. * #interestOps(int)} method. </p></li>
  35. *
  36. * <li><p> The <i>ready set</i> identifies the operation categories for which
  37. * the key's channel has been detected to be ready by the key's selector.
  38. * The ready set is initialized to zero when the key is created; it may later
  39. * be updated by the selector during a selection operation, but it cannot be
  40. * updated directly. </p></li>
  41. *
  42. * </ul>
  43. *
  44. * <p> That a selection key's ready set indicates that its channel is ready for
  45. * some operation category is a hint, but not a guarantee, that an operation in
  46. * such a category may be performed by a thread without causing the thread to
  47. * block. A ready set is most likely to be accurate immediately after the
  48. * completion of a selection operation. It is likely to be made inaccurate by
  49. * external events and by I/O operations that are invoked upon the
  50. * corresponding channel.
  51. *
  52. * <p> This class defines all known operation-set bits, but precisely which
  53. * bits are supported by a given channel depends upon the type of the channel.
  54. * Each subclass of {@link SelectableChannel} defines an {@link
  55. * SelectableChannel#validOps() validOps()} method which returns a set
  56. * identifying just those operations that are supported by the channel. An
  57. * attempt to set or test an operation-set bit that is not supported by a key's
  58. * channel will result in an appropriate run-time exception.
  59. *
  60. * <p> It is often necessary to associate some application-specific data with a
  61. * selection key, for example an object that represents the state of a
  62. * higher-level protocol and handles readiness notifications in order to
  63. * implement that protocol. Selection keys therefore support the
  64. * <i>attachment</i> of a single arbitrary object to a key. An object can be
  65. * attached via the {@link #attach attach} method and then later retrieved via
  66. * the {@link #attachment attachment} method.
  67. *
  68. * <p> Selection keys are safe for use by multiple concurrent threads. The
  69. * operations of reading and writing the interest set will, in general, be
  70. * synchronized with certain operations of the selector. Exactly how this
  71. * synchronization is performed is implementation-dependent: In a naive
  72. * implementation, reading or writing the interest set may block indefinitely
  73. * if a selection operation is already in progress; in a high-performance
  74. * implementation, reading or writing the interest set may block briefly, if at
  75. * all. In any case, a selection operation will always use the interest-set
  76. * value that was current at the moment that the operation began. </p>
  77. *
  78. *
  79. * @author Mark Reinhold
  80. * @author JSR-51 Expert Group
  81. * @version 1.23, 03/01/23
  82. * @since 1.4
  83. *
  84. * @see SelectableChannel
  85. * @see Selector
  86. */
  87. public abstract class SelectionKey {
  88. /**
  89. * Constructs an instance of this class.
  90. */
  91. protected SelectionKey() { }
  92. // -- Channel and selector operations --
  93. /**
  94. * Returns the channel for which this key was created. This method will
  95. * continue to return the channel even after the key is cancelled. </p>
  96. *
  97. * @return This key's channel
  98. */
  99. public abstract SelectableChannel channel();
  100. /**
  101. * Returns the selector for which this key was created. This method will
  102. * continue to return the selector even after the key is cancelled. </p>
  103. *
  104. * @return This key's selector
  105. */
  106. public abstract Selector selector();
  107. /**
  108. * Tells whether or not this key is valid.
  109. *
  110. * <p> A key is valid upon creation and remains so until it is cancelled,
  111. * its channel is closed, or its selector is closed. </p>
  112. *
  113. * @return <tt>true</tt> if, and only if, this key is valid
  114. */
  115. public abstract boolean isValid();
  116. /**
  117. * Requests that the registration of this key's channel with its selector
  118. * be cancelled. Upon return the key will be invalid and will have been
  119. * added to its selector's cancelled-key set. The key will be removed from
  120. * all of the selector's key sets during the next selection operation.
  121. *
  122. * <p> If this key has already been cancelled then invoking this method has
  123. * no effect. Once cancelled, a key remains forever invalid. </p>
  124. *
  125. * <p> This method may be invoked at any time. It synchronizes on the
  126. * selector's cancelled-key set, and therefore may block briefly if invoked
  127. * concurrently with a cancellation or selection operation involving the
  128. * same selector. </p>
  129. */
  130. public abstract void cancel();
  131. // -- Operation-set accessors --
  132. /**
  133. * Retrieves this key's interest set.
  134. *
  135. * <p> It is guaranteed that the returned set will only contain operation
  136. * bits that are valid for this key's channel.
  137. *
  138. * <p> This method may be invoked at any time. Whether or not it blocks,
  139. * and for how long, is implementation-dependent. </p>
  140. *
  141. * @return This key's interest set
  142. *
  143. * @throws CancelledKeyException
  144. * If this key has been cancelled
  145. */
  146. public abstract int interestOps();
  147. /**
  148. * Sets this key's interest set to the given value.
  149. *
  150. * <p> This method may be invoked at any time. Whether or not it blocks,
  151. * and for how long, is implementation-dependent. </p>
  152. *
  153. * @param ops The new interest set
  154. *
  155. * @return This selection key
  156. *
  157. * @throws IllegalArgumentException
  158. * If a bit in the set does not correspond to an operation that
  159. * is supported by this key's channel, that is, if
  160. * <tt>set & ~(channel().validOps()) != 0</tt>
  161. *
  162. * @throws CancelledKeyException
  163. * If this key has been cancelled
  164. */
  165. public abstract SelectionKey interestOps(int ops);
  166. /**
  167. * Retrieves this key's ready-operation set.
  168. *
  169. * <p> It is guaranteed that the returned set will only contain operation
  170. * bits that are valid for this key's channel. </p>
  171. *
  172. * @return This key's ready-operation set
  173. *
  174. * @throws CancelledKeyException
  175. * If this key has been cancelled
  176. */
  177. public abstract int readyOps();
  178. // -- Operation bits and bit-testing convenience methods --
  179. /**
  180. * Operation-set bit for read operations.
  181. *
  182. * <p> Suppose that a selection key's interest set contains
  183. * <tt>OP_READ</tt> at the start of a <a
  184. * href="Selector.html#selop">selection operation</a>. If the selector
  185. * detects that the corresponding channel is ready for reading, has reached
  186. * end-of-stream, has been remotely shut down for further reading, or has
  187. * an error pending, then it will add <tt>OP_READ</tt> to the key's
  188. * ready-operation set and add the key to its selected-key set. </p>
  189. */
  190. public static final int OP_READ = 1 << 0;
  191. /**
  192. * Operation-set bit for write operations. </p>
  193. *
  194. * <p> Suppose that a selection key's interest set contains
  195. * <tt>OP_WRITE</tt> at the start of a <a
  196. * href="Selector.html#selop">selection operation</a>. If the selector
  197. * detects that the corresponding channel is ready for writing, has been
  198. * remotely shut down for further writing, or has an error pending, then it
  199. * will add <tt>OP_WRITE</tt> to the key's ready set and add the key to its
  200. * selected-key set. </p>
  201. */
  202. public static final int OP_WRITE = 1 << 2;
  203. /**
  204. * Operation-set bit for socket-connect operations. </p>
  205. *
  206. * <p> Suppose that a selection key's interest set contains
  207. * <tt>OP_CONNECT</tt> at the start of a <a
  208. * href="Selector.html#selop">selection operation</a>. If the selector
  209. * detects that the corresponding socket channel is ready to complete its
  210. * connection sequence, or has an error pending, then it will add
  211. * <tt>OP_CONNECT</tt> to the key's ready set and add the key to its
  212. * selected-key set. </p>
  213. */
  214. public static final int OP_CONNECT = 1 << 3;
  215. /**
  216. * Operation-set bit for socket-accept operations. </p>
  217. *
  218. * <p> Suppose that a selection key's interest set contains
  219. * <tt>OP_ACCEPT</tt> at the start of a <a
  220. * href="Selector.html#selop">selection operation</a>. If the selector
  221. * detects that the corresponding server-socket channel is ready to accept
  222. * another connection, or has an error pending, then it will add
  223. * <tt>OP_ACCEPT</tt> to the key's ready set and add the key to its
  224. * selected-key set. </p>
  225. */
  226. public static final int OP_ACCEPT = 1 << 4;
  227. /**
  228. * Tests whether this key's channel is ready for reading.
  229. *
  230. * <p> An invocation of this method of the form <tt>k.isReadable()</tt>
  231. * behaves in exactly the same way as the expression
  232. *
  233. * <blockquote><pre>
  234. * k.readyOps() & OP_READ != 0</pre></blockquote>
  235. *
  236. * <p> If this key's channel does not support read operations then this
  237. * method always returns <tt>false</tt>. </p>
  238. *
  239. * @return <tt>true</tt> if, and only if,
  240. * <tt>readyOps()</tt> <tt>&</tt> <tt>OP_READ</tt> is
  241. * nonzero
  242. *
  243. * @throws CancelledKeyException
  244. * If this key has been cancelled
  245. */
  246. public final boolean isReadable() {
  247. return (readyOps() & OP_READ) != 0;
  248. }
  249. /**
  250. * Tests whether this key's channel is ready for writing.
  251. *
  252. * <p> An invocation of this method of the form <tt>k.isWritable()</tt>
  253. * behaves in exactly the same way as the expression
  254. *
  255. * <blockquote><pre>
  256. * k.readyOps() & OP_WRITE != 0</pre></blockquote>
  257. *
  258. * <p> If this key's channel does not support write operations then this
  259. * method always returns <tt>false</tt>. </p>
  260. *
  261. * @return <tt>true</tt> if, and only if,
  262. * <tt>readyOps()</tt> <tt>&</tt> <tt>OP_WRITE</tt>
  263. * is nonzero
  264. *
  265. * @throws CancelledKeyException
  266. * If this key has been cancelled
  267. */
  268. public final boolean isWritable() {
  269. return (readyOps() & OP_WRITE) != 0;
  270. }
  271. /**
  272. * Tests whether this key's channel has either finished, or failed to
  273. * finish, its socket-connection operation.
  274. *
  275. * <p> An invocation of this method of the form <tt>k.isConnectable()</tt>
  276. * behaves in exactly the same way as the expression
  277. *
  278. * <blockquote><pre>
  279. * k.readyOps() & OP_CONNECT != 0</pre></blockquote>
  280. *
  281. * <p> If this key's channel does not support socket-connect operations
  282. * then this method always returns <tt>false</tt>. </p>
  283. *
  284. * @return <tt>true</tt> if, and only if,
  285. * <tt>readyOps()</tt> <tt>&</tt> <tt>OP_CONNECT</tt>
  286. * is nonzero
  287. *
  288. * @throws CancelledKeyException
  289. * If this key has been cancelled
  290. */
  291. public final boolean isConnectable() {
  292. return (readyOps() & OP_CONNECT) != 0;
  293. }
  294. /**
  295. * Tests whether this key's channel is ready to accept a new socket
  296. * connection.
  297. *
  298. * <p> An invocation of this method of the form <tt>k.isAcceptable()</tt>
  299. * behaves in exactly the same way as the expression
  300. *
  301. * <blockquote><pre>
  302. * k.readyOps() & OP_ACCEPT != 0</pre></blockquote>
  303. *
  304. * <p> If this key's channel does not support socket-accept operations then
  305. * this method always returns <tt>false</tt>. </p>
  306. *
  307. * @return <tt>true</tt> if, and only if,
  308. * <tt>readyOps()</tt> <tt>&</tt> <tt>OP_ACCEPT</tt>
  309. * is nonzero
  310. *
  311. * @throws CancelledKeyException
  312. * If this key has been cancelled
  313. */
  314. public final boolean isAcceptable() {
  315. return (readyOps() & OP_ACCEPT) != 0;
  316. }
  317. // -- Attachments --
  318. private volatile Object attachment = null;
  319. /**
  320. * Attaches the given object to this key.
  321. *
  322. * <p> An attached object may later be retrieved via the {@link #attachment
  323. * attachment} method. Only one object may be attached at a time; invoking
  324. * this method causes any previous attachment to be discarded. The current
  325. * attachment may be discarded by attaching <tt>null</tt>. </p>
  326. *
  327. * @param ob
  328. * The object to be attached; may be <tt>null</tt>
  329. *
  330. * @return The previously-attached object, if any,
  331. * otherwise <tt>null</tt>
  332. */
  333. public final Object attach(Object ob) {
  334. Object a = attachment;
  335. attachment = ob;
  336. return a;
  337. }
  338. /**
  339. * Retrieves the current attachment. </p>
  340. *
  341. * @return The object currently attached to this key,
  342. * or <tt>null</tt> if there is no attachment
  343. */
  344. public final Object attachment() {
  345. return attachment;
  346. }
  347. }