Repository: apache/dubbo
File: dubbo-remoting/dubbo-remoting-netty4/src/test/java/org/apache/dubbo/remoting/transport/netty4/NettyBackedChannelBufferTest.java (Java)

Code before:
        assertEquals(2, buffer.readableBytes());

        byte[] actual = new byte[2];
        buffer.getBytes(0, actual);
        assertEquals(1, actual[0]);
        assertEquals(2, actual[1]);
    }

    @Test
    void testBufferTransfer_directToDirect() {
        ByteBuf srcDirect = Unpooled.directBuffer(4);
        ByteBuf dstDirect = Unpooled.directBuffer(4);

        ChannelBuffer source = new NettyBackedChannelBuffer(srcDirect);
        ChannelBuffer target = new NettyBackedChannelBuffer(dstDirect);

        byte[] data = {10, 20, 30, 40};
        source.writeBytes(data);

        target.setBytes(0, source, 0, 4);

        byte[] actual = new byte[4];
        target.getBytes(0, actual);

        assertArrayEquals(data, actual);
        assertEquals(0, target.readerIndex(), "setBytes should not move readerIndex");
    }

    @Test
    void testReadBytes_directBuffer() {
        byte[] data = {1, 2, 3, 4};
        buffer.writeBytes(data);

        byte[] actual = new byte[4];
        buffer.readBytes(actual);

        assertArrayEquals(data, actual);
        assertEquals(4, buffer.readerIndex());
    }

    @Test
    void testGetBytes_directBuffer_shouldNotMoveIndex() {
        buffer.writeBytes(new byte[] {5, 6, 7, 8});

        int before = buffer.readerIndex();

        byte[] actual = new byte[2];
        buffer.getBytes(1, actual);

        assertEquals(before, buffer.readerIndex());
        assertArrayEquals(new byte[] {6, 7}, actual);

Review comment (suggestion):
`testBufferTransfer_directToDirect` allocates two direct `ByteBuf`s via `Unpooled.directBuffer(4)` but never releases them, which leaks off-heap memory (Netty direct buffers are reference-counted and must be released). Even though each buffer is small, it's better to call `release()` on both `srcDirect` and `dstDirect` (e.g., in a `try/finally`) to avoid leaks if this test is run repeatedly or adapted for larger buffers.
```suggestion
        try {
            ChannelBuffer source = new NettyBackedChannelBuffer(srcDirect);
            ChannelBuffer target = new NettyBackedChannelBuffer(dstDirect);

            byte[] data = {10, 20, 30, 40};
            source.writeBytes(data);

            target.setBytes(0, source, 0, 4);

            byte[] actual = new byte[4];
            target.getBytes(0, actual);

            assertArrayEquals(data, actual);
            assertEquals(0, target.readerIndex(), "setBytes should not move readerIndex");
        } finally {
            srcDirect.release();
            dstDirect.release();
        }
```

Code after:
        assertEquals(2, buffer.readableBytes());

        byte[] actual = new byte[2];
        buffer.getBytes(0, actual);
        assertEquals(1, actual[0]);
        assertEquals(2, actual[1]);
    }

    @Test
    void testBufferTransfer_directToDirect() {
        ByteBuf srcDirect = Unpooled.directBuffer(4);
        ByteBuf dstDirect = Unpooled.directBuffer(4);

        try {
            ChannelBuffer source = new NettyBackedChannelBuffer(srcDirect);
            ChannelBuffer target = new NettyBackedChannelBuffer(dstDirect);

            byte[] data = {10, 20, 30, 40};
            source.writeBytes(data);

            target.setBytes(0, source, 0, 4);

            byte[] actual = new byte[4];
            target.getBytes(0, actual);

            assertArrayEquals(data, actual);
            assertEquals(0, target.readerIndex(), "setBytes should not move readerIndex");
        } finally {
            srcDirect.release();
            dstDirect.release();
        }
    }

    @Test
    void testReadBytes_directBuffer() {
        byte[] data = {1, 2, 3, 4};
        buffer.writeBytes(data);

        byte[] actual = new byte[4];
        buffer.readBytes(actual);

        assertArrayEquals(data, actual);
        assertEquals(4, buffer.readerIndex());
    }

    @Test
    void testGetBytes_directBuffer_shouldNotMoveIndex() {
        buffer.writeBytes(new byte[] {5, 6, 7, 8});

        int before = buffer.readerIndex();