use of java.nio.channels.NonReadableChannelException in project robovm by robovm.
the class NonReadableChannelExceptionTest method test_Constructor.
/**
* @tests {@link java.nio.channels.NonReadableChannelException#NonReadableChannelException()}
*/
public void test_Constructor() {
NonReadableChannelException e = new NonReadableChannelException();
assertNull(e.getMessage());
assertNull(e.getLocalizedMessage());
assertNull(e.getCause());
}
use of java.nio.channels.NonReadableChannelException in project robovm by robovm.
the class FileChannelTest method test_readLByteBuffer_WriteOnly.
public void test_readLByteBuffer_WriteOnly() throws Exception {
ByteBuffer readBuffer = ByteBuffer.allocate(CAPACITY);
try {
writeOnlyFileChannel.read(readBuffer);
fail();
} catch (NonReadableChannelException expected) {
}
readBuffer = null;
try {
writeOnlyFileChannel.read(readBuffer);
fail();
} catch (NonReadableChannelException expected) {
} catch (NullPointerException expected) {
}
}
use of java.nio.channels.NonReadableChannelException in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testRegularFileAccessAndModifiedTimeUpdates.
@Test
public void testRegularFileAccessAndModifiedTimeUpdates() throws IOException {
Path foo = path("foo");
Files.createFile(foo);
FileTimeTester tester = new FileTimeTester(foo);
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try (FileChannel channel = FileChannel.open(foo, READ)) {
// opening READ channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.read(ByteBuffer.allocate(100));
// read call on channel does
tester.assertAccessTimeChanged();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.read(ByteBuffer.allocate(100));
tester.assertAccessTimeChanged();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try {
channel.write(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3 }));
} catch (NonWritableChannelException ignore) {
}
// failed write on non-readable channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
}
// closing channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try (FileChannel channel = FileChannel.open(foo, WRITE)) {
// opening WRITE channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.write(ByteBuffer.wrap(new byte[] { 0, 1, 2, 3 }));
// write call on channel does
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
channel.write(ByteBuffer.wrap(new byte[] { 4, 5, 6, 7 }));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
try {
channel.read(ByteBuffer.allocate(100));
} catch (NonReadableChannelException ignore) {
}
// failed read on non-readable channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
}
// closing channel does not change times
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
}
use of java.nio.channels.NonReadableChannelException in project jimfs by google.
the class JimfsFileChannelTest method testReadsInWriteOnlyMode.
@Test
public void testReadsInWriteOnlyMode() throws IOException {
FileChannel channel = channel(regularFile(0), WRITE);
try {
channel.read(buffer("111"));
fail();
} catch (NonReadableChannelException expected) {
}
try {
channel.read(buffer("111"), 10);
fail();
} catch (NonReadableChannelException expected) {
}
try {
channel.read(new ByteBuffer[] { buffer("111"), buffer("111") });
fail();
} catch (NonReadableChannelException expected) {
}
try {
channel.read(new ByteBuffer[] { buffer("111"), buffer("111") }, 0, 2);
fail();
} catch (NonReadableChannelException expected) {
}
try {
channel.transferTo(0, 10, new ByteBufferChannel(buffer("111")));
fail();
} catch (NonReadableChannelException expected) {
}
try {
channel.lock(0, 10, true);
fail();
} catch (NonReadableChannelException expected) {
}
}
use of java.nio.channels.NonReadableChannelException in project j2objc by google.
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;
}
Aggregations