Search in sources :

Example 56 with FileChannel

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

the class JimfsFileChannelTest method testTruncate.

@Test
public void testTruncate() throws IOException {
    RegularFile file = regularFile(10);
    FileChannel channel = channel(file, WRITE);
    // no resize, >= size
    channel.truncate(10);
    assertEquals(10, file.size());
    // no resize, > size
    channel.truncate(11);
    assertEquals(10, file.size());
    // resize down to 5
    channel.truncate(5);
    assertEquals(5, file.size());
    channel.position(20);
    channel.truncate(10);
    assertEquals(10, channel.position());
    channel.truncate(2);
    assertEquals(2, channel.position());
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 57 with FileChannel

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

the class JimfsFileChannelTest method testLockNegative.

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

Example 58 with FileChannel

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

the class JimfsFileChannelTest method testLock.

@Test
public void testLock() throws IOException {
    FileChannel channel = channel(regularFile(10), READ, WRITE);
    assertNotNull(channel.lock());
    assertNotNull(channel.lock(0, 10, false));
    assertNotNull(channel.lock(0, 10, true));
    assertNotNull(channel.tryLock());
    assertNotNull(channel.tryLock(0, 10, false));
    assertNotNull(channel.tryLock(0, 10, true));
    FileLock lock = channel.lock();
    assertTrue(lock.isValid());
    lock.release();
    assertFalse(lock.isValid());
}
Also used : FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) Test(org.junit.Test)

Example 59 with FileChannel

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

the class JimfsFileChannelTest method testFileTimeUpdates.

@Test
public void testFileTimeUpdates() throws IOException {
    RegularFile file = regularFile(10);
    FileChannel channel = new JimfsFileChannel(file, ImmutableSet.<OpenOption>of(READ, WRITE), new FileSystemState(Runnables.doNothing()));
    // accessed
    long accessTime = file.getLastAccessTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.read(ByteBuffer.allocate(10));
    assertNotEquals(accessTime, file.getLastAccessTime());
    accessTime = file.getLastAccessTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.read(ByteBuffer.allocate(10), 0);
    assertNotEquals(accessTime, file.getLastAccessTime());
    accessTime = file.getLastAccessTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.read(new ByteBuffer[] { ByteBuffer.allocate(10) });
    assertNotEquals(accessTime, file.getLastAccessTime());
    accessTime = file.getLastAccessTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.read(new ByteBuffer[] { ByteBuffer.allocate(10) }, 0, 1);
    assertNotEquals(accessTime, file.getLastAccessTime());
    accessTime = file.getLastAccessTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.transferTo(0, 10, new ByteBufferChannel(10));
    assertNotEquals(accessTime, file.getLastAccessTime());
    // modified
    long modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.write(ByteBuffer.allocate(10));
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.write(ByteBuffer.allocate(10), 0);
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.write(new ByteBuffer[] { ByteBuffer.allocate(10) });
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.write(new ByteBuffer[] { ByteBuffer.allocate(10) }, 0, 1);
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.truncate(0);
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
    modifiedTime = file.getLastModifiedTime();
    Uninterruptibles.sleepUninterruptibly(2, MILLISECONDS);
    channel.transferFrom(new ByteBufferChannel(10), 0, 10);
    assertNotEquals(modifiedTime, file.getLastModifiedTime());
}
Also used : FileChannel(java.nio.channels.FileChannel) Test(org.junit.Test)

Example 60 with FileChannel

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

the class JimfsFileChannelTest method testTruncateNegative.

@Test
public void testTruncateNegative() throws IOException {
    FileChannel channel = channel(regularFile(0), READ, WRITE);
    try {
        channel.truncate(-1);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
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