37 |
|
|
38 |
package java.nio.channels; |
package java.nio.channels; |
39 |
|
|
40 |
|
/** |
41 |
|
* @author Michael Koch |
42 |
|
* @since 1.4 |
43 |
|
*/ |
44 |
public abstract class SelectionKey |
public abstract class SelectionKey |
45 |
{ |
{ |
46 |
public static final int OP_ACCEPT = 1<<0; |
public static final int OP_ACCEPT = 1<<0; |
49 |
public static final int OP_WRITE = 1<<3; |
public static final int OP_WRITE = 1<<3; |
50 |
|
|
51 |
Object attached; |
Object attached; |
52 |
|
|
53 |
protected SelectionKey() |
/** |
54 |
|
* Initializes the selection key. |
55 |
|
*/ |
56 |
|
protected SelectionKey () |
57 |
{ |
{ |
58 |
} |
} |
59 |
|
|
60 |
public final Object attach(Object obj) |
/** |
61 |
|
* Attaches obj to the key and returns the old attached object. |
62 |
|
*/ |
63 |
|
public final Object attach (Object obj) |
64 |
{ |
{ |
65 |
Object old = attached; |
Object old = attached; |
66 |
attached = obj; |
attached = obj; |
67 |
return old; |
return old; |
68 |
} |
} |
69 |
|
|
70 |
public final Object attachment() |
/** |
71 |
|
* Returns the object attached to the key. |
72 |
|
*/ |
73 |
|
public final Object attachment () |
74 |
{ |
{ |
75 |
return attached; |
return attached; |
76 |
} |
} |
77 |
|
|
78 |
public final boolean isAcceptable() |
/** |
79 |
|
* Tests if the channel attached to this key is ready to accept |
80 |
|
* a new socket connection. |
81 |
|
* |
82 |
|
* @exception CancelledKeyException If this key has been cancelled |
83 |
|
*/ |
84 |
|
public final boolean isAcceptable () |
85 |
{ |
{ |
86 |
return (readyOps() & OP_ACCEPT) != 0; |
return (readyOps () & OP_ACCEPT) != 0; |
87 |
} |
} |
88 |
|
|
89 |
public final boolean isConnectable() |
/** |
90 |
|
* Tests whether this key's channel has either finished, |
91 |
|
* or failed to finish, its socket-connection operation. |
92 |
|
* |
93 |
|
* @exception CancelledKeyException If this key has been cancelled |
94 |
|
*/ |
95 |
|
public final boolean isConnectable () |
96 |
{ |
{ |
97 |
return (readyOps() & OP_CONNECT) != 0; |
return (readyOps () & OP_CONNECT) != 0; |
98 |
} |
} |
99 |
|
|
100 |
public final boolean isReadable() |
/** |
101 |
|
* Tests if the channel attached to the key is readable. |
102 |
|
* |
103 |
|
* @exception CancelledKeyException If this key has been cancelled |
104 |
|
*/ |
105 |
|
public final boolean isReadable () |
106 |
{ |
{ |
107 |
return (readyOps() & OP_READ) != 0; |
return (readyOps () & OP_READ) != 0; |
108 |
} |
} |
109 |
|
|
110 |
public final boolean isWritable() |
/** |
111 |
|
* Tests if the channel attached to the key is writable. |
112 |
|
* |
113 |
|
* @exception CancelledKeyException If this key has been cancelled |
114 |
|
*/ |
115 |
|
public final boolean isWritable () |
116 |
{ |
{ |
117 |
return (readyOps() & OP_WRITE) != 0; |
return (readyOps () & OP_WRITE) != 0; |
118 |
} |
} |
119 |
|
|
120 |
public abstract void cancel(); |
/** |
121 |
|
* Requests that the registration of this key's channel with |
122 |
public abstract SelectableChannel channel(); |
* its selector be cancelled. |
123 |
|
*/ |
124 |
public abstract int interestOps(); |
public abstract void cancel (); |
|
|
|
|
public abstract SelectionKey interestOps(int ops); |
|
|
|
|
|
public abstract boolean isValid(); |
|
125 |
|
|
126 |
public abstract int readyOps(); |
/** |
127 |
|
* return the channel attached to the key. |
128 |
public abstract Selector selector(); |
*/ |
129 |
|
public abstract SelectableChannel channel (); |
130 |
|
|
131 |
|
/** |
132 |
|
* Returns the key's interest set. |
133 |
|
* |
134 |
|
* @exception CancelledKeyException If this key has been cancelled |
135 |
|
*/ |
136 |
|
public abstract int interestOps (); |
137 |
|
|
138 |
|
/** |
139 |
|
* Sets this key's interest set to the given value. |
140 |
|
* |
141 |
|
* @exception CancelledKeyException If this key has been cancelled |
142 |
|
* @exception IllegalArgumentException If a bit in the set does not |
143 |
|
* correspond to an operation that is supported by this key's channel, |
144 |
|
* that is, if set & ~(channel().validOps()) != 0 |
145 |
|
*/ |
146 |
|
public abstract SelectionKey interestOps (int ops); |
147 |
|
|
148 |
|
/** |
149 |
|
* Tells whether or not this key is valid. |
150 |
|
*/ |
151 |
|
public abstract boolean isValid (); |
152 |
|
|
153 |
|
/** |
154 |
|
* Retrieves this key's ready-operation set. |
155 |
|
* |
156 |
|
* @exception CancelledKeyException If this key has been cancelled |
157 |
|
*/ |
158 |
|
public abstract int readyOps (); |
159 |
|
|
160 |
|
/** |
161 |
|
* Returns the selector for which this key was created. |
162 |
|
*/ |
163 |
|
public abstract Selector selector (); |
164 |
} |
} |