Search in sources :

Example 51 with FileChannel

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

the class JimfsFileChannelTest method testRead.

@Test
public void testRead() throws IOException {
    RegularFile file = regularFile(20);
    FileChannel channel = channel(file, READ);
    assertEquals(0, channel.position());
    ByteBuffer buf = buffer("1234567890");
    ByteBuffer buf2 = buffer("123457890");
    assertEquals(10, channel.read(buf));
    assertEquals(10, channel.position());
    buf.flip();
    assertEquals(10, channel.read(new ByteBuffer[] { buf, buf2 }));
    assertEquals(20, channel.position());
    buf.flip();
    buf2.flip();
    file.write(20, new byte[10], 0, 10);
    assertEquals(10, channel.read(new ByteBuffer[] { buf, buf2 }, 0, 2));
    assertEquals(30, channel.position());
    buf.flip();
    assertEquals(10, channel.read(buf, 5));
    assertEquals(30, channel.position());
    buf.flip();
    assertEquals(-1, channel.read(buf));
    assertEquals(30, channel.position());
}
Also used : FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 52 with FileChannel

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

the class JimfsFileChannelTest method assertClosedByInterrupt.

/**
   * Asserts that when the given operation is run on an interrupted thread,
   * {@code ClosedByInterruptException} is thrown, the channel is closed and the thread is no
   * longer interrupted.
   */
private static void assertClosedByInterrupt(FileChannelMethod method) throws IOException {
    FileChannel channel = channel(regularFile(10), READ, WRITE);
    Thread.currentThread().interrupt();
    try {
        method.call(channel);
        fail("expected the method to throw ClosedByInterruptException or " + "FileLockInterruptionException");
    } catch (ClosedByInterruptException | FileLockInterruptionException expected) {
        assertFalse("expected the channel to be closed", channel.isOpen());
        assertTrue("expected the thread to still be interrupted", Thread.interrupted());
    } finally {
        // ensure the thread isn't interrupted when this method returns
        Thread.interrupted();
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) FileLockInterruptionException(java.nio.channels.FileLockInterruptionException) FileChannel(java.nio.channels.FileChannel)

Example 53 with FileChannel

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

the class JimfsFileChannelTest method testSize.

@Test
public void testSize() throws IOException {
    RegularFile file = regularFile(10);
    FileChannel channel = channel(file, READ);
    assertEquals(10, channel.size());
    file.write(10, new byte[90], 0, 90);
    assertEquals(100, channel.size());
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 54 with FileChannel

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

the class JimfsFileChannelTest method testTransferFromNegative.

@Test
public void testTransferFromNegative() throws IOException {
    FileChannel channel = channel(regularFile(0), READ, WRITE);
    try {
        channel.transferFrom(new ByteBufferChannel(10), -1, 0);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        channel.transferFrom(new ByteBufferChannel(10), 0, -1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 55 with FileChannel

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

the class JimfsFileChannelTest method testTransferFrom.

@Test
public void testTransferFrom() throws IOException {
    RegularFile file = regularFile(0);
    FileChannel channel = channel(file, WRITE);
    ByteBufferChannel readChannel = new ByteBufferChannel(buffer("1234567890"));
    assertEquals(10, channel.transferFrom(readChannel, 0, 100));
    assertEquals(0, channel.position());
}
Also used : FileChannel(java.nio.channels.FileChannel) 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