@Override
    public void writeByteBuffer(ByteBuffer buf) {
        try {
            if (buf.hasArray()) {
                out.write(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
            } else {
                byte[] bytes = Utils.toArray(buf);
                out.write(bytes);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
@Test
    public void testWritingSlicedByteBufferWithNonZeroPosition() {
        byte[] expectedArray = new byte[]{3, 0, 0, 0};
        ByteBuffer originalBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
        ByteBuffer resultBuffer = ByteBuffer.allocate(4);

        // Move position forward to ensure slice is backed by heap buffer with non-zero offset
        originalBuffer.position(2);
        ByteBuffer slicedBuffer = originalBuffer.slice();
        // Move the slice's position forward to ensure the writer starts reading at that position
        slicedBuffer.position(1);

        Writable writable = new DataOutputStreamWritable(
                new DataOutputStream(new ByteBufferOutputStream(resultBuffer)));

        writable.writeByteBuffer(slicedBuffer);

        assertEquals(1, resultBuffer.position(), "Writing to the buffer moves the position forward");
        assertArrayEquals(expectedArray, resultBuffer.array(), "Result buffer should have expected elements");
    }