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