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