Exemplo n.º 1
0
 private static FileChannel channel(RegularFile file, OpenOption... options) throws IOException {
   return new JimfsFileChannel(
       file,
       Options.getOptionsForChannel(ImmutableSet.copyOf(options)),
       new FileSystemState(Runnables.doNothing()));
 }
Exemplo n.º 2
0
  @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());
  }