class RewindableReadableByteChannel implements ReadableByteChannel {

    private final ReadableByteChannel readableByteChannel;
    private final ByteBuffer buffer;
    // If 'true', there are more bytes read from |readableByteChannel| than the allocated buffer size.
    // The rewind is not possible in that case.
    private boolean passedRewindPoint;
    private int nextBufferWritePosition;
    private int nextBufferReadPosition;

    public RewindableReadableByteChannel(ReadableByteChannel readableByteChannel, int bufferCapacity) {
        this.buffer = ByteBuffer.allocate(bufferCapacity);
        this.readableByteChannel = readableByteChannel;
    }

    /**
     * @see ReadableByteChannel#read(ByteBuffer)
     */
    public int read(ByteBuffer dst) throws IOException {<FILL_FUNCTION_BODY>}

    public void rewind() {
        if (passedRewindPoint) {
            throw new IllegalStateException("Passed the rewind point. Increase the buffer capacity.");
        }
        nextBufferReadPosition = 0;
    }

    /**
     * @see ReadableByteChannel#isOpen()
     */
    public boolean isOpen() {
        return readableByteChannel.isOpen();
    }

    /**
     * @see ReadableByteChannel#close()
     */
    public void close() throws IOException {
        readableByteChannel.close();
    }
}

        int initialDstPosition = dst.position();
        // Read data from |readableByteChannel| into |buffer|.
        ((Buffer)buffer).limit(buffer.capacity());
        ((Buffer)buffer).position(nextBufferWritePosition);
        if (buffer.capacity() > 0) {
            readableByteChannel.read(buffer);
            nextBufferWritePosition = buffer.position();
        }

        // Read data from |buffer| into |dst|.
        ((Buffer)buffer).position(nextBufferReadPosition);
        ((Buffer)buffer).limit(nextBufferWritePosition);
        if (buffer.remaining() > dst.remaining()) {
            ((Buffer)buffer).limit(buffer.position() + dst.remaining());
        }
        dst.put(buffer);
        nextBufferReadPosition = buffer.position();

        // If |dst| still has capacity then read data from |readableByteChannel|.
        int bytesRead = readableByteChannel.read(dst);
        if (bytesRead > 0) {
            // We passed the buffering capacity. It will not be possible to rewind
            // |readableByteChannel| anymore.
            passedRewindPoint = true;
        } else if ((bytesRead == -1) && (dst.position() - initialDstPosition == 0)) {
            return -1;
        }
        return dst.position() - initialDstPosition;
    
<no_super_class>