diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java index 0a30809b601..80bfe5f558c 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/Bytes.java @@ -1806,6 +1806,24 @@ public class Bytes implements Comparable { 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 length amount of bytes to grab diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java index eb5e4537838..aa13223cf0c 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestBytes.java @@ -49,6 +49,19 @@ public class TestBytes extends TestCase { 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 { byte [] lowest = Bytes.toBytes("AAA"); byte [] middle = Bytes.toBytes("CCC");