jdk/src/share/classes/java/nio/channels/SelectableChannel.java
author alanb
Fri, 28 Jun 2013 16:10:23 +0100
changeset 18574 4aeaeb541678
parent 18164 68f1bc4eadd4
child 23010 6dadb192ad81
permissions -rw-r--r--
8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.** Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
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
 *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    67
 * <a name="bm"></a>
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
    68
 * <h2>Blocking mode</h2>
2
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
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   114
     * concrete channel class.
2
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
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   143
     * selector.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   145
     * @param   sel
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   146
     *          The selector
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18164
diff changeset
   147
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return  The key returned when this channel was last registered with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          given selector, or <tt>null</tt> if this channel is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          currently registered with that selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public abstract SelectionKey keyFor(Selector sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // sync(keySet) { return findKey(sel); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Registers this channel with the given selector, returning a selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p> If this channel is currently registered with the given selector then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * the selection key representing that registration is returned.  The key's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * interest set will have been changed to <tt>ops</tt>, as if by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * the {@link SelectionKey#interestOps(int) interestOps(int)} method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * the <tt>att</tt> argument is not <tt>null</tt> then the key's attachment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * will have been set to that value.  A {@link CancelledKeyException} will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * be thrown if the key has already been cancelled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p> Otherwise this channel has not yet been registered with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * selector, so it is registered and the resulting new key is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * The key's initial interest set will be <tt>ops</tt> and its attachment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * will be <tt>att</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p> This method may be invoked at any time.  If this method is invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * while another invocation of this method or of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * #configureBlocking(boolean) configureBlocking} method is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * then it will first block until the other operation is complete.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * method will then synchronize on the selector's key set and therefore may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * block if invoked concurrently with another registration or selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * operation involving the same selector. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p> If this channel is closed while this operation is in progress then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * the key returned by this method will have been cancelled and will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * therefore be invalid. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param  sel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *         The selector with which this channel is to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @param  ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *         The interest set for the resulting key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param  att
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *         The attachment for the resulting key; may be <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   197
     * @throws  ClosedSelectorException
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   198
     *          If the selector is closed
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   199
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @throws  IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *          If this channel is in blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * @throws  IllegalSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *          If this channel was not created by the same provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *          as the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *          If this channel is currently registered with the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *          but the corresponding key has already been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *          If a bit in the <tt>ops</tt> set does not correspond to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *          operation that is supported by this channel, that is, if
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   214
     *          {@code set & ~validOps() != 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return  A key representing the registration of this channel with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *          the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public abstract SelectionKey register(Selector sel, int ops, Object att)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        throws ClosedChannelException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    // sync(regLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    //   sync(keySet) { look for selector }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    //   if (channel found) { set interest ops -- may block in selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    //                        return key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    //   create new key -- may block somewhere in selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    //   sync(keySet) { add key; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    //   attach(attachment);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    //   return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Registers this channel with the given selector, returning a selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <p> An invocation of this convenience method of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <blockquote><tt>sc.register(sel, ops)</tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <blockquote><tt>sc.{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * #register(java.nio.channels.Selector,int,java.lang.Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * register}(sel, ops, null)</tt></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @param  sel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *         The selector with which this channel is to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param  ops
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         The interest set for the resulting key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   255
     * @throws  ClosedSelectorException
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   256
     *          If the selector is closed
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   257
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @throws  IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *          If this channel is in blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @throws  IllegalSelectorException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *          If this channel was not created by the same provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *          as the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *          If this channel is currently registered with the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *          but the corresponding key has already been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          If a bit in <tt>ops</tt> does not correspond to an operation
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   271
     *          that is supported by this channel, that is, if {@code set &
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   272
     *          ~validOps() != 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return  A key representing the registration of this channel with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          the given selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public final SelectionKey register(Selector sel, int ops)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        throws ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return register(sel, ops, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * Adjusts this channel's blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * <p> If this channel is registered with one or more selectors then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * attempt to place it into blocking mode will cause an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * IllegalBlockingModeException} to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p> This method may be invoked at any time.  The new blocking mode will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * only affect I/O operations that are initiated after this method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * For some implementations this may require blocking until all pending I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * operations are complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p> If this method is invoked while another invocation of this method or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * of the {@link #register(Selector, int) register} method is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * then it will first block until the other operation is complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param  block  If <tt>true</tt> then this channel will be placed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *                blocking mode; if <tt>false</tt> then it will be placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *                non-blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @return  This selectable channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws  IllegalBlockingModeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          If <tt>block</tt> is <tt>true</tt> and this channel is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     *          registered with one or more selectors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *         If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public abstract SelectableChannel configureBlocking(boolean block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    // sync(regLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    //   sync(keySet) { throw IBME if block && isRegistered; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    //   change mode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    // }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Tells whether or not every I/O operation on this channel will block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * until it completes.  A newly-created channel is always in blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <p> If this channel is closed then the value returned by this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * not specified. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return <tt>true</tt> if, and only if, this channel is in blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public abstract boolean isBlocking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Retrieves the object upon which the {@link #configureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * configureBlocking} and {@link #register register} methods synchronize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * This is often useful in the implementation of adaptors that require a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * specific blocking mode to be maintained for a short period of time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * @return  The blocking-mode lock object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public abstract Object blockingLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
}