@Test
public void testDecodingFileWithOffsetAndBufferedSessionData() throws Exception {

    final ReadableByteChannel channel = new ReadableByteChannelMock(
            new String[] {"stuff; ", "more stuff; ", "a lot more stuff!"}, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final IdentityDecoder decoder = new IdentityDecoder(
            channel, inbuf, metrics);
    final int i = inbuf.fill(channel);
    Assert.assertEquals(7, i);
    final byte[] beginning = "beginning; ".getBytes(StandardCharsets.US_ASCII);
    createTempFile();
    RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        testfile.write(beginning);
    } finally {
        testfile.close();
    }
    testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        final FileChannel fchannel = testfile.getChannel();
        long pos = beginning.length;
        while (!decoder.isCompleted()) {
            if(testfile.length() < pos) {
                testfile.setLength(pos);
            }
            final long bytesRead = decoder.transfer(fchannel, pos, 10);
            if (bytesRead > 0) {
                pos += bytesRead;
            }
        }
        // count everything except the initial 7 bytes that went to the session buffer
        Assert.assertEquals(testfile.length() - 7 - beginning.length, metrics.getBytesTransferred());
    } finally {
        testfile.close();
    }
    Assert.assertEquals("beginning; stuff; more stuff; a lot more stuff!",
        CodecTestUtils.readFromFile(this.tmpfile));
}