< prev index next > src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java
Print this page
private final FileDescriptor fd;
// File access mode (immutable)
private final boolean writable;
private final boolean readable;
+ private final boolean sync; // O_SYNC or O_DSYNC
// Required to prevent finalization of creating stream (immutable)
private final Closeable parent;
// The path of the referenced file
}
}
}
private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
! boolean writable, boolean direct, Closeable parent)
{
this.fd = fd;
this.path = path;
this.readable = readable;
this.writable = writable;
this.direct = direct;
this.parent = parent;
if (direct) {
assert path != null;
this.alignment = nd.setDirectIO(fd, path);
}
}
}
private FileChannelImpl(FileDescriptor fd, String path, boolean readable,
! boolean writable, boolean sync, boolean direct,
+ Closeable parent)
{
this.fd = fd;
this.path = path;
this.readable = readable;
this.writable = writable;
+ this.sync = sync;
this.direct = direct;
this.parent = parent;
if (direct) {
assert path != null;
this.alignment = nd.setDirectIO(fd, path);
// Used by FileInputStream::getChannel, FileOutputStream::getChannel,
// and RandomAccessFile::getChannel
public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable,
! boolean direct, Closeable parent)
{
! return new FileChannelImpl(fd, path, readable, writable, direct, parent);
}
private void ensureOpen() throws IOException {
if (!isOpen())
throw new ClosedChannelException();
// Used by FileInputStream::getChannel, FileOutputStream::getChannel,
// and RandomAccessFile::getChannel
public static FileChannel open(FileDescriptor fd, String path,
boolean readable, boolean writable,
! boolean sync, boolean direct, Closeable parent)
{
! return new FileChannelImpl(fd, path, readable, writable, sync, direct, parent);
}
private void ensureOpen() throws IOException {
if (!isOpen())
throw new ClosedChannelException();
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! boolean attempted = Blocker.begin(direct);
try {
n = IOUtil.read(fd, dst, -1, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.read(fd, dsts, offset, length, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! boolean attempted = Blocker.begin(direct);
try {
n = IOUtil.read(fd, dsts, offset, length, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.write(fd, src, -1, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! boolean attempted = Blocker.begin(sync || direct);
try {
n = IOUtil.write(fd, src, -1, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.write(fd, srcs, offset, length, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return 0;
do {
! boolean attempted = Blocker.begin(sync || direct);
try {
n = IOUtil.write(fd, srcs, offset, length, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
ti = threads.add();
if (!isOpen())
return 0;
boolean append = fdAccess.getAppend(fd);
do {
! long comp = Blocker.begin();
! try {
- // in append-mode then position is advanced to end before writing
- p = (append) ? nd.size(fd) : nd.seek(fd, -1);
- } finally {
- Blocker.end(comp);
- }
} while ((p == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(p);
} finally {
threads.remove(ti);
endBlocking(p > -1);
ti = threads.add();
if (!isOpen())
return 0;
boolean append = fdAccess.getAppend(fd);
do {
! // in append-mode then position is advanced to end before writing
! p = (append) ? nd.size(fd) : nd.seek(fd, -1);
} while ((p == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(p);
} finally {
threads.remove(ti);
endBlocking(p > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return null;
do {
! long comp = Blocker.begin();
- try {
- p = nd.seek(fd, newPosition);
- } finally {
- Blocker.end(comp);
- }
} while ((p == IOStatus.INTERRUPTED) && isOpen());
return this;
} finally {
threads.remove(ti);
endBlocking(p > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return null;
do {
! p = nd.seek(fd, newPosition);
} while ((p == IOStatus.INTERRUPTED) && isOpen());
return this;
} finally {
threads.remove(ti);
endBlocking(p > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! long comp = Blocker.begin();
- try {
- s = nd.size(fd);
- } finally {
- Blocker.end(comp);
- }
} while ((s == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(s);
} finally {
threads.remove(ti);
endBlocking(s > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! s = nd.size(fd);
} while ((s == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(s);
} finally {
threads.remove(ti);
endBlocking(s > -1);
return null;
// get current size
long size;
do {
! long comp = Blocker.begin();
- try {
- size = nd.size(fd);
- } finally {
- Blocker.end(comp);
- }
} while ((size == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
// get current position
do {
! long comp = Blocker.begin();
- try {
- p = nd.seek(fd, -1);
- } finally {
- Blocker.end(comp);
- }
} while ((p == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
assert p >= 0;
// truncate file if given size is less than the current size
if (newSize < size) {
do {
! long comp = Blocker.begin();
- try {
- rv = nd.truncate(fd, newSize);
- } finally {
- Blocker.end(comp);
- }
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
}
// if position is beyond new size then adjust it
if (p > newSize)
p = newSize;
do {
! long comp = Blocker.begin();
- try {
- rp = nd.seek(fd, p);
- } finally {
- Blocker.end(comp);
- }
} while ((rp == IOStatus.INTERRUPTED) && isOpen());
return this;
} finally {
threads.remove(ti);
endBlocking(rv > -1);
return null;
// get current size
long size;
do {
! size = nd.size(fd);
} while ((size == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
// get current position
do {
! p = nd.seek(fd, -1);
} while ((p == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
assert p >= 0;
// truncate file if given size is less than the current size
if (newSize < size) {
do {
! rv = nd.truncate(fd, newSize);
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
}
// if position is beyond new size then adjust it
if (p > newSize)
p = newSize;
do {
! rp = nd.seek(fd, p);
} while ((rp == IOStatus.INTERRUPTED) && isOpen());
return this;
} finally {
threads.remove(ti);
endBlocking(rv > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return;
do {
! long comp = Blocker.begin();
try {
rv = nd.force(fd, metaData);
} finally {
! Blocker.end(comp);
}
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
} finally {
threads.remove(ti);
endBlocking(rv > -1);
beginBlocking();
ti = threads.add();
if (!isOpen())
return;
do {
! boolean attempted = Blocker.begin();
try {
rv = nd.force(fd, metaData);
} finally {
! Blocker.end(attempted);
}
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
} finally {
threads.remove(ti);
endBlocking(rv > -1);
*/
private long transferToFileDescriptor(long position, int count, FileDescriptor targetFD) {
long n;
boolean append = fdAccess.getAppend(targetFD);
do {
! long comp = Blocker.begin();
- try {
- n = nd.transferTo(fd, position, count, targetFD, append);
- } finally {
- Blocker.end(comp);
- }
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return n;
}
/**
*/
private long transferToFileDescriptor(long position, int count, FileDescriptor targetFD) {
long n;
boolean append = fdAccess.getAppend(targetFD);
do {
! n = nd.transferTo(fd, position, count, targetFD, append);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return n;
}
/**
*/
private long transferFromFileDescriptor(FileDescriptor srcFD, long position, long count) {
long n;
boolean append = fdAccess.getAppend(fd);
do {
! long comp = Blocker.begin();
- try {
- n = nd.transferFrom(srcFD, fd, position, count, append);
- } finally {
- Blocker.end(comp);
- }
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return n;
}
/**
*/
private long transferFromFileDescriptor(FileDescriptor srcFD, long position, long count) {
long n;
boolean append = fdAccess.getAppend(fd);
do {
! n = nd.transferFrom(srcFD, fd, position, count, append);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return n;
}
/**
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.read(fd, dst, position, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! boolean attempted = Blocker.begin(direct);
try {
n = IOUtil.read(fd, dst, position, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! long comp = Blocker.begin();
try {
n = IOUtil.write(fd, src, position, direct, alignment, nd);
} finally {
! Blocker.end(comp);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
beginBlocking();
ti = threads.add();
if (!isOpen())
return -1;
do {
! boolean attempted = Blocker.begin(sync || direct);
try {
n = IOUtil.write(fd, src, position, direct, alignment, nd);
} finally {
! Blocker.end(attempted);
}
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
long mapSize;
int pagePosition;
synchronized (positionLock) {
long filesize;
do {
! long comp = Blocker.begin();
- try {
- filesize = nd.size(fd);
- } finally {
- Blocker.end(comp);
- }
} while ((filesize == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
if (filesize < position + size) { // Extend file size
long mapSize;
int pagePosition;
synchronized (positionLock) {
long filesize;
do {
! filesize = nd.size(fd);
} while ((filesize == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
if (filesize < position + size) { // Extend file size
throw new IOException("Channel not open for writing " +
"- cannot extend file to required size");
}
int rv;
do {
! long comp = Blocker.begin();
- try {
- rv = nd.truncate(fd, position + size);
- } finally {
- Blocker.end(comp);
- }
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
}
throw new IOException("Channel not open for writing " +
"- cannot extend file to required size");
}
int rv;
do {
! rv = nd.truncate(fd, position + size);
} while ((rv == IOStatus.INTERRUPTED) && isOpen());
if (!isOpen())
return null;
}
ti = threads.add();
if (!isOpen())
return null;
int n;
do {
! long comp = Blocker.begin();
try {
n = nd.lock(fd, true, position, size, shared);
} finally {
! Blocker.end(comp);
}
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
if (isOpen()) {
if (n == FileDispatcher.RET_EX_LOCK) {
assert shared;
ti = threads.add();
if (!isOpen())
return null;
int n;
do {
! boolean attempted = Blocker.begin();
try {
n = nd.lock(fd, true, position, size, shared);
} finally {
! Blocker.end(attempted);
}
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
if (isOpen()) {
if (n == FileDispatcher.RET_EX_LOCK) {
assert shared;
< prev index next >