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