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