HBASE-7137 Improve Bytes to accept byte buffers which don't allow us to directly access their backing arrays (Hiroshi)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1409148 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Zhihong Yu 2012-11-14 10:58:04 +00:00
parent 98bdd5df13
commit 32f23b3d0e
2 changed files with 98 additions and 18 deletions

View File

@ -242,14 +242,24 @@ public class Bytes {
} }
/** /**
* Returns a new byte array, copied from the passed ByteBuffer. * Returns a new byte array, copied from the given {@code buf},
* @param bb A ByteBuffer * from the index 0 (inclusive) to the limit (exclusive),
* regardless of the current position.
* The position and the other index parameters are not changed.
*
* @param buf a byte buffer
* @return the byte array * @return the byte array
* @see #getBytes(ByteBuffer)
*/ */
public static byte[] toBytes(ByteBuffer bb) { public static byte[] toBytes(ByteBuffer buf) {
int length = bb.limit(); ByteBuffer dup = buf.duplicate();
byte [] result = new byte[length]; dup.position(0);
System.arraycopy(bb.array(), bb.arrayOffset(), result, 0, length); return readBytes(dup);
}
private static byte[] readBytes(ByteBuffer buf) {
byte [] result = new byte[buf.remaining()];
buf.get(result);
return result; return result;
} }
@ -309,17 +319,24 @@ public class Bytes {
} }
/** /**
* Converts the given byte buffer, from its array offset to its limit, to * Converts the given byte buffer to a printable representation,
* a string. The position and the mark are ignored. * from the index 0 (inclusive) to the limit (exclusive),
* regardless of the current position.
* The position and the other index parameters are not changed.
* *
* @param buf a byte buffer * @param buf a byte buffer
* @return a string representation of the buffer's binary contents * @return a string representation of the buffer's binary contents
* @see #toBytes(ByteBuffer)
* @see #getBytes(ByteBuffer)
*/ */
public static String toStringBinary(ByteBuffer buf) { public static String toStringBinary(ByteBuffer buf) {
if (buf == null) if (buf == null)
return "null"; return "null";
if (buf.hasArray()) {
return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit()); return toStringBinary(buf.array(), buf.arrayOffset(), buf.limit());
} }
return toStringBinary(toBytes(buf));
}
/** /**
* Write a printable representation of a byte array. Non-printable * Write a printable representation of a byte array. Non-printable
@ -737,17 +754,16 @@ public class Bytes {
} }
/** /**
* This method will get a sequence of bytes from pos -> limit, * Returns a new byte array, copied from the given {@code buf},
* but will restore pos after. * from the position (inclusive) to the limit (exclusive).
* @param buf * The position and the other index parameters are not changed.
* @return byte array *
* @param buf a byte buffer
* @return the byte array
* @see #toBytes(ByteBuffer)
*/ */
public static byte[] getBytes(ByteBuffer buf) { public static byte[] getBytes(ByteBuffer buf) {
int savedPos = buf.position(); return readBytes(buf.duplicate());
byte [] newBytes = new byte[buf.remaining()];
buf.get(newBytes);
buf.position(savedPos);
return newBytes;
} }
/** /**

View File

@ -23,6 +23,7 @@ import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.util.Random;
@ -172,6 +173,69 @@ public class TestBytes extends TestCase {
return result; return result;
} }
public void testToBytesForByteBuffer() {
byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ByteBuffer target = ByteBuffer.wrap(array);
target.position(2);
target.limit(7);
byte[] actual = Bytes.toBytes(target);
byte[] expected = { 0, 1, 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected, actual));
assertEquals(2, target.position());
assertEquals(7, target.limit());
ByteBuffer target2 = target.slice();
assertEquals(0, target2.position());
assertEquals(5, target2.limit());
byte[] actual2 = Bytes.toBytes(target2);
byte[] expected2 = { 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected2, actual2));
assertEquals(0, target2.position());
assertEquals(5, target2.limit());
}
public void testGetBytesForByteBuffer() {
byte[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
ByteBuffer target = ByteBuffer.wrap(array);
target.position(2);
target.limit(7);
byte[] actual = Bytes.getBytes(target);
byte[] expected = { 2, 3, 4, 5, 6 };
assertTrue(Arrays.equals(expected, actual));
assertEquals(2, target.position());
assertEquals(7, target.limit());
}
public void testToStringBinaryForBytes() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
String actual = Bytes.toStringBinary(array);
String expected = "09azAZ@\\x01";
assertEquals(expected, actual);
String actual2 = Bytes.toStringBinary(array, 2, 3);
String expected2 = "azA";
assertEquals(expected2, actual2);
}
public void testToStringBinaryForArrayBasedByteBuffer() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
ByteBuffer target = ByteBuffer.wrap(array);
String actual = Bytes.toStringBinary(target);
String expected = "09azAZ@\\x01";
assertEquals(expected, actual);
}
public void testToStringBinaryForReadOnlyByteBuffer() {
byte[] array = { '0', '9', 'a', 'z', 'A', 'Z', '@', 1 };
ByteBuffer target = ByteBuffer.wrap(array).asReadOnlyBuffer();
String actual = Bytes.toStringBinary(target);
String expected = "09azAZ@\\x01";
assertEquals(expected, actual);
}
public void testBinarySearch() throws Exception { public void testBinarySearch() throws Exception {
byte [][] arr = { byte [][] arr = {
{1}, {1},