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