use of java.nio.channels.FileChannel in project j2objc by google.
the class FileChannelTest method useFileChannel.
private void useFileChannel() throws IOException {
File file = File.createTempFile("j2objc", "tmp");
ChannelGetter channelGetter = new ChannelGetter();
channelGetter.createChannel(file);
// The FileOutputStream used to create the channel is released at this point.
FileChannel channel = channelGetter.get();
FileLock lock = channel.lock();
lock.close();
channel.close();
assertTrue(file.delete());
}
use of java.nio.channels.FileChannel 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();
}
use of java.nio.channels.FileChannel in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testChannels.
@Test
public void testChannels() throws IOException {
try (FileChannel channel = FileChannel.open(path("/test.txt"), CREATE_NEW, WRITE)) {
ByteBuffer buf1 = UTF_8.encode("hello");
ByteBuffer buf2 = UTF_8.encode(" world");
while (buf1.hasRemaining() || buf2.hasRemaining()) {
channel.write(new ByteBuffer[] { buf1, buf2 });
}
assertEquals(11, channel.position());
assertEquals(11, channel.size());
channel.write(UTF_8.encode("!"));
assertEquals(12, channel.position());
assertEquals(12, channel.size());
}
try (SeekableByteChannel channel = Files.newByteChannel(path("/test.txt"), READ)) {
assertEquals(0, channel.position());
assertEquals(12, channel.size());
ByteBuffer buffer = ByteBuffer.allocate(100);
while (channel.read(buffer) != -1) {
}
buffer.flip();
assertEquals("hello world!", UTF_8.decode(buffer).toString());
}
byte[] bytes = preFilledBytes(100);
Files.write(path("/test"), bytes);
try (SeekableByteChannel channel = Files.newByteChannel(path("/test"), READ, WRITE)) {
ByteBuffer buffer = ByteBuffer.wrap(preFilledBytes(50));
channel.position(50);
channel.write(buffer);
buffer.flip();
channel.write(buffer);
channel.position(0);
ByteBuffer readBuffer = ByteBuffer.allocate(150);
while (readBuffer.hasRemaining()) {
channel.read(readBuffer);
}
byte[] expected = Bytes.concat(preFilledBytes(50), preFilledBytes(50), preFilledBytes(50));
assertArrayEquals(expected, readBuffer.array());
}
try (FileChannel channel = FileChannel.open(path("/test"), READ, WRITE)) {
assertEquals(150, channel.size());
channel.truncate(10);
assertEquals(10, channel.size());
ByteBuffer buffer = ByteBuffer.allocate(20);
assertEquals(10, channel.read(buffer));
buffer.flip();
byte[] expected = new byte[20];
System.arraycopy(preFilledBytes(10), 0, expected, 0, 10);
assertArrayEquals(expected, buffer.array());
}
}
use of java.nio.channels.FileChannel in project jimfs by google.
the class JimfsUnixLikeFileSystemTest method testOpenFile_withReadAndTruncateExisting_doesNotTruncateFile.
@Test
public void testOpenFile_withReadAndTruncateExisting_doesNotTruncateFile() throws IOException {
byte[] bytes = bytes(1, 2, 3, 4);
Files.write(path("/test"), bytes);
try (FileChannel channel = FileChannel.open(path("/test"), READ, TRUNCATE_EXISTING)) {
// TRUNCATE_EXISTING ignored when opening for read
byte[] readBytes = new byte[4];
channel.read(ByteBuffer.wrap(readBytes));
assertThat(Bytes.asList(readBytes)).isEqualTo(Bytes.asList(bytes));
}
}
use of java.nio.channels.FileChannel in project jimfs by google.
the class JimfsFileChannelTest method testAsynchronousClose.
@Test
public void testAsynchronousClose() 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();
CountDownLatch latch = new CountDownLatch(BLOCKING_OP_COUNT);
List<Future<?>> futures = queueAllBlockingOperations(channel, executor, latch);
// wait for all the threads to have started running
latch.await();
// then ensure time for operations to start blocking
Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
// close channel on this thread
channel.close();
// AsynchronousCloseException
for (Future<?> future : futures) {
try {
future.get();
fail();
} catch (ExecutionException expected) {
assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
}
}
}
Aggregations