jdk/src/solaris/classes/sun/nio/ch/SinkChannelImpl.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
     2
 * Copyright 2000-2009 Sun Microsystems, Inc.  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
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 sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
class SinkChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    extends Pipe.SinkChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    implements SelChImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // Used to make native read and write calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static NativeDispatcher nd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // The file descriptor associated with this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    FileDescriptor fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // fd value needed for dev/poll. This value will remain valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // even after the value in the file descriptor object has been set to -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // ID of native thread doing write, for signalling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private volatile long thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // Lock held by current reading thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private final Object lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // Lock held by any thread that modifies the state fields declared below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // DO NOT invoke a blocking I/O operation while holding this lock!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final Object stateLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // -- The following fields are protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // Channel state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int ST_UNINITIALIZED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int ST_INUSE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static final int ST_KILLED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private volatile int state = ST_UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // -- End of fields protected by stateLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public FileDescriptor getFD() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public int getFDVal() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return fdVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    SinkChannelImpl(SelectorProvider sp, FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(sp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        this.fdVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this.state = ST_INUSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    protected void implCloseSelectableChannel() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            nd.preClose(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            long th = thread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (th != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                NativeThread.signal(th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (!isRegistered())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                kill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void kill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        synchronized (stateLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (state == ST_KILLED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (state == ST_UNINITIALIZED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            assert !isOpen() && !isRegistered();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            state = ST_KILLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    protected void implConfigureBlocking(boolean block) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        IOUtil.configureBlocking(fd, block);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public boolean translateReadyOps(int ops, int initialOps,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                     SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int intOps = sk.nioInterestOps();// Do this just once, it synchronizes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        int oldOps = sk.nioReadyOps();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int newOps = initialOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ((ops & PollArrayWrapper.POLLNVAL) != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            throw new Error("POLLNVAL detected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if ((ops & (PollArrayWrapper.POLLERR
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    | PollArrayWrapper.POLLHUP)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            newOps = intOps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (((ops & PollArrayWrapper.POLLOUT) != 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            ((intOps & SelectionKey.OP_WRITE) != 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            newOps |= SelectionKey.OP_WRITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        sk.nioReadyOps(newOps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return (newOps & ~oldOps) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        return translateReadyOps(ops, sk.nioReadyOps(), sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        return translateReadyOps(ops, 0, sk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public void translateAndSetInterestOps(int ops, SelectionKeyImpl sk) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (ops == SelectionKey.OP_WRITE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            ops = PollArrayWrapper.POLLOUT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        sk.selector.putEventOps(sk, ops);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                thread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    n = IOUtil.write(fd, src, -1, nd, lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public long write(ByteBuffer[] srcs) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (srcs == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                begin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                thread = NativeThread.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    n = IOUtil.write(fd, srcs, nd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                thread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                end((n > 0) || (n == IOStatus.UNAVAILABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
           throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return write(Util.subsequence(srcs, offset, length));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Util.load();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   211
        nd = new FileDispatcherImpl();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
}