Search in sources :

Example 41 with FileChannel

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());
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) File(java.io.File)

Example 42 with FileChannel

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();
}
Also used : Path(java.nio.file.Path) NonReadableChannelException(java.nio.channels.NonReadableChannelException) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) NonWritableChannelException(java.nio.channels.NonWritableChannelException) Test(org.junit.Test)

Example 43 with FileChannel

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());
    }
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 44 with FileChannel

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));
    }
}
Also used : AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 45 with FileChannel

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);
        }
    }
}
Also used : AsynchronousCloseException(java.nio.channels.AsynchronousCloseException) FileChannel(java.nio.channels.FileChannel) ExecutorService(java.util.concurrent.ExecutorService) SettableFuture(com.google.common.util.concurrent.SettableFuture) Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch) 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