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