@Override
    public void truncate(final Long length) {
        this.length = length;
        if(temporary.exists()) {
            try {
                final RandomAccessFile file = random();
                if(length < file.length()) {
                    // Truncate current
                    file.setLength(length);
                }
            }
            catch(IOException e) {
                log.warn(String.format("Failure truncating file %s to %d", temporary, length));
            }
        }
    }
@Test
    public void testTruncate() throws Exception {
        final FileBuffer buffer = new FileBuffer();
        assertEquals(0L, buffer.length(), 0L);
        final byte[] chunk = RandomUtils.nextBytes(100);
        buffer.write(chunk, 0L);
        assertEquals(100L, buffer.length(), 0L);
        buffer.truncate(1L);
        assertEquals(1L, buffer.length(), 0L);
        {
            final byte[] read = new byte[1];
            assertEquals(1, buffer.read(read, 0L));
            assertEquals(chunk[0], read[0]);
        }
        {
            final byte[] read = new byte[2];
            assertEquals(1, buffer.read(read, 0L));
            assertEquals(chunk[0], read[0]);
        }
        assertEquals(1L, buffer.length(), 0L);
        buffer.truncate(102L);
        assertEquals(102L, buffer.length(), 0L);
        {
            final byte[] read = new byte[2];
            assertEquals(2, buffer.read(read, 0L));
            assertEquals(chunk[0], read[0]);
            assertEquals(0, read[1]);
        }
        buffer.write(chunk, 0L);
        {
            final byte[] read = new byte[100];
            assertEquals(100, buffer.read(read, 0L));
            assertArrayEquals(chunk, read);
        }
        {
            final byte[] read = new byte[2];
            assertEquals(2, buffer.read(read, 100L));
            assertEquals(0, read[0]);
            assertEquals(0, read[1]);
        }
        {
            final byte[] read = new byte[3];
            assertEquals(3, buffer.read(read, 99L));
            assertEquals(chunk[99], read[0]);
            assertEquals(0, read[1]);
            assertEquals(0, read[2]);
        }
        assertEquals(IOUtils.EOF, buffer.read(new byte[1], 102L));
    }