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