HBASE-16880 Correct the javadoc/behaviour of the APIs in ByteBufferUtils

(Ram)
This commit is contained in:
Ramkrishna 2016-10-25 10:52:57 +05:30
parent 4533bb63cf
commit 9875c699af
2 changed files with 24 additions and 3 deletions

View File

@ -365,16 +365,22 @@ public final class ByteBufferUtils {
/**
* Copy one buffer's whole data to another. Write starts at the current position of 'out' buffer.
* Note : This will advance the position marker of {@code out} but not change the position maker
* for {@code in}. The position and limit of the {@code in} buffer to be set properly by caller.
* Note : This will advance the position marker of {@code out} and also change the position maker
* for {@code in}.
* @param in source buffer
* @param out destination buffer
*/
public static void copyFromBufferToBuffer(ByteBuffer in, ByteBuffer out) {
if (UNSAFE_AVAIL) {
if (in.hasArray() && out.hasArray()) {
int length = in.remaining();
System.arraycopy(in.array(), in.arrayOffset(), out.array(), out.arrayOffset(), length);
out.position(out.position() + length);
in.position(in.limit());
} else if (UNSAFE_AVAIL) {
int length = in.remaining();
UnsafeAccess.copy(in, in.position(), out, out.position(), length);
out.position(out.position() + length);
in.position(in.limit());
} else {
out.put(in);
}

View File

@ -374,6 +374,21 @@ public class TestByteBufferUtils {
assertEquals(l, Bytes.toLong(b, 7));
}
@Test
public void testRelativeCopyFromBuffertoBuffer() {
ByteBuffer bb1 = ByteBuffer.allocate(135);
ByteBuffer bb2 = ByteBuffer.allocate(135);
fillBB(bb1, (byte) 5);
ByteBufferUtils.copyFromBufferToBuffer(bb1, bb2);
assertTrue(bb1.position() == bb2.position());
assertTrue(bb1.limit() == bb2.limit());
bb1 = ByteBuffer.allocateDirect(135);
bb2 = ByteBuffer.allocateDirect(135);
fillBB(bb1, (byte) 5);
ByteBufferUtils.copyFromBufferToBuffer(bb1, bb2);
assertTrue(bb1.position() == bb2.position());
assertTrue(bb1.limit() == bb2.limit());
}
@Test
public void testCompareTo() {
ByteBuffer bb1 = ByteBuffer.allocate(135);