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