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());
}
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();
}
}
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());
}
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) {
}
}
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());
}
Aggregations