2
* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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).
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.
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
28
import cli.Microsoft.Win32.SafeHandles.SafeFileHandle;
29
import cli.System.IntPtr;
30
import cli.System.IO.FileStream;
31
import cli.System.Runtime.InteropServices.DllImportAttribute;
32
import cli.System.Runtime.InteropServices.StructLayoutAttribute;
33
import cli.System.Runtime.InteropServices.LayoutKind;
34
import java.io.FileDescriptor;
35
import java.io.IOException;
36
import java.nio.ByteBuffer;
37
import java.nio.MappedByteBuffer;
38
import java.nio.channels.*;
39
import java.util.ArrayList;
40
import java.util.List;
41
import java.security.AccessController;
42
import sun.misc.Cleaner;
43
import sun.security.action.GetPropertyAction;
45
public class FileChannelImpl
48
private static final boolean win32 = ikvm.internal.Util.WINDOWS;
50
// Memory allocation size for mapping buffers
51
private static final long allocationGranularity = 64 * 1024; // HACK we're using a hard coded value here that works on all mainstream platforms
53
// Used to make native read and write calls
54
private final FileDispatcher nd;
57
private final FileDescriptor fd;
59
// File access mode (immutable)
60
private final boolean writable;
61
private final boolean readable;
62
private final boolean append;
64
// Required to prevent finalization of creating stream (immutable)
65
private final Object parent;
67
// Thread-safe set of IDs of native threads, for signalling
68
private final NativeThreadSet threads = new NativeThreadSet(2);
70
// Lock for operations involving position and size
71
private final Object positionLock = new Object();
73
private FileChannelImpl(FileDescriptor fd, boolean readable,
74
boolean writable, boolean append, Object parent)
77
this.readable = readable;
78
this.writable = writable;
81
this.nd = new FileDispatcherImpl(append);
84
// Used by FileInputStream.getChannel() and RandomAccessFile.getChannel()
85
public static FileChannel open(FileDescriptor fd,
86
boolean readable, boolean writable,
89
return new FileChannelImpl(fd, readable, writable, false, parent);
92
// Used by FileOutputStream.getChannel
93
public static FileChannel open(FileDescriptor fd,
94
boolean readable, boolean writable,
95
boolean append, Object parent)
97
return new FileChannelImpl(fd, readable, writable, append, parent);
100
private void ensureOpen() throws IOException {
102
throw new ClosedChannelException();
106
// -- Standard channel operations --
108
protected void implCloseChannel() throws IOException {
109
// Release and invalidate any locks that we still hold
110
if (fileLockTable != null) {
111
for (FileLock fl: fileLockTable.removeAll()) {
114
nd.release(fd, fl.position(), fl.size());
115
((FileLockImpl)fl).invalidate();
122
threads.signalAndWait();
124
if (parent != null) {
126
// Close the fd via the parent stream's close method. The parent
127
// will reinvoke our close method, which is defined in the
128
// superclass AbstractInterruptibleChannel, but the isOpen logic in
129
// that method will prevent this method from being reinvoked.
131
((java.io.Closeable)parent).close();
138
public int read(ByteBuffer dst) throws IOException {
141
throw new NonReadableChannelException();
142
synchronized (positionLock) {
151
n = IOUtil.read(fd, dst, -1, nd, positionLock);
152
} while ((n == IOStatus.INTERRUPTED) && isOpen());
153
return IOStatus.normalize(n);
157
assert IOStatus.check(n);
162
public long read(ByteBuffer[] dsts, int offset, int length)
165
if ((offset < 0) || (length < 0) || (offset > dsts.length - length))
166
throw new IndexOutOfBoundsException();
169
throw new NonReadableChannelException();
170
synchronized (positionLock) {
179
n = IOUtil.read(fd, dsts, offset, length, nd);
180
} while ((n == IOStatus.INTERRUPTED) && isOpen());
181
return IOStatus.normalize(n);
185
assert IOStatus.check(n);
190
public int write(ByteBuffer src) throws IOException {
193
throw new NonWritableChannelException();
194
synchronized (positionLock) {
203
n = IOUtil.write(fd, src, -1, nd, positionLock);
204
} while ((n == IOStatus.INTERRUPTED) && isOpen());
205
return IOStatus.normalize(n);
209
assert IOStatus.check(n);
214
public long write(ByteBuffer[] srcs, int offset, int length)
217
if ((offset < 0) || (length < 0) || (offset > srcs.length - length))
218
throw new IndexOutOfBoundsException();
221
throw new NonWritableChannelException();
222
synchronized (positionLock) {
231
n = IOUtil.write(fd, srcs, offset, length, nd);
232
} while ((n == IOStatus.INTERRUPTED) && isOpen());
233
return IOStatus.normalize(n);
237
assert IOStatus.check(n);
242
// -- Other operations --
244
public long position() throws IOException {
246
synchronized (positionLock) {
255
// in append-mode then position is advanced to end before writing
256
p = (append) ? nd.size(fd) : position0(fd, -1);
257
} while ((p == IOStatus.INTERRUPTED) && isOpen());
258
return IOStatus.normalize(p);
262
assert IOStatus.check(p);
267
public FileChannel position(long newPosition) throws IOException {
270
throw new IllegalArgumentException();
271
synchronized (positionLock) {
280
p = position0(fd, newPosition);
281
} while ((p == IOStatus.INTERRUPTED) && isOpen());
286
assert IOStatus.check(p);
291
public long size() throws IOException {
293
synchronized (positionLock) {
303
} while ((s == IOStatus.INTERRUPTED) && isOpen());
304
return IOStatus.normalize(s);
308
assert IOStatus.check(s);
313
public FileChannel truncate(long size) throws IOException {
316
throw new IllegalArgumentException();
320
throw new NonWritableChannelException();
321
synchronized (positionLock) {
331
// get current position
333
p = position0(fd, -1);
334
} while ((p == IOStatus.INTERRUPTED) && isOpen());
341
rv = nd.truncate(fd, size);
342
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
346
// [IKVM] in append mode we're not allowed to seek backwards, but the atomic append will honor the new file size
350
// set position to size if greater than size
354
rv = (int)position0(fd, p);
355
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
360
assert IOStatus.check(rv);
365
public void force(boolean metaData) throws IOException {
375
rv = nd.force(fd, metaData);
376
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
380
assert IOStatus.check(rv);
384
private long transferToArbitraryChannel(long position, int icount,
385
WritableByteChannel target)
388
// Untrusted target: Use a newly-erased buffer
389
int c = Math.min(icount, TRANSFER_SIZE);
390
ByteBuffer bb = ByteBuffer.allocate(c);
391
long tw = 0; // Total bytes written
394
while (tw < icount) {
395
bb.limit(Math.min((int)(icount - tw), TRANSFER_SIZE));
396
int nr = read(bb, pos);
400
// ## Bug: Will block writing target if this channel
401
// ## is asynchronously closed
402
int nw = target.write(bb);
410
} catch (IOException x) {
417
public long transferTo(long position, long count,
418
WritableByteChannel target)
422
if (!target.isOpen())
423
throw new ClosedChannelException();
425
throw new NonReadableChannelException();
426
if (target instanceof FileChannelImpl &&
427
!((FileChannelImpl)target).writable)
428
throw new NonWritableChannelException();
429
if ((position < 0) || (count < 0))
430
throw new IllegalArgumentException();
434
int icount = (int)Math.min(count, Integer.MAX_VALUE);
435
if ((sz - position) < icount)
436
icount = (int)(sz - position);
438
// Slow path for untrusted targets
439
return transferToArbitraryChannel(position, icount, target);
442
private long transferFromFileChannel(FileChannelImpl src,
443
long position, long count)
447
throw new NonReadableChannelException();
448
return transferFromArbitraryChannel(src, position, count);
451
private static final int TRANSFER_SIZE = 8192;
453
private long transferFromArbitraryChannel(ReadableByteChannel src,
454
long position, long count)
457
// Untrusted target: Use a newly-erased buffer
458
int c = (int)Math.min(count, TRANSFER_SIZE);
459
ByteBuffer bb = ByteBuffer.allocate(c);
460
long tw = 0; // Total bytes written
464
bb.limit((int)Math.min((count - tw), (long)TRANSFER_SIZE));
465
// ## Bug: Will block reading src if this channel
466
// ## is asynchronously closed
467
int nr = src.read(bb);
471
int nw = write(bb, pos);
479
} catch (IOException x) {
486
public long transferFrom(ReadableByteChannel src,
487
long position, long count)
492
throw new ClosedChannelException();
494
throw new NonWritableChannelException();
495
if ((position < 0) || (count < 0))
496
throw new IllegalArgumentException();
497
if (position > size())
499
if (src instanceof FileChannelImpl)
500
return transferFromFileChannel((FileChannelImpl)src,
503
return transferFromArbitraryChannel(src, position, count);
506
public int read(ByteBuffer dst, long position) throws IOException {
508
throw new NullPointerException();
510
throw new IllegalArgumentException("Negative position");
512
throw new NonReadableChannelException();
522
n = IOUtil.read(fd, dst, position, nd, positionLock);
523
} while ((n == IOStatus.INTERRUPTED) && isOpen());
524
return IOStatus.normalize(n);
528
assert IOStatus.check(n);
532
public int write(ByteBuffer src, long position) throws IOException {
534
throw new NullPointerException();
536
throw new IllegalArgumentException("Negative position");
538
throw new NonWritableChannelException();
548
n = IOUtil.write(fd, src, position, nd, positionLock);
549
} while ((n == IOStatus.INTERRUPTED) && isOpen());
550
return IOStatus.normalize(n);
554
assert IOStatus.check(n);
559
// -- Memory-mapped buffers --
561
private static class Unmapper
564
// may be required to close file
565
private static final NativeDispatcher nd = new FileDispatcherImpl();
567
// keep track of mapped buffer usage
568
static volatile int count;
569
static volatile long totalSize;
570
static volatile long totalCapacity;
572
private volatile long address;
573
private final long size;
574
private final int cap;
575
private final FileDescriptor fd;
577
private Unmapper(long address, long size, int cap,
580
assert (address != 0);
581
this.address = address;
586
synchronized (Unmapper.class) {
589
totalCapacity += cap;
596
unmap0(address, size);
599
// if this mapping has a valid file descriptor then we close it
603
} catch (IOException ignore) {
608
synchronized (Unmapper.class) {
611
totalCapacity -= cap;
616
private static void unmap(MappedByteBuffer bb) {
617
Cleaner cl = ((DirectBuffer)bb).cleaner();
622
private static final int MAP_RO = 0;
623
private static final int MAP_RW = 1;
624
private static final int MAP_PV = 2;
626
public MappedByteBuffer map(MapMode mode, long position, long size)
631
throw new IllegalArgumentException("Negative position");
633
throw new IllegalArgumentException("Negative size");
634
if (position + size < 0)
635
throw new IllegalArgumentException("Position + size overflow");
636
if (size > Integer.MAX_VALUE)
637
throw new IllegalArgumentException("Size exceeds Integer.MAX_VALUE");
639
if (mode == MapMode.READ_ONLY)
641
else if (mode == MapMode.READ_WRITE)
643
else if (mode == MapMode.PRIVATE)
646
if ((mode != MapMode.READ_ONLY) && !writable)
647
throw new NonWritableChannelException();
649
throw new NonReadableChannelException();
658
if (size() < position + size) { // Extend file size
660
throw new IOException("Channel not open for writing " +
661
"- cannot extend file to required size");
665
rv = nd.truncate(fd, position + size);
666
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
670
// a valid file descriptor is not required
671
FileDescriptor dummy = new FileDescriptor();
672
if ((!writable) || (imode == MAP_RO))
673
return Util.newMappedByteBufferR(0, 0, dummy, null);
675
return Util.newMappedByteBuffer(0, 0, dummy, null);
678
int pagePosition = (int)(position % allocationGranularity);
679
long mapPosition = position - pagePosition;
680
long mapSize = size + pagePosition;
682
// If no exception was thrown from map0, the address is valid
683
addr = map0(imode, mapPosition, mapSize);
684
} catch (OutOfMemoryError x) {
685
// An OutOfMemoryError may indicate that we've exhausted memory
686
// so force gc and re-attempt map
690
} catch (InterruptedException y) {
691
Thread.currentThread().interrupt();
694
addr = map0(imode, mapPosition, mapSize);
695
} catch (OutOfMemoryError y) {
696
// After a second OOME, fail
697
throw new IOException("Map failed", y);
701
// On Windows, and potentially other platforms, we need an open
702
// file descriptor for some mapping operations.
705
mfd = nd.duplicateForMapping(fd);
706
} catch (IOException ioe) {
707
unmap0(addr, mapSize);
711
assert (IOStatus.checkAll(addr));
712
assert (addr % allocationGranularity == 0);
713
int isize = (int)size;
714
Unmapper um = new Unmapper(addr, mapSize, isize, mfd);
715
if ((!writable) || (imode == MAP_RO)) {
716
return Util.newMappedByteBufferR(isize,
721
return Util.newMappedByteBuffer(isize,
728
end(IOStatus.checkAll(addr));
733
* Invoked by sun.management.ManagementFactoryHelper to create the management
734
* interface for mapped buffers.
736
public static sun.misc.JavaNioAccess.BufferPool getMappedBufferPool() {
737
return new sun.misc.JavaNioAccess.BufferPool() {
739
public String getName() {
743
public long getCount() {
744
return Unmapper.count;
747
public long getTotalCapacity() {
748
return Unmapper.totalCapacity;
751
public long getMemoryUsed() {
752
return Unmapper.totalSize;
761
// keeps track of locks on this file
762
private volatile FileLockTable fileLockTable;
764
// indicates if file locks are maintained system-wide (as per spec)
765
private static boolean isSharedFileLockTable;
767
// indicates if the disableSystemWideOverlappingFileLockCheck property
769
private static volatile boolean propertyChecked;
771
// The lock list in J2SE 1.4/5.0 was local to each FileChannel instance so
772
// the overlap check wasn't system wide when there were multiple channels to
773
// the same file. This property is used to get 1.4/5.0 behavior if desired.
774
private static boolean isSharedFileLockTable() {
775
if (!propertyChecked) {
776
synchronized (FileChannelImpl.class) {
777
if (!propertyChecked) {
778
String value = AccessController.doPrivileged(
779
new GetPropertyAction(
780
"sun.nio.ch.disableSystemWideOverlappingFileLockCheck"));
781
isSharedFileLockTable = ((value == null) || value.equals("false"));
782
propertyChecked = true;
786
return isSharedFileLockTable;
789
private FileLockTable fileLockTable() throws IOException {
790
if (fileLockTable == null) {
791
synchronized (this) {
792
if (fileLockTable == null) {
793
if (isSharedFileLockTable()) {
794
int ti = threads.add();
797
fileLockTable = FileLockTable.newSharedFileLockTable(this, fd);
802
fileLockTable = new SimpleFileLockTable();
807
return fileLockTable;
810
public FileLock lock(long position, long size, boolean shared)
814
if (shared && !readable)
815
throw new NonReadableChannelException();
816
if (!shared && !writable)
817
throw new NonWritableChannelException();
818
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
819
FileLockTable flt = fileLockTable();
821
boolean completed = false;
830
n = nd.lock(fd, true, position, size, shared);
831
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
833
if (n == FileDispatcher.RET_EX_LOCK) {
835
FileLockImpl fli2 = new FileLockImpl(this, position, size,
837
flt.replace(fli, fli2);
848
} catch (ClosedByInterruptException e) {
849
throw new FileLockInterruptionException();
855
public FileLock tryLock(long position, long size, boolean shared)
859
if (shared && !readable)
860
throw new NonReadableChannelException();
861
if (!shared && !writable)
862
throw new NonWritableChannelException();
863
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
864
FileLockTable flt = fileLockTable();
868
int ti = threads.add();
872
result = nd.lock(fd, false, position, size, shared);
873
} catch (IOException e) {
877
if (result == FileDispatcher.NO_LOCK) {
881
if (result == FileDispatcher.RET_EX_LOCK) {
883
FileLockImpl fli2 = new FileLockImpl(this, position, size,
885
flt.replace(fli, fli2);
894
void release(FileLockImpl fli) throws IOException {
895
int ti = threads.add();
898
nd.release(fd, fli.position(), fli.size());
902
assert fileLockTable != null;
903
fileLockTable.remove(fli);
906
// -- File lock support --
909
* A simple file lock table that maintains a list of FileLocks obtained by a
910
* FileChannel. Use to get 1.4/5.0 behaviour.
912
private static class SimpleFileLockTable extends FileLockTable {
913
// synchronize on list for access
914
private final List<FileLock> lockList = new ArrayList<FileLock>(2);
916
public SimpleFileLockTable() {
919
private void checkList(long position, long size)
920
throws OverlappingFileLockException
922
assert Thread.holdsLock(lockList);
923
for (FileLock fl: lockList) {
924
if (fl.overlaps(position, size)) {
925
throw new OverlappingFileLockException();
930
public void add(FileLock fl) throws OverlappingFileLockException {
931
synchronized (lockList) {
932
checkList(fl.position(), fl.size());
937
public void remove(FileLock fl) {
938
synchronized (lockList) {
943
public List<FileLock> removeAll() {
944
synchronized(lockList) {
945
List<FileLock> result = new ArrayList<FileLock>(lockList);
951
public void replace(FileLock fl1, FileLock fl2) {
952
synchronized (lockList) {
953
lockList.remove(fl1);
959
// -- Native methods --
961
// Creates a new mapping
962
private long map0(int prot, long position, long length) throws IOException
964
FileStream fs = (FileStream)fd.getStream();
966
return mapViewOfFileWin32(fs, prot, position, length);
968
return mapViewOfFilePosix(fs, prot, position, length);
971
@cli.System.Security.SecuritySafeCriticalAttribute.Annotation
972
private static long mapViewOfFileWin32(FileStream fs, int prot, long position, long length) throws IOException
976
int PAGE_READONLY = 2;
977
int PAGE_READWRITE = 4;
978
int PAGE_WRITECOPY = 8;
980
int FILE_MAP_WRITE = 2;
981
int FILE_MAP_READ = 4;
982
int FILE_MAP_COPY = 1;
990
fileProtect = PAGE_READONLY;
991
mapAccess = FILE_MAP_READ;
994
fileProtect = PAGE_READWRITE;
995
mapAccess = FILE_MAP_WRITE;
998
fileProtect = PAGE_WRITECOPY;
999
mapAccess = FILE_MAP_COPY;
1005
long maxSize = length + position;
1006
SafeFileHandle hFileMapping = CreateFileMapping(fs.get_SafeFileHandle(), IntPtr.Zero, fileProtect, (int)(maxSize >> 32), (int)maxSize, null);
1007
int err = cli.System.Runtime.InteropServices.Marshal.GetLastWin32Error();
1008
if (hFileMapping.get_IsInvalid())
1010
throw new IOException("Win32 error " + err);
1012
IntPtr p = MapViewOfFile(hFileMapping, mapAccess, (int)(position >> 32), (int)position, IntPtr.op_Explicit(length));
1013
err = cli.System.Runtime.InteropServices.Marshal.GetLastWin32Error();
1014
hFileMapping.Close();
1015
if (p.Equals(IntPtr.Zero))
1017
if (err == 8 /*ERROR_NOT_ENOUGH_MEMORY*/)
1019
throw new OutOfMemoryError("Map failed");
1021
throw new IOException("Win32 error " + err);
1023
cli.System.GC.AddMemoryPressure(length);
1028
cli.System.GC.KeepAlive(fs);
1032
@cli.System.Security.SecuritySafeCriticalAttribute.Annotation
1033
private static long mapViewOfFilePosix(FileStream fs, int prot, long position, long length) throws IOException
1035
byte writeable = prot != MAP_RO ? (byte)1 : (byte)0;
1036
byte copy_on_write = prot == MAP_PV ? (byte)1 : (byte)0;
1037
IntPtr p = ikvm_mmap(fs.get_SafeFileHandle(), writeable, copy_on_write, position, (int)length);
1038
cli.System.GC.KeepAlive(fs);
1039
// HACK ikvm_mmap should really be changed to return a null pointer on failure,
1040
// instead of whatever MAP_FAILED is defined to on the particular system we're running on,
1041
// common values for MAP_FAILED are 0 and -1, so we test for these.
1042
if (p.Equals(IntPtr.Zero) || p.Equals(new IntPtr(-1)))
1044
throw new IOException("file mapping failed");
1046
cli.System.GC.AddMemoryPressure(length);
1050
@DllImportAttribute.Annotation(value="kernel32", SetLastError=true)
1051
private static native SafeFileHandle CreateFileMapping(SafeFileHandle hFile, IntPtr lpAttributes, int flProtect, int dwMaximumSizeHigh, int dwMaximumSizeLow, String lpName);
1053
@DllImportAttribute.Annotation(value="kernel32", SetLastError=true)
1054
private static native IntPtr MapViewOfFile(SafeFileHandle hFileMapping, int dwDesiredAccess, int dwFileOffsetHigh, int dwFileOffsetLow, IntPtr dwNumberOfBytesToMap);
1056
@DllImportAttribute.Annotation("kernel32")
1057
private static native int UnmapViewOfFile(IntPtr lpBaseAddress);
1059
@DllImportAttribute.Annotation("ikvm-native")
1060
private static native int ikvm_munmap(IntPtr address, int size);
1062
@DllImportAttribute.Annotation("ikvm-native")
1063
private static native IntPtr ikvm_mmap(SafeFileHandle handle, byte writeable, byte copy_on_write, long position, int size);
1065
// Removes an existing mapping
1066
@cli.System.Security.SecuritySafeCriticalAttribute.Annotation
1067
static int unmap0(long address, long length)
1070
UnmapViewOfFile(IntPtr.op_Explicit(address));
1072
ikvm_munmap(IntPtr.op_Explicit(address), (int)length);
1073
cli.System.GC.RemoveMemoryPressure(length);
1077
// Sets or reports this file's position
1078
// If offset is -1, the current position is returned
1079
// otherwise the position is set to offset
1080
private static long position0(FileDescriptor fd, long offset) throws IOException
1084
return fd.getFilePointer();