SelectionKey.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* SelectionKey.java --
  2. Copyright (C) 2002, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.nio.channels;
  32. /**
  33. * @author Michael Koch
  34. * @since 1.4
  35. */
  36. public abstract class SelectionKey
  37. {
  38. public static final int OP_ACCEPT = 16;
  39. public static final int OP_CONNECT = 8;
  40. public static final int OP_READ = 1;
  41. public static final int OP_WRITE = 4;
  42. Object attached;
  43. /**
  44. * Initializes the selection key.
  45. */
  46. protected SelectionKey()
  47. {
  48. }
  49. /**
  50. * Attaches obj to the key and returns the old attached object.
  51. */
  52. public final synchronized Object attach(Object obj)
  53. {
  54. Object old = attached;
  55. attached = obj;
  56. return old;
  57. }
  58. /**
  59. * Returns the object attached to the key.
  60. */
  61. public final synchronized Object attachment()
  62. {
  63. return attached;
  64. }
  65. /**
  66. * Tests if the channel attached to this key is ready to accept
  67. * a new socket connection.
  68. *
  69. * @exception CancelledKeyException If this key has been cancelled
  70. */
  71. public final boolean isAcceptable()
  72. {
  73. return (readyOps() & OP_ACCEPT) != 0;
  74. }
  75. /**
  76. * Tests whether this key's channel has either finished,
  77. * or failed to finish, its socket-connection operation.
  78. *
  79. * @exception CancelledKeyException If this key has been cancelled
  80. */
  81. public final boolean isConnectable()
  82. {
  83. return (readyOps() & OP_CONNECT) != 0;
  84. }
  85. /**
  86. * Tests if the channel attached to the key is readable.
  87. *
  88. * @exception CancelledKeyException If this key has been cancelled
  89. */
  90. public final boolean isReadable()
  91. {
  92. return (readyOps() & OP_READ) != 0;
  93. }
  94. /**
  95. * Tests if the channel attached to the key is writable.
  96. *
  97. * @exception CancelledKeyException If this key has been cancelled
  98. */
  99. public final boolean isWritable()
  100. {
  101. return (readyOps() & OP_WRITE) != 0;
  102. }
  103. /**
  104. * Requests that the registration of this key's channel with
  105. * its selector be cancelled.
  106. */
  107. public abstract void cancel();
  108. /**
  109. * return the channel attached to the key.
  110. */
  111. public abstract SelectableChannel channel();
  112. /**
  113. * Returns the key's interest set.
  114. *
  115. * @exception CancelledKeyException If this key has been cancelled
  116. */
  117. public abstract int interestOps();
  118. /**
  119. * Sets this key's interest set to the given value.
  120. *
  121. * @exception CancelledKeyException If this key has been cancelled
  122. * @exception IllegalArgumentException If a bit in the set does not
  123. * correspond to an operation that is supported by this key's channel,
  124. * that is, if set & ~(channel().validOps()) != 0
  125. */
  126. public abstract SelectionKey interestOps(int ops);
  127. /**
  128. * Tells whether or not this key is valid.
  129. */
  130. public abstract boolean isValid();
  131. /**
  132. * Retrieves this key's ready-operation set.
  133. *
  134. * @exception CancelledKeyException If this key has been cancelled
  135. */
  136. public abstract int readyOps();
  137. /**
  138. * Returns the selector for which this key was created.
  139. */
  140. public abstract Selector selector();
  141. }