dev@grizzly.java.net

Critical bug or confusing behaviour of HeapBuffer.toByteBuffer()

From: Tigran Mkrtchyan <tigran.mkrtchyan_at_desy.de>
Date: Fri, 24 Feb 2012 00:10:01 +0100

Hi.

Tracing it costed me a week of debugging:

    @Test
    public void testBufferSlice() {
        Buffer b = MemoryManager.
                    DEFAULT_MEMORY_MANAGER.allocate(10);
        b.putInt(1);
        ByteBuffer bb = b.slice().toByteBuffer();
        bb.rewind();
        bb.putInt(2);

        b.rewind();
        assertEquals(b.getInt(), 1);
    }


here is the code:

    @Override
    public ByteBuffer toByteBuffer(final int position, final int limit) {
        if (byteBuffer == null) {
            byteBuffer = ByteBuffer.wrap(heap);
        }

        Buffers.setPositionLimit(byteBuffer, offset + position, offset + limit);
        return byteBuffer;
    }


I think it's a bug and have to be:
byteBuffer = ByteBuffer.wrap(heap, offset, cap);

or java doc have to be updated ( and created in the first place) to
describe that toByteBuffer exposes underlying heap.

Tigran.