@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 testWritingSlicedByteBuffer() {
        byte[] expectedArray = new byte[]{2, 3, 0, 0};
        ByteBuffer sourceBuffer = ByteBuffer.wrap(new byte[]{0, 1, 2, 3});
        ByteBuffer resultBuffer = ByteBuffer.allocate(4);

        // Move position forward to ensure slice is not whole buffer
        sourceBuffer.position(2);
        ByteBuffer slicedBuffer = sourceBuffer.slice();

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

        writable.writeByteBuffer(slicedBuffer);

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