1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.nio.channels; 27 28 import java.io.IOException; 29 import java.nio.channels.spi.AbstractInterruptibleChannel; 30 import java.nio.channels.spi.SelectorProvider; 31 32 33 /** 34 * A channel that can be multiplexed via a {@link Selector}. 35 * 36 * <p> In order to be used with a selector, an instance of this class must 37 * first be <i>registered</i> via the {@link #register(Selector,int,Object) 38 * register} method. This method returns a new {@link SelectionKey} object 39 * that represents the channel's registration with the selector. 40 * 41 * <p> Once registered with a selector, a channel remains registered until it 42 * is <i>deregistered</i>. This involves deallocating whatever resources were 43 * allocated to the channel by the selector. 44 * 45 * <p> A channel cannot be deregistered directly; instead, the key representing 46 * its registration must be <i>cancelled</i>. Cancelling a key requests that 47 * the channel be deregistered during the selector's next selection operation. 48 * A key may be cancelled explicitly by invoking its {@link 49 * SelectionKey#cancel() cancel} method. All of a channel's keys are cancelled 50 * implicitly when the channel is closed, whether by invoking its {@link 51 * Channel#close close} method or by interrupting a thread blocked in an I/O 52 * operation upon the channel. 53 * 54 * <p> If the selector itself is closed then the channel will be deregistered, 55 * and the key representing its registration will be invalidated, without 56 * further delay. 57 * 58 * <p> A channel may be registered at most once with any particular selector. 59 * 60 * <p> Whether or not a channel is registered with one or more selectors may be 61 * determined by invoking the {@link #isRegistered isRegistered} method. 62 * 63 * <p> Selectable channels are safe for use by multiple concurrent 64 * threads. </p> 65 * 66 * 67 * <a id="bm"></a> 68 * <h2>Blocking mode</h2> 69 * 70 * A selectable channel is either in <i>blocking</i> mode or in 71 * <i>non-blocking</i> mode. In blocking mode, every I/O operation invoked 72 * upon the channel will block until it completes. In non-blocking mode an I/O 73 * operation will never block and may transfer fewer bytes than were requested 74 * or possibly no bytes at all. The blocking mode of a selectable channel may 75 * be determined by invoking its {@link #isBlocking isBlocking} method. 76 * 77 * <p> Newly-created selectable channels are always in blocking mode. 78 * Non-blocking mode is most useful in conjunction with selector-based 79 * multiplexing. A channel must be placed into non-blocking mode before being 80 * registered with a selector, and may not be returned to blocking mode until 81 * it has been deregistered. 82 * 83 * 84 * @author Mark Reinhold 85 * @author JSR-51 Expert Group 86 * @since 1.4 87 * 88 * @see SelectionKey 89 * @see Selector 90 */ 91 92 public abstract class SelectableChannel 93 extends AbstractInterruptibleChannel 94 implements Channel 95 { 96 97 /** 98 * Initializes a new instance of this class. 99 */ SelectableChannel()100 protected SelectableChannel() { } 101 102 /** 103 * Returns the provider that created this channel. 104 * 105 * @return The provider that created this channel 106 */ provider()107 public abstract SelectorProvider provider(); 108 109 /** 110 * Returns an <a href="SelectionKey.html#opsets">operation set</a> 111 * identifying this channel's supported operations. The bits that are set 112 * in this integer value denote exactly the operations that are valid for 113 * this channel. This method always returns the same value for a given 114 * concrete channel class. 115 * 116 * @return The valid-operation set 117 */ validOps()118 public abstract int validOps(); 119 120 /** 121 * Tells whether or not this channel is currently registered with any 122 * selectors. A newly-created channel is not registered. 123 * 124 * <p> Due to the inherent delay between key cancellation and channel 125 * deregistration, a channel may remain registered for some time after all 126 * of its keys have been cancelled. A channel may also remain registered 127 * for some time after it is closed. </p> 128 * 129 * @return {@code true} if, and only if, this channel is registered 130 */ isRegistered()131 public abstract boolean isRegistered(); 132 133 /** 134 * Retrieves the key representing the channel's registration with the given 135 * selector. 136 * 137 * @param sel 138 * The selector 139 * 140 * @return The key returned when this channel was last registered with the 141 * given selector, or {@code null} if this channel is not 142 * currently registered with that selector 143 */ keyFor(Selector sel)144 public abstract SelectionKey keyFor(Selector sel); 145 146 /** 147 * Registers this channel with the given selector, returning a selection 148 * key. 149 * 150 * <p> If this channel is currently registered with the given selector then 151 * the selection key representing that registration is returned. The key's 152 * interest set will have been changed to {@code ops}, as if by invoking 153 * the {@link SelectionKey#interestOps(int) interestOps(int)} method. If 154 * the {@code att} argument is not {@code null} then the key's attachment 155 * will have been set to that value. A {@link CancelledKeyException} will 156 * be thrown if the key has already been cancelled. 157 * 158 * <p> Otherwise this channel has not yet been registered with the given 159 * selector, so it is registered and the resulting new key is returned. 160 * The key's initial interest set will be {@code ops} and its attachment 161 * will be {@code att}. 162 * 163 * <p> This method may be invoked at any time. If this method is invoked 164 * while a selection operation is in progress then it has no effect upon 165 * that operation; the new registration or change to the key's interest set 166 * will be seen by the next selection operation. If this method is invoked 167 * while an invocation of {@link #configureBlocking(boolean) configureBlocking} 168 * is in progress then it will block until the channel's blocking mode has 169 * been adjusted. 170 * 171 * <p> If this channel is closed while this operation is in progress then 172 * the key returned by this method will have been cancelled and will 173 * therefore be invalid. </p> 174 * 175 * @param sel 176 * The selector with which this channel is to be registered 177 * 178 * @param ops 179 * The interest set for the resulting key 180 * 181 * @param att 182 * The attachment for the resulting key; may be {@code null} 183 * 184 * @throws ClosedChannelException 185 * If this channel is closed 186 * 187 * @throws ClosedSelectorException 188 * If the selector is closed 189 * 190 * @throws IllegalBlockingModeException 191 * If this channel is in blocking mode 192 * 193 * @throws IllegalSelectorException 194 * If this channel was not created by the same provider 195 * as the given selector 196 * 197 * @throws CancelledKeyException 198 * If this channel is currently registered with the given selector 199 * but the corresponding key has already been cancelled 200 * 201 * @throws IllegalArgumentException 202 * If a bit in the {@code ops} set does not correspond to an 203 * operation that is supported by this channel, that is, if 204 * {@code set & ~validOps() != 0} 205 * 206 * @return A key representing the registration of this channel with 207 * the given selector 208 */ register(Selector sel, int ops, Object att)209 public abstract SelectionKey register(Selector sel, int ops, Object att) 210 throws ClosedChannelException; 211 212 /** 213 * Registers this channel with the given selector, returning a selection 214 * key. 215 * 216 * <p> An invocation of this convenience method of the form 217 * 218 * <blockquote>{@code sc.register(sel, ops)}</blockquote> 219 * 220 * behaves in exactly the same way as the invocation 221 * 222 * <blockquote>{@code sc.}{@link 223 * #register(java.nio.channels.Selector,int,java.lang.Object) 224 * register(sel, ops, null)}</blockquote> 225 * 226 * @param sel 227 * The selector with which this channel is to be registered 228 * 229 * @param ops 230 * The interest set for the resulting key 231 * 232 * @throws ClosedChannelException 233 * If this channel is closed 234 * 235 * @throws ClosedSelectorException 236 * If the selector is closed 237 * 238 * @throws IllegalBlockingModeException 239 * If this channel is in blocking mode 240 * 241 * @throws IllegalSelectorException 242 * If this channel was not created by the same provider 243 * as the given selector 244 * 245 * @throws CancelledKeyException 246 * If this channel is currently registered with the given selector 247 * but the corresponding key has already been cancelled 248 * 249 * @throws IllegalArgumentException 250 * If a bit in {@code ops} does not correspond to an operation 251 * that is supported by this channel, that is, if {@code set & 252 * ~validOps() != 0} 253 * 254 * @return A key representing the registration of this channel with 255 * the given selector 256 */ register(Selector sel, int ops)257 public final SelectionKey register(Selector sel, int ops) 258 throws ClosedChannelException 259 { 260 return register(sel, ops, null); 261 } 262 263 /** 264 * Adjusts this channel's blocking mode. 265 * 266 * <p> If this channel is registered with one or more selectors then an 267 * attempt to place it into blocking mode will cause an {@link 268 * IllegalBlockingModeException} to be thrown. 269 * 270 * <p> This method may be invoked at any time. The new blocking mode will 271 * only affect I/O operations that are initiated after this method returns. 272 * For some implementations this may require blocking until all pending I/O 273 * operations are complete. 274 * 275 * <p> If this method is invoked while another invocation of this method or 276 * of the {@link #register(Selector, int) register} method is in progress 277 * then it will first block until the other operation is complete. </p> 278 * 279 * @param block If {@code true} then this channel will be placed in 280 * blocking mode; if {@code false} then it will be placed 281 * non-blocking mode 282 * 283 * @return This selectable channel 284 * 285 * @throws ClosedChannelException 286 * If this channel is closed 287 * 288 * @throws IllegalBlockingModeException 289 * If {@code block} is {@code true} and this channel is 290 * registered with one or more selectors 291 * 292 * @throws IOException 293 * If an I/O error occurs 294 */ configureBlocking(boolean block)295 public abstract SelectableChannel configureBlocking(boolean block) 296 throws IOException; 297 298 /** 299 * Tells whether or not every I/O operation on this channel will block 300 * until it completes. A newly-created channel is always in blocking mode. 301 * 302 * <p> If this channel is closed then the value returned by this method is 303 * not specified. </p> 304 * 305 * @return {@code true} if, and only if, this channel is in blocking mode 306 */ isBlocking()307 public abstract boolean isBlocking(); 308 309 /** 310 * Retrieves the object upon which the {@link #configureBlocking 311 * configureBlocking} and {@link #register register} methods synchronize. 312 * This is often useful in the implementation of adaptors that require a 313 * specific blocking mode to be maintained for a short period of time. 314 * 315 * @return The blocking-mode lock object 316 */ blockingLock()317 public abstract Object blockingLock(); 318 319 } 320