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