Search in sources :

Example 1 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException in project robovm by robovm.

the class NonWritableChannelExceptionTest method test_Constructor.

/**
     * @tests {@link java.nio.channels.NonWritableChannelException#NonWritableChannelException()}
     */
public void test_Constructor() {
    NonWritableChannelException e = new NonWritableChannelException();
    assertNull(e.getMessage());
    assertNull(e.getLocalizedMessage());
    assertNull(e.getCause());
}
Also used : NonWritableChannelException(java.nio.channels.NonWritableChannelException)

Example 2 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException in project robovm by robovm.

the class FileChannelTest method test_writeLByteBuffer_ReadOnly.

/**
     * @tests java.nio.channels.FileChannel#write(ByteBuffer)
     */
public void test_writeLByteBuffer_ReadOnly() throws Exception {
    ByteBuffer writeBuffer = ByteBuffer.allocate(CAPACITY);
    try {
        readOnlyFileChannel.write(writeBuffer);
        fail("should throw NonWritableChannelException");
    } catch (NonWritableChannelException e) {
    // expected
    }
    // first throws NonWriteableChannelException
    writeBuffer = null;
    try {
        readOnlyFileChannel.write(writeBuffer);
        fail("should throw NonWritableChannelException");
    } catch (NonWritableChannelException e) {
    // expected
    }
}
Also used : NonWritableChannelException(java.nio.channels.NonWritableChannelException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 3 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException in project robovm by robovm.

the class FileChannelLockingTest method test_tryLockLLZ.

public void test_tryLockLLZ() throws IOException {
    // It is illegal to request an exclusive lock on a read-only channel
    try {
        readOnlyChannel.tryLock(0, 99, false);
        fail("Acquiring exclusive lock on read-only channel should fail");
    } catch (NonWritableChannelException ex) {
    // Expected
    }
    // It is invalid to request a lock starting before the file start
    try {
        readOnlyChannel.tryLock(-99, 0, true);
        fail("Acquiring an illegal lock value should fail.");
    } catch (IllegalArgumentException ex) {
    // expected
    }
    // Acquire a valid lock
    FileLock tmpLock = readOnlyChannel.tryLock(0, 10, true);
    assertTrue(tmpLock.isValid());
    tmpLock.release();
    // Acquire another valid lock -- and don't release it yet
    FileLock lock = readOnlyChannel.tryLock(10, 788, true);
    assertTrue(lock.isValid());
    // Overlapping locks are illegal
    try {
        readOnlyChannel.tryLock(1, 23, true);
        fail("Acquiring an overlapping lock should fail.");
    } catch (OverlappingFileLockException ex) {
    // Expected
    }
    // Adjacent locks are legal
    FileLock adjacentLock = readOnlyChannel.tryLock(1, 3, true);
    assertTrue(adjacentLock.isValid());
    adjacentLock.release();
    // Release longer lived lock
    lock.release();
}
Also used : FileLock(java.nio.channels.FileLock) NonWritableChannelException(java.nio.channels.NonWritableChannelException) OverlappingFileLockException(java.nio.channels.OverlappingFileLockException)

Example 4 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException 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 5 with NonWritableChannelException

use of java.nio.channels.NonWritableChannelException in project j2objc by google.

the class OldRandomAccessFileTest method test_ConstructorLjava_lang_StringLjava_lang_String.

/**
     * java.io.RandomAccessFile#RandomAccessFile(java.lang.String,
     *        java.lang.String)
     */
public void test_ConstructorLjava_lang_StringLjava_lang_String() throws IOException {
    RandomAccessFile raf = null;
    File tmpFile = new File(fileName);
    try {
        raf = new java.io.RandomAccessFile(fileName, "r");
        fail("Test 1: FileNotFoundException expected.");
    } catch (FileNotFoundException e) {
    // Expected.
    } catch (IllegalArgumentException e) {
        fail("Test 2: Unexpected IllegalArgumentException: " + e.getMessage());
    }
    try {
        // Checking the remaining valid mode parameters.
        try {
            raf = new java.io.RandomAccessFile(fileName, "rwd");
        } catch (IllegalArgumentException e) {
            fail("Test 3: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(fileName, "rws");
        } catch (IllegalArgumentException e) {
            fail("Test 4: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        try {
            raf = new java.io.RandomAccessFile(fileName, "rw");
        } catch (IllegalArgumentException e) {
            fail("Test 5: Unexpected IllegalArgumentException: " + e.getMessage());
        }
        raf.close();
        // Checking an invalid mode parameter.
        try {
            raf = new java.io.RandomAccessFile(fileName, "i");
            fail("Test 6: IllegalArgumentException expected.");
        } catch (IllegalArgumentException e) {
        // Expected.
        }
        // Checking for NoWritableChannelException.
        raf = new java.io.RandomAccessFile(fileName, "r");
        FileChannel fcr = raf.getChannel();
        try {
            fcr.lock(0L, Long.MAX_VALUE, false);
            fail("Test 7: NonWritableChannelException expected.");
        } catch (NonWritableChannelException e) {
        // Expected.
        }
    } finally {
        if (raf != null)
            raf.close();
        if (tmpFile.exists())
            tmpFile.delete();
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) NonWritableChannelException(java.nio.channels.NonWritableChannelException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

NonWritableChannelException (java.nio.channels.NonWritableChannelException)22 NonReadableChannelException (java.nio.channels.NonReadableChannelException)11 ErrnoException (libcore.io.ErrnoException)6 FileChannel (java.nio.channels.FileChannel)5 FileLock (java.nio.channels.FileLock)4 MappedByteBuffer (java.nio.MappedByteBuffer)3 StructFlock (libcore.io.StructFlock)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 RandomAccessFile (java.io.RandomAccessFile)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.Test)2 FileDescriptor (java.io.FileDescriptor)1 FileInputStream (java.io.FileInputStream)1 BufferUnderflowException (java.nio.BufferUnderflowException)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