jdk/src/share/classes/java/nio/channels/SelectionKey.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 5506 202f599c92aa
child 18164 68f1bc4eadd4
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, psandoz
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: 1247
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: 1247
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: 1247
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: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
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.util.concurrent.atomic.AtomicReferenceFieldUpdater;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A token representing the registration of a {@link SelectableChannel} with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * {@link Selector}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> A selection key is created each time a channel is registered with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * selector.  A key remains valid until it is <i>cancelled</i> by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * {@link #cancel cancel} method, by closing its channel, or by closing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * selector.  Cancelling a key does not immediately remove it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * selector; it is instead added to the selector's <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * href="Selector.html#ks"><i>cancelled-key set</i></a> for removal during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * next selection operation.  The validity of a key may be tested by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * its {@link #isValid isValid} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <a name="opsets">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> A selection key contains two <i>operation sets</i> represented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * integer values.  Each bit of an operation set denotes a category of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * selectable operations that are supported by the key's channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   <li><p> The <i>interest set</i> determines which operation categories will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   be tested for readiness the next time one of the selector's selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   methods is invoked.  The interest set is initialized with the value given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   when the key is created; it may later be changed via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *   #interestOps(int)} method. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   <li><p> The <i>ready set</i> identifies the operation categories for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   the key's channel has been detected to be ready by the key's selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   The ready set is initialized to zero when the key is created; it may later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   be updated by the selector during a selection operation, but it cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *   updated directly. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> That a selection key's ready set indicates that its channel is ready for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * some operation category is a hint, but not a guarantee, that an operation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * such a category may be performed by a thread without causing the thread to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * block.  A ready set is most likely to be accurate immediately after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * completion of a selection operation.  It is likely to be made inaccurate by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * external events and by I/O operations that are invoked upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * corresponding channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p> This class defines all known operation-set bits, but precisely which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * bits are supported by a given channel depends upon the type of the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Each subclass of {@link SelectableChannel} defines an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * SelectableChannel#validOps() validOps()} method which returns a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * identifying just those operations that are supported by the channel.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * attempt to set or test an operation-set bit that is not supported by a key's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * channel will result in an appropriate run-time exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * <p> It is often necessary to associate some application-specific data with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * selection key, for example an object that represents the state of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * higher-level protocol and handles readiness notifications in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * implement that protocol.  Selection keys therefore support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <i>attachment</i> of a single arbitrary object to a key.  An object can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * attached via the {@link #attach attach} method and then later retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the {@link #attachment() attachment} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p> Selection keys are safe for use by multiple concurrent threads.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * operations of reading and writing the interest set will, in general, be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * synchronized with certain operations of the selector.  Exactly how this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * synchronization is performed is implementation-dependent: In a naive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * implementation, reading or writing the interest set may block indefinitely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * if a selection operation is already in progress; in a high-performance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * implementation, reading or writing the interest set may block briefly, if at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * all.  In any case, a selection operation will always use the interest-set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * value that was current at the moment that the operation began.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * @see SelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * @see Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
public abstract class SelectionKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Constructs an instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected SelectionKey() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // -- Channel and selector operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * Returns the channel for which this key was created.  This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * continue to return the channel even after the key is cancelled.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return  This key's channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public abstract SelectableChannel channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Returns the selector for which this key was created.  This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * continue to return the selector even after the key is cancelled.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @return  This key's selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public abstract Selector selector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Tells whether or not this key is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <p> A key is valid upon creation and remains so until it is cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * its channel is closed, or its selector is closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return  <tt>true</tt> if, and only if, this key is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Requests that the registration of this key's channel with its selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * be cancelled.  Upon return the key will be invalid and will have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * added to its selector's cancelled-key set.  The key will be removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * all of the selector's key sets during the next selection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * <p> If this key has already been cancelled then invoking this method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * no effect.  Once cancelled, a key remains forever invalid. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * <p> This method may be invoked at any time.  It synchronizes on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * selector's cancelled-key set, and therefore may block briefly if invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * concurrently with a cancellation or selection operation involving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * same selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public abstract void cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    // -- Operation-set accessors --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Retrieves this key's interest set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p> It is guaranteed that the returned set will only contain operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * bits that are valid for this key's channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p> This method may be invoked at any time.  Whether or not it blocks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * and for how long, is implementation-dependent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return  This key's interest set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public abstract int interestOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Sets this key's interest set to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * <p> This method may be invoked at any time.  Whether or not it blocks,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * and for how long, is implementation-dependent.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param  ops  The new interest set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * @return  This selection key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *          If a bit in the set does not correspond to an operation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *          is supported by this key's channel, that is, if
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   194
     *          {@code (ops & ~channel().validOps()) != 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public abstract SelectionKey interestOps(int ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Retrieves this key's ready-operation set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * <p> It is guaranteed that the returned set will only contain operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * bits that are valid for this key's channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return  This key's ready-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public abstract int readyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // -- Operation bits and bit-testing convenience methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Operation-set bit for read operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p> Suppose that a selection key's interest set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <tt>OP_READ</tt> at the start of a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * detects that the corresponding channel is ready for reading, has reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * end-of-stream, has been remotely shut down for further reading, or has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * an error pending, then it will add <tt>OP_READ</tt> to the key's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * ready-operation set and add the key to its selected-key&nbsp;set.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public static final int OP_READ = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Operation-set bit for write operations.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <p> Suppose that a selection key's interest set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <tt>OP_WRITE</tt> at the start of a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * detects that the corresponding channel is ready for writing, has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * remotely shut down for further writing, or has an error pending, then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * will add <tt>OP_WRITE</tt> to the key's ready set and add the key to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * selected-key&nbsp;set.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public static final int OP_WRITE = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * Operation-set bit for socket-connect operations.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p> Suppose that a selection key's interest set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <tt>OP_CONNECT</tt> at the start of a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * detects that the corresponding socket channel is ready to complete its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * connection sequence, or has an error pending, then it will add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <tt>OP_CONNECT</tt> to the key's ready set and add the key to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * selected-key&nbsp;set.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public static final int OP_CONNECT = 1 << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Operation-set bit for socket-accept operations.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p> Suppose that a selection key's interest set contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <tt>OP_ACCEPT</tt> at the start of a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * detects that the corresponding server-socket channel is ready to accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * another connection, or has an error pending, then it will add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * <tt>OP_ACCEPT</tt> to the key's ready set and add the key to its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * selected-key&nbsp;set.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static final int OP_ACCEPT = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Tests whether this key's channel is ready for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <p> An invocation of this method of the form <tt>k.isReadable()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   275
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   276
     * k.readyOps() & OP_READ != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   277
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <p> If this key's channel does not support read operations then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * method always returns <tt>false</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return  <tt>true</tt> if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   283
                {@code readyOps() & OP_READ} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public final boolean isReadable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return (readyOps() & OP_READ) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * Tests whether this key's channel is ready for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p> An invocation of this method of the form <tt>k.isWritable()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   298
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   299
     * k.readyOps() & OP_WRITE != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   300
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p> If this key's channel does not support write operations then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * method always returns <tt>false</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return  <tt>true</tt> if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   306
     *          {@code readyOps() & OP_WRITE} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    public final boolean isWritable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        return (readyOps() & OP_WRITE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Tests whether this key's channel has either finished, or failed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * finish, its socket-connection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p> An invocation of this method of the form <tt>k.isConnectable()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   322
     * <blockqoute><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   323
     * k.readyOps() & OP_CONNECT != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   324
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p> If this key's channel does not support socket-connect operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * then this method always returns <tt>false</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * @return  <tt>true</tt> if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   330
     *          {@code readyOps() & OP_CONNECT} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public final boolean isConnectable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return (readyOps() & OP_CONNECT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Tests whether this key's channel is ready to accept a new socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <p> An invocation of this method of the form <tt>k.isAcceptable()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   346
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   347
     * k.readyOps() & OP_ACCEPT != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   348
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p> If this key's channel does not support socket-accept operations then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * this method always returns <tt>false</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @return  <tt>true</tt> if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   354
     *          {@code readyOps() & OP_ACCEPT} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public final boolean isAcceptable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return (readyOps() & OP_ACCEPT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    // -- Attachments --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    private volatile Object attachment = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    private static final AtomicReferenceFieldUpdater<SelectionKey,Object>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        attachmentUpdater = AtomicReferenceFieldUpdater.newUpdater(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            SelectionKey.class, Object.class, "attachment"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Attaches the given object to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * <p> An attached object may later be retrieved via the {@link #attachment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * attachment} method.  Only one object may be attached at a time; invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * this method causes any previous attachment to be discarded.  The current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * attachment may be discarded by attaching <tt>null</tt>.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param  ob
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *         The object to be attached; may be <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @return  The previously-attached object, if any,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *          otherwise <tt>null</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public final Object attach(Object ob) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return attachmentUpdater.getAndSet(this, ob);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Retrieves the current attachment.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return  The object currently attached to this key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *          or <tt>null</tt> if there is no attachment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public final Object attachment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        return attachment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
}