jdk/src/share/classes/java/nio/channels/spi/AbstractSelectableChannel.java
author sherman
Mon, 13 Oct 2008 14:45:27 -0700
changeset 1449 2ed6188288d6
parent 2 90ce3da70b43
child 1639 a97859015238
permissions -rw-r--r--
5025260: Register methods should throw ClosedChannelException instead of NPE Summary: update the spec and implementation to throw ClosedSelectorException Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels.spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.*;
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
 * Base implementation class for selectable channels.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> This class defines methods that handle the mechanics of channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * registration, deregistration, and closing.  It maintains the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * blocking mode of this channel as well as its current set of selection keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * It performs all of the synchronization required to implement the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * java.nio.channels.SelectableChannel} specification.  Implementations of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * abstract protected methods defined in this class need not synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * against other threads that might be engaged in the same operations.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author Mike McCloskey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class AbstractSelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    extends SelectableChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private final SelectorProvider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Keys that have been created by registering this channel with selectors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // They are saved because if this channel is closed the keys must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // deregistered.  Protected by keyLock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private SelectionKey[] keys = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private int keyCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // Lock for key set and count
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private final Object keyLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // Lock for registration and configureBlocking operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private final Object regLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Blocking mode, protected by regLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    boolean blocking = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Initializes a new instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected AbstractSelectableChannel(SelectorProvider provider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Returns the provider that created this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return  The provider that created this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public final SelectorProvider provider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    // -- Utility methods for the key set --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private void addKey(SelectionKey k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if ((keys != null) && (keyCount < keys.length)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                // Find empty element of key array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                for (i = 0; i < keys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    if (keys[i] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            } else if (keys == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                keys =  new SelectionKey[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // Grow key array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                int n = keys.length * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                SelectionKey[] ks =  new SelectionKey[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                for (i = 0; i < keys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                    ks[i] = keys[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                keys = ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                i = keyCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            keys[i] = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            keyCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private SelectionKey findKey(Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (keys == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            for (int i = 0; i < keys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                if ((keys[i] != null) && (keys[i].selector() == sel))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    return keys[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    void removeKey(SelectionKey k) {                    // package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            for (int i = 0; i < keys.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                if (keys[i] == k) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                    keys[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    keyCount--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            ((AbstractSelectionKey)k).invalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private boolean haveValidKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (keyCount == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            for (int i = 0; i < keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                if ((keys[i] != null) && keys[i].isValid())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    // -- Registration --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public final boolean isRegistered() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return keyCount != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public final SelectionKey keyFor(Selector sel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return findKey(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Registers this channel with the given selector, returning a selection key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>  This method first verifies that this channel is open and that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * given initial interest set is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p> If this channel is already registered with the given selector then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * the selection key representing that registration is returned after
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * setting its interest set to the given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * <p> Otherwise this channel has not yet been registered with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * selector, so the {@link AbstractSelector#register register} method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * the selector is invoked while holding the appropriate locks.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * resulting key is added to this channel's key set before being returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * </p>
1449
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   178
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   179
     * @throws  ClosedSelectorException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   180
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   181
     * @throws  IllegalBlockingModeException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   182
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   183
     * @throws  IllegalSelectorException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   184
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   185
     * @throws  CancelledKeyException {@inheritDoc}
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   186
     *
2ed6188288d6 5025260: Register methods should throw ClosedChannelException instead of NPE
sherman
parents: 2
diff changeset
   187
     * @throws  IllegalArgumentException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public final SelectionKey register(Selector sel, int ops,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                       Object att)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throws ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if ((ops & ~validOps()) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        synchronized (regLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            SelectionKey k = findKey(sel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (k != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                k.interestOps(ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                k.attach(att);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (k == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                // New registration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                k = ((AbstractSelector)sel).register(this, ops, att);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                addKey(k);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    // -- Closing --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Closes this channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <p> This method, which is specified in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * AbstractInterruptibleChannel} class and is invoked by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * java.nio.channels.Channel#close close} method, in turn invokes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * {@link #implCloseSelectableChannel implCloseSelectableChannel} method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * order to perform the actual work of closing this channel.  It then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * cancels all of this channel's keys.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected final void implCloseChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        implCloseSelectableChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        synchronized (keyLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            int count = (keys == null) ? 0 : keys.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                SelectionKey k = keys[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                if (k != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    k.cancel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Closes this selectable channel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <p> This method is invoked by the {@link java.nio.channels.Channel#close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * close} method in order to perform the actual work of closing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * channel.  This method is only invoked if the channel has not yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * closed, and it is never invoked more than once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <p> An implementation of this method must arrange for any other thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * that is blocked in an I/O operation upon this channel to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * immediately, either by throwing an exception or by returning normally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    protected abstract void implCloseSelectableChannel() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    // -- Blocking --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public final boolean isBlocking() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        synchronized (regLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            return blocking;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public final Object blockingLock() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return regLock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Adjusts this channel's blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * <p> If the given blocking mode is different from the current blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * mode then this method invokes the {@link #implConfigureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * implConfigureBlocking} method, while holding the appropriate locks, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * order to change the mode.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public final SelectableChannel configureBlocking(boolean block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        synchronized (regLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            if (blocking == block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if (block && haveValidKeys())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                throw new IllegalBlockingModeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            implConfigureBlocking(block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            blocking = block;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Adjusts this channel's blocking mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> This method is invoked by the {@link #configureBlocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * configureBlocking} method in order to perform the actual work of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * changing the blocking mode.  This method is only invoked if the new mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * is different from the current mode.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *         If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    protected abstract void implConfigureBlocking(boolean block)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}