SizeControllableFile extends AbstractControllableFile { @Override public long size() { return fileSize.getSize(() -> super.size()); } SizeControllableFile(File file, FileSize fileSize); @Override long size();  }
@Test public void testSize() throws IOException { sizeControllableFile = new SizeControllableFile(file, new FileSize() { @Override public long getSize(LongSupplier realSizeProvider) { return realSizeProvider.getAsLong() - 100; } }); long totalLen = 0; for (int i = 0; i < testCount; i++) { int dataLen = randomInt(1, 1024); sizeControllableFile.getFileChannel().write(ByteBuffer.wrap(randomString(dataLen).getBytes())); totalLen += dataLen; Assert.assertEquals(totalLen - 100, sizeControllableFile.size()); } }
@Test public void testLazyOpen() throws IOException { sizeControllableFile = new SizeControllableFile(file, new FileSize() { @Override public long getSize(LongSupplier realSizeProvider) { return realSizeProvider.getAsLong(); } }) { @Override protected void doOpen() throws IOException { openCount.incrementAndGet(); super.doOpen(); } }; Assert.assertEquals(0, openCount.get()); for (int i = 0; i < testCount; i++) { sizeControllableFile.getFileChannel(); sizeControllableFile.size(); Assert.assertEquals(1, openCount.get()); } sizeControllableFile.close(); for (int i = 0; i < testCount; i++) { sizeControllableFile.getFileChannel(); sizeControllableFile.size(); Assert.assertEquals(2, openCount.get()); } }