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