public static ByteBuffer cloneByteBuffer(ByteBuffer buf) {
    ByteBuffer ret = ByteBuffer.allocate(buf.limit() - buf.position());
    if (buf.hasArray()) {
      ret.put(buf.array(), buf.position(), buf.limit() - buf.position());
    } else {
      // direct buffer
      ret.put(buf);
    }
    ret.flip();
    return ret;
  }
@Test
  public void cloneByteBuffer() {
    final int bufferSize = 10;
    ByteBuffer buf = ByteBuffer.allocate(bufferSize);
    for (byte i = 0; i < bufferSize; i++) {
      buf.put(i);
    }
    ByteBuffer bufClone = BufferUtils.cloneByteBuffer(buf);
    assertEquals(buf, bufClone);
  }