Search in sources :

Example 1 with NonReadableChannelException

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());
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException)

Example 2 with NonReadableChannelException

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) {
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 3 with NonReadableChannelException

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();
}
Also used : Path(java.nio.file.Path) NonReadableChannelException(java.nio.channels.NonReadableChannelException) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) NonWritableChannelException(java.nio.channels.NonWritableChannelException) Test(org.junit.Test)

Example 4 with NonReadableChannelException

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) {
    }
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 5 with NonReadableChannelException

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;
}
Also used : NonReadableChannelException(java.nio.channels.NonReadableChannelException) ErrnoException(libcore.io.ErrnoException) StructFlock(libcore.io.StructFlock) FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Aggregations

NonReadableChannelException (java.nio.channels.NonReadableChannelException)18 NonWritableChannelException (java.nio.channels.NonWritableChannelException)11 ErrnoException (libcore.io.ErrnoException)6 IOException (java.io.IOException)3 MappedByteBuffer (java.nio.MappedByteBuffer)3 FileLock (java.nio.channels.FileLock)3 StructFlock (libcore.io.StructFlock)3 ByteBuffer (java.nio.ByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 Test (org.junit.Test)2 FileDescriptor (java.io.FileDescriptor)1 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 ClosedChannelException (java.nio.channels.ClosedChannelException)1 FileLockInterruptionException (java.nio.channels.FileLockInterruptionException)1 Path (java.nio.file.Path)1