private final ByteBuffer copyToByteBuffer(Buffer buf) {
   if (buf instanceof ByteBuffer) {
     if (buf.position() == 0) {
       return (ByteBuffer) buf;
     }
     return Buffers.copyByteBuffer((ByteBuffer) buf);
   } else if (buf instanceof ShortBuffer) {
     return Buffers.copyShortBufferAsByteBuffer((ShortBuffer) buf);
   } else if (buf instanceof IntBuffer) {
     return Buffers.copyIntBufferAsByteBuffer((IntBuffer) buf);
   } else if (buf instanceof FloatBuffer) {
     return Buffers.copyFloatBufferAsByteBuffer((FloatBuffer) buf);
   } else {
     throw new IllegalArgumentException(
         "Unsupported buffer type (must be one of byte, short, int, or float)");
   }
 }