jdk/src/share/classes/sun/nio/ch/FileChannelImpl.java
author alanb
Sun, 22 Apr 2012 21:22:17 +0100
changeset 12547 ae1b2051db5d
parent 9035 1255eb81cc2f
child 14401 1f9a8607f4df
permissions -rw-r--r--
7132924: (dc) DatagramChannel.disconnect throws SocketException with IPv4 socket and IPv6 enabled [macosx] Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8539
diff changeset
     2
 * Copyright (c) 2000, 2011, 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: 3066
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: 3066
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: 3066
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3066
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 sun.nio.ch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.FileDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.MappedByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.misc.Cleaner;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class FileChannelImpl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    extends FileChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
{
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    42
    // Memory allocation size for mapping buffers
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    43
    private static final long allocationGranularity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // Used to make native read and write calls
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    46
    private final FileDispatcher nd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // File descriptor
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    49
    private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // File access mode (immutable)
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    52
    private final boolean writable;
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    53
    private final boolean readable;
8402
41789c995fe2 6526860: (fc) FileChannel.position returns 0 when FileOutputStream opened in append mode
alanb
parents: 7668
diff changeset
    54
    private final boolean append;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // Required to prevent finalization of creating stream (immutable)
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    57
    private final Object parent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // Thread-safe set of IDs of native threads, for signalling
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    60
    private final NativeThreadSet threads = new NativeThreadSet(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // Lock for operations involving position and size
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
    63
    private final Object positionLock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private FileChannelImpl(FileDescriptor fd, boolean readable,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    66
                            boolean writable, boolean append, Object parent)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this.fd = fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        this.readable = readable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.writable = writable;
8402
41789c995fe2 6526860: (fc) FileChannel.position returns 0 when FileOutputStream opened in append mode
alanb
parents: 7668
diff changeset
    71
        this.append = append;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.parent = parent;
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    73
        this.nd = new FileDispatcherImpl(append);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    76
    // Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public static FileChannel open(FileDescriptor fd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                   boolean readable, boolean writable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                                   Object parent)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    81
        return new FileChannelImpl(fd, readable, writable, false, parent);
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    82
    }
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    83
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    84
    // Used by FileOutputStream.getChannel
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    85
    public static FileChannel open(FileDescriptor fd,
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    86
                                   boolean readable, boolean writable,
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    87
                                   boolean append, Object parent)
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    88
    {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
    89
        return new FileChannelImpl(fd, readable, writable, append, parent);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // -- Standard channel operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void implCloseChannel() throws IOException {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   101
        // Release and invalidate any locks that we still hold
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (fileLockTable != null) {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   103
            for (FileLock fl: fileLockTable.removeAll()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   104
                synchronized (fl) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   105
                    if (fl.isValid()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   106
                        nd.release(fd, fl.position(), fl.size());
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   107
                        ((FileLockImpl)fl).invalidate();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   108
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                }
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   110
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   113
        nd.preClose(fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   114
        threads.signalAndWait();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   115
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            // Close the fd via the parent stream's close method.  The parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // will reinvoke our close method, which is defined in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // superclass AbstractInterruptibleChannel, but the isOpen logic in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // that method will prevent this method from being reinvoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            //
47
c8f0e41aea68 6631352: File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)
martin
parents: 2
diff changeset
   123
            ((java.io.Closeable)parent).close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            nd.close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public int read(ByteBuffer dst) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   136
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   139
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    n = IOUtil.read(fd, dst, -1, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   154
    public long read(ByteBuffer[] dsts, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   155
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   156
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   157
        if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   158
            throw new IndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            long n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   164
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   167
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                do {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   171
                    n = IOUtil.read(fd, dsts, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   188
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   191
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    n = IOUtil.write(fd, src, -1, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   206
    public long write(ByteBuffer[] srcs, int offset, int length)
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   207
        throws IOException
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   208
    {
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   209
        if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   210
            throw new IndexOutOfBoundsException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            long n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   216
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   219
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                do {
6301
c90a67d75c9f 6971825: (so) improve scatter/gather implementation
alanb
parents: 5506
diff changeset
   223
                    n = IOUtil.write(fd, srcs, offset, length, nd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // -- Other operations --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public long position() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   240
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   243
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                do {
8402
41789c995fe2 6526860: (fc) FileChannel.position returns 0 when FileOutputStream opened in append mode
alanb
parents: 7668
diff changeset
   247
                    // in append-mode then position is advanced to end before writing
41789c995fe2 6526860: (fc) FileChannel.position returns 0 when FileOutputStream opened in append mode
alanb
parents: 7668
diff changeset
   248
                    p = (append) ? nd.size(fd) : position0(fd, -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                return IOStatus.normalize(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                end(p > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                assert IOStatus.check(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public FileChannel position(long newPosition) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (newPosition < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   265
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   268
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    p  = position0(fd, newPosition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                end(p > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                assert IOStatus.check(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public long size() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            long s = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   287
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   290
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   294
                    s = nd.size(fd);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } while ((s == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return IOStatus.normalize(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                end(s > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                assert IOStatus.check(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public FileChannel truncate(long size) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (size > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        synchronized (positionLock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            int rv = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            long p = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   316
            int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   319
                ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                // get current position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    p = position0(fd, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                } while ((p == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                assert p >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                // truncate file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   333
                    rv = nd.truncate(fd, size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                // set position to size if greater than size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                if (p > size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    p = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    rv = (int)position0(fd, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                end(rv > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                assert IOStatus.check(rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void force(boolean metaData) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int rv = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   356
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   359
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   363
                rv = nd.force(fd, metaData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            end(rv > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            assert IOStatus.check(rv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // Assume at first that the underlying kernel supports sendfile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    // set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private static volatile boolean transferSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    // Assume that the underlying kernel sendfile() will work if the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    // fd is a pipe; set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private static volatile boolean pipeSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    // Assume that the underlying kernel sendfile() will work if the target
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    // fd is a file; set this to false if we find out later that it doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private static volatile boolean fileSupported = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    private long transferToDirectly(long position, int icount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                    WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        if (!transferSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        FileDescriptor targetFD = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (target instanceof FileChannelImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (!fileSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            targetFD = ((FileChannelImpl)target).fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        } else if (target instanceof SelChImpl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            // Direct transfer to pipe causes EINVAL on some configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if ((target instanceof SinkChannelImpl) && !pipeSupported)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            targetFD = ((SelChImpl)target).getFD();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (targetFD == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        int thisFDVal = IOUtil.fdVal(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        int targetFDVal = IOUtil.fdVal(targetFD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (thisFDVal == targetFDVal) // Not supported on some configurations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        long n = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   413
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   416
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                n = transferTo0(thisFDVal, position, icount, targetFDVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (n == IOStatus.UNSUPPORTED_CASE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (target instanceof SinkChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    pipeSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (target instanceof FileChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    fileSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                return IOStatus.UNSUPPORTED_CASE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            if (n == IOStatus.UNSUPPORTED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                // Don't bother trying again
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                transferSupported = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            end (n > -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   441
    // Maximum size to map when using a mapped buffer
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   442
    private static final long MAPPED_TRANSFER_SIZE = 8L*1024L*1024L;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   443
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   444
    private long transferToTrustedChannel(long position, long count,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                                          WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    {
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   448
        boolean isSelChImpl = (target instanceof SelChImpl);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   449
        if (!((target instanceof FileChannelImpl) || isSelChImpl))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            return IOStatus.UNSUPPORTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        // Trusted target: Use a mapped buffer
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   453
        long remaining = count;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   454
        while (remaining > 0L) {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   455
            long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   456
            try {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   457
                MappedByteBuffer dbb = map(MapMode.READ_ONLY, position, size);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   458
                try {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   459
                    // ## Bug: Closing this channel will not terminate the write
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   460
                    int n = target.write(dbb);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   461
                    assert n >= 0;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   462
                    remaining -= n;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   463
                    if (isSelChImpl) {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   464
                        // one attempt to write to selectable channel
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   465
                        break;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   466
                    }
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   467
                    assert n > 0;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   468
                    position += n;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   469
                } finally {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   470
                    unmap(dbb);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   471
                }
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   472
            } catch (ClosedByInterruptException e) {
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   473
                // target closed by interrupt as ClosedByInterruptException needs
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   474
                // to be thrown after closing this channel.
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   475
                assert !target.isOpen();
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   476
                try {
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   477
                    close();
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8402
diff changeset
   478
                } catch (Throwable suppressed) {
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8402
diff changeset
   479
                    e.addSuppressed(suppressed);
7177
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   480
                }
0113db4feebc 6979009: (fc) FileChannel.read() fails to throw ClosedByInterruptException
alanb
parents: 7025
diff changeset
   481
                throw e;
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   482
            } catch (IOException ioe) {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   483
                // Only throw exception if no bytes have been written
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   484
                if (remaining == count)
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   485
                    throw ioe;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   486
                break;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   487
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   489
        return count - remaining;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    private long transferToArbitraryChannel(long position, int icount,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                                            WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // Untrusted target: Use a newly-erased buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        int c = Math.min(icount, TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        long tw = 0;                    // Total bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        long pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            Util.erase(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            while (tw < icount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                bb.limit(Math.min((int)(icount - tw), TRANSFER_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                int nr = read(bb, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                if (nr <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // ## Bug: Will block writing target if this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // ##      is asynchronously closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                int nw = target.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                tw += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                if (nw != nr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                pos += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (tw > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    public long transferTo(long position, long count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                           WritableByteChannel target)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (!target.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (target instanceof FileChannelImpl &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            !((FileChannelImpl)target).writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        if ((position < 0) || (count < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        long sz = size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        if (position > sz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        int icount = (int)Math.min(count, Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if ((sz - position) < icount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            icount = (int)(sz - position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        long n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        // Attempt a direct transfer, if the kernel supports it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        if ((n = transferToDirectly(position, icount, target)) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // Attempt a mapped transfer, but only to trusted channel types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if ((n = transferToTrustedChannel(position, icount, target)) >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        // Slow path for untrusted targets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        return transferToArbitraryChannel(position, icount, target);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    private long transferFromFileChannel(FileChannelImpl src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                         long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    {
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 6319
diff changeset
   567
        if (!src.readable)
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 6319
diff changeset
   568
            throw new NonReadableChannelException();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        synchronized (src.positionLock) {
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   570
            long pos = src.position();
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   571
            long max = Math.min(count, src.size() - pos);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   572
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   573
            long remaining = max;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   574
            long p = pos;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   575
            while (remaining > 0L) {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   576
                long size = Math.min(remaining, MAPPED_TRANSFER_SIZE);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   577
                // ## Bug: Closing this channel will not terminate the write
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   578
                MappedByteBuffer bb = src.map(MapMode.READ_ONLY, p, size);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   579
                try {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   580
                    long n = write(bb, position);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   581
                    assert n > 0;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   582
                    p += n;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   583
                    position += n;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   584
                    remaining -= n;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   585
                } catch (IOException ioe) {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   586
                    // Only throw exception if no bytes have been written
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   587
                    if (remaining == max)
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   588
                        throw ioe;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   589
                    break;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   590
                } finally {
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   591
                    unmap(bb);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   592
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            }
6319
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   594
            long nwritten = max - remaining;
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   595
            src.position(pos + nwritten);
47d9b9e70f99 6431344: (fc) FileChannel.transferTo() doesn't work if address space runs out
alanb
parents: 6301
diff changeset
   596
            return nwritten;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private static final int TRANSFER_SIZE = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    private long transferFromArbitraryChannel(ReadableByteChannel src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                              long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        // Untrusted target: Use a newly-erased buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        int c = (int)Math.min(count, TRANSFER_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        ByteBuffer bb = Util.getTemporaryDirectBuffer(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        long tw = 0;                    // Total bytes written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        long pos = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            Util.erase(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            while (tw < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                // ## Bug: Will block reading src if this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                // ##      is asynchronously closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                int nr = src.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                if (nr <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                int nw = write(bb, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                tw += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (nw != nr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                pos += nw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            if (tw > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                return tw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            throw x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            Util.releaseTemporaryDirectBuffer(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    public long transferFrom(ReadableByteChannel src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                             long position, long count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        if (!src.isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            throw new ClosedChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if ((position < 0) || (count < 0))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        if (position > size())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (src instanceof FileChannelImpl)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
           return transferFromFileChannel((FileChannelImpl)src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                          position, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        return transferFromArbitraryChannel(src, position, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public int read(ByteBuffer dst, long position) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (dst == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   667
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   670
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                n = IOUtil.read(fd, dst, position, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    public int write(ByteBuffer src, long position) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (src == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (!writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        int n = 0;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   693
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   696
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                n = IOUtil.write(fd, src, position, nd, positionLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            } while ((n == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return IOStatus.normalize(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            end(n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            assert IOStatus.check(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    // -- Memory-mapped buffers --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    private static class Unmapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        implements Runnable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
   716
        // may be required to close file
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
   717
        private static final NativeDispatcher nd = new FileDispatcherImpl();
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7177
diff changeset
   718
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   719
        // keep track of mapped buffer usage
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   720
        static volatile int count;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   721
        static volatile long totalSize;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   722
        static volatile long totalCapacity;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   724
        private volatile long address;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   725
        private final long size;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   726
        private final int cap;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   727
        private final FileDescriptor fd;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   729
        private Unmapper(long address, long size, int cap,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   730
                         FileDescriptor fd)
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   731
        {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            assert (address != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            this.address = address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            this.size = size;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   735
            this.cap = cap;
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   736
            this.fd = fd;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   737
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   738
            synchronized (Unmapper.class) {
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   739
                count++;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   740
                totalSize += size;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   741
                totalCapacity += cap;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   742
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            if (address == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            unmap0(address, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            address = 0;
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   750
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   751
            // if this mapping has a valid file descriptor then we close it
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   752
            if (fd.valid()) {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   753
                try {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   754
                    nd.close(fd);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   755
                } catch (IOException ignore) {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   756
                    // nothing we can do
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   757
                }
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   758
            }
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   759
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   760
            synchronized (Unmapper.class) {
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   761
                count--;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   762
                totalSize -= size;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   763
                totalCapacity -= cap;
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   764
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    private static void unmap(MappedByteBuffer bb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        Cleaner cl = ((DirectBuffer)bb).cleaner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (cl != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            cl.clean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    private static final int MAP_RO = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    private static final int MAP_RW = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private static final int MAP_PV = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    public MappedByteBuffer map(MapMode mode, long position, long size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        if (position < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        if (size < 0L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            throw new IllegalArgumentException("Negative size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        if (position + size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            throw new IllegalArgumentException("Position + size overflow");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        if (size > Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        int imode = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        if (mode == MapMode.READ_ONLY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            imode = MAP_RO;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        else if (mode == MapMode.READ_WRITE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            imode = MAP_RW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        else if (mode == MapMode.PRIVATE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            imode = MAP_PV;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        assert (imode >= 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        if ((mode != MapMode.READ_ONLY) && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        if (!readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        long addr = -1;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   804
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   807
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (size() < position + size) { // Extend file size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                if (!writable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                    throw new IOException("Channel not open for writing " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        "- cannot extend file to required size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                int rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                do {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   817
                    rv = nd.truncate(fd, position + size);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                addr = 0;
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   822
                // a valid file descriptor is not required
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   823
                FileDescriptor dummy = new FileDescriptor();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                if ((!writable) || (imode == MAP_RO))
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   825
                    return Util.newMappedByteBufferR(0, 0, dummy, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                else
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   827
                    return Util.newMappedByteBuffer(0, 0, dummy, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            int pagePosition = (int)(position % allocationGranularity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            long mapPosition = position - pagePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            long mapSize = size + pagePosition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                // If no exception was thrown from map0, the address is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                addr = map0(imode, mapPosition, mapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            } catch (OutOfMemoryError x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                // An OutOfMemoryError may indicate that we've exhausted memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                // so force gc and re-attempt map
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                    Thread.sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                } catch (InterruptedException y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    Thread.currentThread().interrupt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    addr = map0(imode, mapPosition, mapSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                } catch (OutOfMemoryError y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    // After a second OOME, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    throw new IOException("Map failed", y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   853
            // On Windows, and potentially other platforms, we need an open
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   854
            // file descriptor for some mapping operations.
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   855
            FileDescriptor mfd;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   856
            try {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   857
                mfd = nd.duplicateForMapping(fd);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   858
            } catch (IOException ioe) {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   859
                unmap0(addr, mapSize);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   860
                throw ioe;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   861
            }
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   862
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            assert (IOStatus.checkAll(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            assert (addr % allocationGranularity == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            int isize = (int)size;
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   866
            Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   867
            if ((!writable) || (imode == MAP_RO)) {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   868
                return Util.newMappedByteBufferR(isize,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   869
                                                 addr + pagePosition,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   870
                                                 mfd,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   871
                                                 um);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   872
            } else {
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   873
                return Util.newMappedByteBuffer(isize,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   874
                                                addr + pagePosition,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   875
                                                mfd,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   876
                                                um);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 6545
diff changeset
   877
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            end(IOStatus.checkAll(addr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   884
    /**
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   885
     * Invoked by sun.management.ManagementFactoryHelper to create the management
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   886
     * interface for mapped buffers.
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   887
     */
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   888
    public static sun.misc.JavaNioAccess.BufferPool getMappedBufferPool() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   889
        return new sun.misc.JavaNioAccess.BufferPool() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   890
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   891
            public String getName() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   892
                return "mapped";
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   893
            }
3066
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   894
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   895
            public long getCount() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   896
                return Unmapper.count;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   897
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   898
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   899
            public long getTotalCapacity() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   900
                return Unmapper.totalCapacity;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   901
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   902
            @Override
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   903
            public long getMemoryUsed() {
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   904
                return Unmapper.totalSize;
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   905
            }
cd3861104f4d 6844054: (bf) Eliminate dependency on javax.management.ObjectName
alanb
parents: 2594
diff changeset
   906
        };
1143
645d4b930f93 6682020: (bf) Support monitoring of direct and mapped buffer usage
alanb
parents: 715
diff changeset
   907
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    // -- Locks --
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   911
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    // keeps track of locks on this file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    private volatile FileLockTable fileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    // indicates if file locks are maintained system-wide (as per spec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    private static boolean isSharedFileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    // indicates if the disableSystemWideOverlappingFileLockCheck property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    // has been checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    private static volatile boolean propertyChecked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
    // The lock list in J2SE 1.4/5.0 was local to each FileChannel instance so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    // the overlap check wasn't system wide when there were multiple channels to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    // the same file. This property is used to get 1.4/5.0 behavior if desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    private static boolean isSharedFileLockTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        if (!propertyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            synchronized (FileChannelImpl.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                if (!propertyChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    String value = AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                        new GetPropertyAction(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                            "sun.nio.ch.disableSystemWideOverlappingFileLockCheck"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
                    isSharedFileLockTable = ((value == null) || value.equals("false"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    propertyChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        return isSharedFileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   941
    private FileLockTable fileLockTable() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        if (fileLockTable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                if (fileLockTable == null) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   945
                    if (isSharedFileLockTable()) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   946
                        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   947
                        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   948
                            ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   949
                            fileLockTable = FileLockTable.newSharedFileLockTable(this, fd);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   950
                        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   951
                            threads.remove(ti);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   952
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   953
                    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   954
                        fileLockTable = new SimpleFileLockTable();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
   955
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        return fileLockTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public FileLock lock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (shared && !readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        if (!shared && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        FileLockImpl fli = new FileLockImpl(this, position, size, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        FileLockTable flt = fileLockTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        flt.add(fli);
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   973
        boolean completed = false;
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   974
        int ti = -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            begin();
2441
228c040622a2 6824135: (ch) test/java/nio/channels/AsyncCloseAndInterrupt.java fails (lnx)
alanb
parents: 2057
diff changeset
   977
            ti = threads.add();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            if (!isOpen())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                return null;
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   980
            int n;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   981
            do {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   982
                n = nd.lock(fd, true, position, size, shared);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   983
            } while ((n == FileDispatcher.INTERRUPTED) && isOpen());
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   984
            if (isOpen()) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   985
                if (n == FileDispatcher.RET_EX_LOCK) {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   986
                    assert shared;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   987
                    FileLockImpl fli2 = new FileLockImpl(this, position, size,
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   988
                                                         false);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   989
                    flt.replace(fli, fli2);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   990
                    fli = fli2;
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   991
                }
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   992
                completed = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            }
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   994
        } finally {
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   995
            if (!completed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                flt.remove(fli);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            threads.remove(ti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            try {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
   999
                end(completed);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            } catch (ClosedByInterruptException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                throw new FileLockInterruptionException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        return fli;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    public FileLock tryLock(long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        if (shared && !readable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
            throw new NonReadableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        if (!shared && !writable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            throw new NonWritableChannelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        FileLockImpl fli = new FileLockImpl(this, position, size, shared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
        FileLockTable flt = fileLockTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        flt.add(fli);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1018
        int result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1019
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1020
        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1021
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1022
            try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1023
                ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1024
                result = nd.lock(fd, false, position, size, shared);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1025
            } catch (IOException e) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1026
                flt.remove(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1027
                throw e;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1028
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1029
            if (result == FileDispatcher.NO_LOCK) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1030
                flt.remove(fli);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1031
                return null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1032
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1033
            if (result == FileDispatcher.RET_EX_LOCK) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1034
                assert shared;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1035
                FileLockImpl fli2 = new FileLockImpl(this, position, size,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1036
                                                     false);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1037
                flt.replace(fli, fli2);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1038
                return fli2;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1039
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1040
            return fli;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1041
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1042
            threads.remove(ti);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    void release(FileLockImpl fli) throws IOException {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1047
        int ti = threads.add();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1048
        try {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1049
            ensureOpen();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1050
            nd.release(fd, fli.position(), fli.size());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1051
        } finally {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1052
            threads.remove(ti);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1053
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        assert fileLockTable != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        fileLockTable.remove(fli);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1058
    // -- File lock support --
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * A simple file lock table that maintains a list of FileLocks obtained by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * FileChannel. Use to get 1.4/5.0 behaviour.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     */
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 1821
diff changeset
  1064
    private static class SimpleFileLockTable extends FileLockTable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        // synchronize on list for access
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1066
        private final List<FileLock> lockList = new ArrayList<FileLock>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        public SimpleFileLockTable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        private void checkList(long position, long size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            throws OverlappingFileLockException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            assert Thread.holdsLock(lockList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            for (FileLock fl: lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                if (fl.overlaps(position, size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    throw new OverlappingFileLockException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        public void add(FileLock fl) throws OverlappingFileLockException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                checkList(fl.position(), fl.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                lockList.add(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        public void remove(FileLock fl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                lockList.remove(fl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1095
        public List<FileLock> removeAll() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            synchronized(lockList) {
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1097
                List<FileLock> result = new ArrayList<FileLock>(lockList);
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1098
                lockList.clear();
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2441
diff changeset
  1099
                return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        public void replace(FileLock fl1, FileLock fl2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            synchronized (lockList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                lockList.remove(fl1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                lockList.add(fl2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    // -- Native methods --
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    // Creates a new mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    private native long map0(int prot, long position, long length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    // Removes an existing mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    private static native int unmap0(long address, long length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    // Transfers from src to dst, or returns -2 if kernel can't do that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
    private native long transferTo0(int src, long position, long count, int dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
    // Sets or reports this file's position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    // If offset is -1, the current position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    // otherwise the position is set to offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    private native long position0(FileDescriptor fd, long offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    // Caches fieldIDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    private static native long initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        Util.load();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        allocationGranularity = initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
}