### Question:
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(); static void addSequenceToBuffer(int begin, int count, IntBuffer buf); static ByteBuffer clone(ByteBuffer buf); static ByteBuffer newByteBuffer(int capacity); static ByteBuffer newByteBuffer(int capacity, boolean direct); static FloatBuffer newFloatBuffer(int capacity); static IntBuffer newIntBuffer(int capacity); static ByteBuffer newRepeatingByteBuffer(int numberOfRepeats, ByteBuffer sequence); static IntBuffer newSequentialIntBuffer(int begin, int count); static byte[] toByteArray(ByteBuffer data); static IntBuffer toIntBuffer(int[] array); static IntBuffer toIntBuffer(TIntList list);  }### Answer:
@Test public void testCloneByteBuffer() { byte[] arr = new byte[20]; RANDOM.nextBytes(arr); ByteBuffer buf; ByteBuffer clone; buf = ByteBuffer.wrap(arr, 5, 10); clone = BufferUtilities.clone(buf); Assert.assertNotSame(arr, clone.array()); Assert.assertEquals(buf.position(), clone.position()); Assert.assertEquals(buf.limit(), clone.limit()); Assert.assertEquals(buf.capacity(), clone.capacity()); Assert.assertTrue(buf.isDirect() == clone.isDirect()); buf.position(0).limit(buf.capacity()); clone.position(0).limit(buf.capacity()); Assert.assertTrue(buf.equals(clone)); clone.put(5, (byte)(clone.get(5) ^ 1)); Assert.assertFalse(buf.equals(clone)); buf = buf.asReadOnlyBuffer(); clone = BufferUtilities.clone(buf); Assert.assertNotSame(arr, clone.array()); Assert.assertEquals(buf.position(), clone.position()); Assert.assertEquals(buf.limit(), clone.limit()); Assert.assertEquals(buf.capacity(), clone.capacity()); Assert.assertTrue(buf.isDirect() == clone.isDirect()); buf.position(0).limit(buf.capacity()); clone.position(0).limit(buf.capacity()); Assert.assertTrue(buf.equals(clone)); clone.put(5, (byte)(clone.get(5) ^ 1)); Assert.assertFalse(buf.equals(clone)); buf = ByteBuffer.allocateDirect(arr.length); buf.put(arr); clone = BufferUtilities.clone(buf); Assert.assertEquals(buf.position(), clone.position()); Assert.assertEquals(buf.limit(), clone.limit()); Assert.assertEquals(buf.capacity(), clone.capacity()); buf.position(0).limit(buf.capacity()); clone.position(0).limit(buf.capacity()); Assert.assertTrue(buf.equals(clone)); clone.put(5, (byte)(clone.get(5) ^ 1)); Assert.assertFalse(buf.equals(clone)); }