src/java.base/share/classes/java/nio/channels/SelectionKey.java
author alanb
Tue, 08 Oct 2019 10:58:36 +0100
changeset 58496 7f34de3cdfe9
parent 50602 ed8de3d0cd28
permissions -rw-r--r--
8231921: (se) SelectorImpl.register does not need to set the attachment when it is null Reviewed-by: bpb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58496
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
     2
 * Copyright (c) 2000, 2019, 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
58496
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
    28
import java.lang.invoke.MethodHandles;
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
    29
import java.lang.invoke.VarHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A token representing the registration of a {@link SelectableChannel} with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * {@link Selector}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> A selection key is created each time a channel is registered with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * selector.  A key remains valid until it is <i>cancelled</i> by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * {@link #cancel cancel} method, by closing its channel, or by closing its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * selector.  Cancelling a key does not immediately remove it from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * selector; it is instead added to the selector's <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * href="Selector.html#ks"><i>cancelled-key set</i></a> for removal during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * next selection operation.  The validity of a key may be tested by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * its {@link #isValid isValid} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 34774
diff changeset
    44
 * <a id="opsets"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p> A selection key contains two <i>operation sets</i> represented as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * integer values.  Each bit of an operation set denotes a category of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * selectable operations that are supported by the key's channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *   <li><p> The <i>interest set</i> determines which operation categories will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *   be tested for readiness the next time one of the selector's selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *   methods is invoked.  The interest set is initialized with the value given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *   when the key is created; it may later be changed via the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *   #interestOps(int)} method. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *   <li><p> The <i>ready set</i> identifies the operation categories for which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *   the key's channel has been detected to be ready by the key's selector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *   The ready set is initialized to zero when the key is created; it may later
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *   be updated by the selector during a selection operation, but it cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *   updated directly. </p></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> That a selection key's ready set indicates that its channel is ready for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * some operation category is a hint, but not a guarantee, that an operation in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * such a category may be performed by a thread without causing the thread to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * block.  A ready set is most likely to be accurate immediately after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * completion of a selection operation.  It is likely to be made inaccurate by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * external events and by I/O operations that are invoked upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * corresponding channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <p> This class defines all known operation-set bits, but precisely which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * bits are supported by a given channel depends upon the type of the channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Each subclass of {@link SelectableChannel} defines an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * SelectableChannel#validOps() validOps()} method which returns a set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * identifying just those operations that are supported by the channel.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * attempt to set or test an operation-set bit that is not supported by a key's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * channel will result in an appropriate run-time exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> It is often necessary to associate some application-specific data with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * selection key, for example an object that represents the state of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * higher-level protocol and handles readiness notifications in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * implement that protocol.  Selection keys therefore support the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <i>attachment</i> of a single arbitrary object to a key.  An object can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * attached via the {@link #attach attach} method and then later retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * the {@link #attachment() attachment} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    90
 * <p> Selection keys are safe for use by multiple concurrent threads.  A
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    91
 * selection operation will always use the interest-set value that was current
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
    92
 * at the moment that the operation began.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * @see SelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * @see Selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
public abstract class SelectionKey {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Constructs an instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected SelectionKey() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // -- Channel and selector operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns the channel for which this key was created.  This method will
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   115
     * continue to return the channel even after the key is cancelled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return  This key's channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public abstract SelectableChannel channel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Returns the selector for which this key was created.  This method will
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   123
     * continue to return the selector even after the key is cancelled.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return  This key's selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public abstract Selector selector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Tells whether or not this key is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p> A key is valid upon creation and remains so until it is cancelled,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * its channel is closed, or its selector is closed.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   135
     * @return  {@code true} if, and only if, this key is valid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Requests that the registration of this key's channel with its selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * be cancelled.  Upon return the key will be invalid and will have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * added to its selector's cancelled-key set.  The key will be removed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * all of the selector's key sets during the next selection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * <p> If this key has already been cancelled then invoking this method has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * no effect.  Once cancelled, a key remains forever invalid. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * <p> This method may be invoked at any time.  It synchronizes on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * selector's cancelled-key set, and therefore may block briefly if invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * concurrently with a cancellation or selection operation involving the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * same selector.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public abstract void cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    // -- Operation-set accessors --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Retrieves this key's interest set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p> It is guaranteed that the returned set will only contain operation
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   162
     * bits that are valid for this key's channel. </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return  This key's interest set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public abstract int interestOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Sets this key's interest set to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
49802
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   174
     * <p> This method may be invoked at any time.  If this method is invoked
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   175
     * while a selection operation is in progress then it has no effect upon
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   176
     * that operation; the change to the key's interest set will be seen by the
8ac08fa69f00 8201315: (se) Allow SelectableChannel.register to be invoked while selection operation is in progress
alanb
parents: 47216
diff changeset
   177
     * next selection operation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param  ops  The new interest set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return  This selection key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *          If a bit in the set does not correspond to an operation that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *          is supported by this key's channel, that is, if
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   186
     *          {@code (ops & ~channel().validOps()) != 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public abstract SelectionKey interestOps(int ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
50439
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   194
     * Atomically sets this key's interest set to the bitwise union ("or") of
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   195
     * the existing interest set and the given value. This method is guaranteed
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   196
     * to be atomic with respect to other concurrent calls to this method or to
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   197
     * {@link #interestOpsAnd(int)}.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   198
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   199
     * <p> This method may be invoked at any time.  If this method is invoked
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   200
     * while a selection operation is in progress then it has no effect upon
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   201
     * that operation; the change to the key's interest set will be seen by the
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   202
     * next selection operation.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   203
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   204
     * @implSpec The default implementation synchronizes on this key and invokes
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   205
     * {@code interestOps()} and {@code interestOps(int)} to retrieve and set
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   206
     * this key's interest set.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   207
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   208
     * @param  ops  The interest set to apply
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   209
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   210
     * @return  The previous interest set
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   211
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   212
     * @throws  IllegalArgumentException
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   213
     *          If a bit in the set does not correspond to an operation that
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   214
     *          is supported by this key's channel, that is, if
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   215
     *          {@code (ops & ~channel().validOps()) != 0}
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   216
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   217
     * @throws  CancelledKeyException
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   218
     *          If this key has been cancelled
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   219
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   220
     * @since 11
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   221
     */
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   222
    public int interestOpsOr(int ops) {
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   223
        synchronized (this) {
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   224
            int oldVal = interestOps();
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   225
            interestOps(oldVal | ops);
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   226
            return oldVal;
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   227
        }
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   228
    }
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   229
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   230
    /**
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   231
     * Atomically sets this key's interest set to the bitwise intersection ("and")
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   232
     * of the existing interest set and the given value. This method is guaranteed
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   233
     * to be atomic with respect to other concurrent calls to this method or to
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   234
     * {@link #interestOpsOr(int)}.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   235
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   236
     * <p> This method may be invoked at any time.  If this method is invoked
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   237
     * while a selection operation is in progress then it has no effect upon
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   238
     * that operation; the change to the key's interest set will be seen by the
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   239
     * next selection operation.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   240
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   241
     * @apiNote Unlike the {@code interestOps(int)} and {@code interestOpsOr(int)}
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   242
     * methods, this method does not throw {@code IllegalArgumentException} when
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   243
     * invoked with bits in the interest set that do not correspond to an
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   244
     * operation that is supported by this key's channel. This is to allow
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   245
     * operation bits in the interest set to be cleared using bitwise complement
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   246
     * values, e.g., {@code interestOpsAnd(~SelectionKey.OP_READ)} will remove
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   247
     * the {@code OP_READ} from the interest set without affecting other bits.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   248
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   249
     * @implSpec The default implementation synchronizes on this key and invokes
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   250
     * {@code interestOps()} and {@code interestOps(int)} to retrieve and set
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   251
     * this key's interest set.
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   252
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   253
     * @param  ops  The interest set to apply
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   254
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   255
     * @return  The previous interest set
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   256
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   257
     * @throws  CancelledKeyException
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   258
     *          If this key has been cancelled
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   259
     *
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   260
     * @since 11
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   261
     */
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   262
    public int interestOpsAnd(int ops) {
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   263
        synchronized (this) {
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   264
            int oldVal = interestOps();
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   265
            interestOps(oldVal & ops);
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   266
            return oldVal;
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   267
        }
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   268
    }
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   269
c5c827f3bf72 6350055: (se) SelectionKey.interestOps variants to atomically update interest ops
alanb
parents: 49802
diff changeset
   270
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Retrieves this key's ready-operation set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <p> It is guaranteed that the returned set will only contain operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * bits that are valid for this key's channel.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @return  This key's ready-operation set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    public abstract int readyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // -- Operation bits and bit-testing convenience methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Operation-set bit for read operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * <p> Suppose that a selection key's interest set contains
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   290
     * {@code OP_READ} at the start of a <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * detects that the corresponding channel is ready for reading, has reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * end-of-stream, has been remotely shut down for further reading, or has
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   294
     * an error pending, then it will add {@code OP_READ} to the key's
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 50439
diff changeset
   295
     * ready-operation set.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public static final int OP_READ = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   300
     * Operation-set bit for write operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * <p> Suppose that a selection key's interest set contains
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   303
     * {@code OP_WRITE} at the start of a <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * detects that the corresponding channel is ready for writing, has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * remotely shut down for further writing, or has an error pending, then it
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 50439
diff changeset
   307
     * will add {@code OP_WRITE} to the key's ready set.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public static final int OP_WRITE = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   312
     * Operation-set bit for socket-connect operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * <p> Suppose that a selection key's interest set contains
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   315
     * {@code OP_CONNECT} at the start of a <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * detects that the corresponding socket channel is ready to complete its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * connection sequence, or has an error pending, then it will add
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 50439
diff changeset
   319
     * {@code OP_CONNECT} to the key's ready set.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    public static final int OP_CONNECT = 1 << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   324
     * Operation-set bit for socket-accept operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p> Suppose that a selection key's interest set contains
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   327
     * {@code OP_ACCEPT} at the start of a <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * href="Selector.html#selop">selection operation</a>.  If the selector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * detects that the corresponding server-socket channel is ready to accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * another connection, or has an error pending, then it will add
50602
ed8de3d0cd28 8199433: (se) select(Consumer<SelectionKey> action) as alternative to selected-key set
alanb
parents: 50439
diff changeset
   331
     * {@code OP_ACCEPT} to the key's ready set.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public static final int OP_ACCEPT = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * Tests whether this key's channel is ready for reading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   338
     * <p> An invocation of this method of the form {@code k.isReadable()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   341
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   342
     * k.readyOps() & OP_READ != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   343
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * <p> If this key's channel does not support read operations then this
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   346
     * method always returns {@code false}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   348
     * @return  {@code true} if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   349
                {@code readyOps() & OP_READ} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public final boolean isReadable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return (readyOps() & OP_READ) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Tests whether this key's channel is ready for writing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   361
     * <p> An invocation of this method of the form {@code k.isWritable()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   364
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   365
     * k.readyOps() & OP_WRITE != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   366
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * <p> If this key's channel does not support write operations then this
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   369
     * method always returns {@code false}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   371
     * @return  {@code true} if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   372
     *          {@code readyOps() & OP_WRITE} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public final boolean isWritable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        return (readyOps() & OP_WRITE) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * Tests whether this key's channel has either finished, or failed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * finish, its socket-connection operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   385
     * <p> An invocation of this method of the form {@code k.isConnectable()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   388
     * <blockquote><pre>{@code
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   389
     * k.readyOps() & OP_CONNECT != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   390
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p> If this key's channel does not support socket-connect operations
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   393
     * then this method always returns {@code false}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   395
     * @return  {@code true} if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   396
     *          {@code readyOps() & OP_CONNECT} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    public final boolean isConnectable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return (readyOps() & OP_CONNECT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Tests whether this key's channel is ready to accept a new socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   409
     * <p> An invocation of this method of the form {@code k.isAcceptable()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * behaves in exactly the same way as the expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   412
     * <blockquote><pre>{@code
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   413
     * k.readyOps() & OP_ACCEPT != 0
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   414
     * }</pre></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p> If this key's channel does not support socket-accept operations then
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   417
     * this method always returns {@code false}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   419
     * @return  {@code true} if, and only if,
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 5506
diff changeset
   420
     *          {@code readyOps() & OP_ACCEPT} is nonzero
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @throws  CancelledKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *          If this key has been cancelled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public final boolean isAcceptable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        return (readyOps() & OP_ACCEPT) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    // -- Attachments --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
58496
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   432
    private static final VarHandle ATTACHMENT;
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   433
    static {
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   434
        try {
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   435
            MethodHandles.Lookup l = MethodHandles.lookup();
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   436
            ATTACHMENT = l.findVarHandle(SelectionKey.class, "attachment", Object.class);
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   437
        } catch (Exception e) {
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   438
            throw new InternalError(e);
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   439
        }
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   440
    }
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 32143
diff changeset
   441
    private volatile Object attachment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * Attaches the given object to this key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p> An attached object may later be retrieved via the {@link #attachment()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * attachment} method.  Only one object may be attached at a time; invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * this method causes any previous attachment to be discarded.  The current
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   449
     * attachment may be discarded by attaching {@code null}.  </p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * @param  ob
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   452
     *         The object to be attached; may be {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return  The previously-attached object, if any,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   455
     *          otherwise {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    public final Object attach(Object ob) {
58496
7f34de3cdfe9 8231921: (se) SelectorImpl.register does not need to set the attachment when it is null
alanb
parents: 50602
diff changeset
   458
        return ATTACHMENT.getAndSet(this, ob);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
18164
68f1bc4eadd4 8016370: javadoc warnings, unexpected </p> mostly
alanb
parents: 18156
diff changeset
   462
     * Retrieves the current attachment.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return  The object currently attached to this key,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   465
     *          or {@code null} if there is no attachment
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public final Object attachment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return attachment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
}