use of java.nio.channels.NonReadableChannelException in project j2objc by google.
the class FileChannelImpl method map.
public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
checkOpen();
if (mapMode == null) {
throw new NullPointerException("mapMode == null");
}
if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (mapMode != MapMode.READ_ONLY) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
throw new NonReadableChannelException();
}
if (position + size > size()) {
// and we only care about making our backing file longer here.
try {
Libcore.os.ftruncate(fd, position + size);
} catch (ErrnoException ftruncateException) {
// continue on.
try {
if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
throw ftruncateException.rethrowAsIOException();
}
} catch (ErrnoException fstatException) {
throw fstatException.rethrowAsIOException();
}
}
}
long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
int offset = (int) (position - alignment);
MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, ChannelUtils.mapModeToInt(mapMode));
return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), ChannelUtils.mapModeToInt(mapMode));
}
use of java.nio.channels.NonReadableChannelException in project XobotOS by xamarin.
the class FileChannelImpl method map.
public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
checkOpen();
if (mapMode == null) {
throw new NullPointerException("mapMode == null");
}
if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (mapMode != MapMode.READ_ONLY) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
throw new NonReadableChannelException();
}
if (position + size > size()) {
// and we only care about making our backing file longer here.
try {
Libcore.os.ftruncate(fd, position + size);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
}
}
long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
int offset = (int) (position - alignment);
MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
return new MappedByteBufferAdapter(block, (int) size, offset, mapMode);
}
use of java.nio.channels.NonReadableChannelException in project XobotOS by xamarin.
the class FileChannelImpl method basicLock.
private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
int accessMode = (mode & O_ACCMODE);
if (accessMode == O_RDONLY) {
if (!shared) {
throw new NonWritableChannelException();
}
} else if (accessMode == O_WRONLY) {
if (shared) {
throw new NonReadableChannelException();
}
}
if (position < 0 || size < 0) {
throw new IllegalArgumentException("position=" + position + " size=" + size);
}
FileLock pendingLock = new FileLockImpl(this, position, size, shared);
addLock(pendingLock);
StructFlock flock = new StructFlock();
flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
flock.l_whence = (short) SEEK_SET;
flock.l_start = position;
flock.l_len = translateLockLength(size);
boolean success = false;
try {
success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
} catch (ErrnoException errnoException) {
throw errnoException.rethrowAsIOException();
} finally {
if (!success) {
removeLock(pendingLock);
}
}
return success ? pendingLock : null;
}
use of java.nio.channels.NonReadableChannelException in project jdk8u_jdk by JetBrains.
the class FileChannelImpl method lock.
public FileLock lock(long position, long size, boolean shared) throws IOException {
ensureOpen();
if (shared && !readable)
throw new NonReadableChannelException();
if (!shared && !writable)
throw new NonWritableChannelException();
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
FileLockTable flt = fileLockTable();
flt.add(fli);
boolean completed = false;
int ti = -1;
try {
begin();
ti = threads.add();
if (!isOpen())
return null;
int n;
do {
n = nd.lock(fd, true, position, size, shared);
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
if (isOpen()) {
if (n == FileDispatcher.RET_EX_LOCK) {
assert shared;
FileLockImpl fli2 = new FileLockImpl(this, position, size, false);
flt.replace(fli, fli2);
fli = fli2;
}
completed = true;
}
} finally {
if (!completed)
flt.remove(fli);
threads.remove(ti);
try {
end(completed);
} catch (ClosedByInterruptException e) {
throw new FileLockInterruptionException();
}
}
return fli;
}
use of java.nio.channels.NonReadableChannelException in project jdk8u_jdk by JetBrains.
the class FileChannelImpl method read.
public int read(ByteBuffer dst) throws IOException {
ensureOpen();
if (!readable)
throw new NonReadableChannelException();
synchronized (positionLock) {
int n = 0;
int ti = -1;
try {
begin();
ti = threads.add();
if (!isOpen())
return 0;
do {
n = IOUtil.read(fd, dst, -1, nd);
} while ((n == IOStatus.INTERRUPTED) && isOpen());
return IOStatus.normalize(n);
} finally {
threads.remove(ti);
end(n > 0);
assert IOStatus.check(n);
}
}
}
Aggregations