class BufferUtilities {

    public static ByteBuffer clone(ByteBuffer buf)
    {
        final ByteBuffer result = buf.isDirect() ? ByteBuffer.allocateDirect(buf.capacity()) : ByteBuffer.allocate(buf.capacity());
        final ByteBuffer dup = buf.duplicate();
        dup.rewind().limit(dup.capacity());
        result.put(dup);
        result.position(buf.position());
        result.limit(buf.limit());
        result.order(buf.order());
        return result;
    }

    private  BufferUtilities();

}