HBASE-12415 Add add(byte[][] arrays) to Bytes (Jean-Marc Spaggiari)

This commit is contained in:
Andrew Purtell 2015-05-01 18:12:36 -07:00
parent a6027aedb3
commit 1c4b47f856
2 changed files with 31 additions and 0 deletions

View File

@ -1806,6 +1806,24 @@ public class Bytes implements Comparable<Bytes> {
return result; return result;
} }
/**
* @param arrays all the arrays to concatenate together.
* @return New array made from the concatenation of the given arrays.
*/
public static byte [] add(final byte [][] arrays) {
int length = 0;
for (int i = 0; i < arrays.length; i++) {
length += arrays[i].length;
}
byte [] result = new byte[length];
int index = 0;
for (int i = 0; i < arrays.length; i++) {
System.arraycopy(arrays[i], 0, result, index, arrays[i].length);
index += arrays[i].length;
}
return result;
}
/** /**
* @param a array * @param a array
* @param length amount of bytes to grab * @param length amount of bytes to grab

View File

@ -49,6 +49,19 @@ public class TestBytes extends TestCase {
assertNotNull(ee); assertNotNull(ee);
} }
public void testAdd () throws Exception {
byte[] a = {0,0,0,0,0,0,0,0,0,0};
byte[] b = {1,1,1,1,1,1,1,1,1,1,1};
byte[] c = {2,2,2,2,2,2,2,2,2,2,2,2};
byte[] d = {3,3,3,3,3,3,3,3,3,3,3,3,3};
byte[] result1 = Bytes.add (a, b, c);
byte[] result2 = Bytes.add (new byte[][] {a, b, c});
assertEquals(0, Bytes.compareTo(result1, result2));
byte[] result4 = Bytes.add (result1, d);
byte[] result5 = Bytes.add (new byte[][] {result1, d});
assertEquals(0, Bytes.compareTo(result1, result2));
}
public void testSplit() throws Exception { public void testSplit() throws Exception {
byte [] lowest = Bytes.toBytes("AAA"); byte [] lowest = Bytes.toBytes("AAA");
byte [] middle = Bytes.toBytes("CCC"); byte [] middle = Bytes.toBytes("CCC");