@Nullable
    static Buffer sliceNextBuffer(ByteBuffer memory) {
        final int remaining = memory.remaining();

        // we only check the correct case where data is exhausted
        // all other cases can only occur if our write logic is wrong and will already throw
        // buffer underflow exceptions which will cause the read to fail.
        if (remaining == 0) {
            return null;
        }

        final BufferHeader header = parseBufferHeader(memory);

        memory.limit(memory.position() + header.getLength());
        ByteBuffer buf = memory.slice();
        memory.position(memory.limit());
        memory.limit(memory.capacity());

        MemorySegment memorySegment = MemorySegmentFactory.wrapOffHeapMemory(buf);
        return new NetworkBuffer(
                memorySegment,
                FreeingBufferRecycler.INSTANCE,
                header.getDataType(),
                header.isCompressed(),
                header.getLength());
    }
@Test
    void readFromEmptyByteBuffer() {
        final ByteBuffer memory = ByteBuffer.allocateDirect(100);
        memory.position(memory.limit());

        final Buffer result = BufferReaderWriterUtil.sliceNextBuffer(memory);

        assertThat(result).isNull();
    }