Search in sources :

Example 46 with FileChannel

use of java.nio.channels.FileChannel in project jimfs by google.

the class JimfsFileChannelTest method testAppend.

@Test
public void testAppend() throws IOException {
    RegularFile file = regularFile(0);
    FileChannel channel = channel(file, WRITE, APPEND);
    assertEquals(0, channel.position());
    ByteBuffer buf = buffer("1234567890");
    ByteBuffer buf2 = buffer("1234567890");
    assertEquals(10, channel.write(buf));
    assertEquals(10, channel.position());
    buf.flip();
    channel.position(0);
    assertEquals(20, channel.write(new ByteBuffer[] { buf, buf2 }));
    assertEquals(30, channel.position());
    buf.flip();
    buf2.flip();
    channel.position(0);
    assertEquals(20, channel.write(new ByteBuffer[] { buf, buf2 }, 0, 2));
    assertEquals(50, channel.position());
    buf.flip();
    channel.position(0);
    assertEquals(10, channel.write(buf, 5));
    assertEquals(60, channel.position());
    buf.flip();
    channel.position(0);
    assertEquals(10, channel.transferFrom(new ByteBufferChannel(buf), 0, 10));
    assertEquals(70, channel.position());
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 47 with FileChannel

use of java.nio.channels.FileChannel in project jimfs by google.

the class JimfsFileChannelTest method testTransferTo.

@Test
public void testTransferTo() throws IOException {
    RegularFile file = regularFile(10);
    FileChannel channel = channel(file, READ);
    ByteBufferChannel writeChannel = new ByteBufferChannel(buffer("1234567890"));
    assertEquals(10, channel.transferTo(0, 100, writeChannel));
    assertEquals(0, channel.position());
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 48 with FileChannel

use of java.nio.channels.FileChannel 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 49 with FileChannel

use of java.nio.channels.FileChannel in project jimfs by google.

the class JimfsFileChannelTest method testWriteNegative.

@Test
public void testWriteNegative() throws IOException {
    FileChannel channel = channel(regularFile(0), READ, WRITE);
    try {
        channel.write(buffer("111"), -1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    ByteBuffer[] bufs = { buffer("111"), buffer("111") };
    try {
        channel.write(bufs, -1, 10);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
    try {
        channel.write(bufs, 0, -1);
        fail();
    } catch (IndexOutOfBoundsException expected) {
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 50 with FileChannel

use of java.nio.channels.FileChannel in project jimfs by google.

the class JimfsFileChannelTest method testCloseByInterrupt.

@Test
public void testCloseByInterrupt() throws Exception {
    RegularFile file = regularFile(10);
    final FileChannel channel = channel(file, READ, WRITE);
    // ensure all operations on the channel will block
    file.writeLock().lock();
    ExecutorService executor = Executors.newCachedThreadPool();
    final CountDownLatch threadStartLatch = new CountDownLatch(1);
    final SettableFuture<Throwable> interruptException = SettableFuture.create();
    // This thread, being the first to run, will be blocking on the interruptible lock (the byte
    // file's write lock) and as such will be interrupted properly... the other threads will be
    // blocked on the lock that guards the position field and the specification that only one method
    // on the channel will be in progress at a time. That lock is not interruptible, so we must
    // interrupt this thread.
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            threadStartLatch.countDown();
            try {
                channel.write(ByteBuffer.allocate(20));
                interruptException.set(null);
            } catch (Throwable e) {
                interruptException.set(e);
            }
        }
    });
    thread.start();
    // let the thread start running
    threadStartLatch.await();
    // then ensure time for thread to start blocking on the write lock
    Uninterruptibles.sleepUninterruptibly(10, MILLISECONDS);
    CountDownLatch blockingStartLatch = new CountDownLatch(BLOCKING_OP_COUNT);
    List<Future<?>> futures = queueAllBlockingOperations(channel, executor, blockingStartLatch);
    // wait for all blocking threads to start
    blockingStartLatch.await();
    // then ensure time for the operations to start blocking
    Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
    // interrupting this blocking thread closes the channel and makes all the other threads
    // throw AsynchronousCloseException... the operation on this thread should throw
    // ClosedByInterruptException
    thread.interrupt();
    // get the exception that caused the interrupted operation to terminate
    assertThat(interruptException.get(200, MILLISECONDS)).named("interrupted thread exception").isInstanceOf(ClosedByInterruptException.class);
    // different thread, closed the channel)
    for (Future<?> future : futures) {
        try {
            future.get();
            fail();
        } catch (ExecutionException expected) {
            assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
        }
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) FileChannel(java.nio.channels.FileChannel) CountDownLatch(java.util.concurrent.CountDownLatch) ExecutorService(java.util.concurrent.ExecutorService) SettableFuture(com.google.common.util.concurrent.SettableFuture) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9