HBASE-6352 Add copy method in Bytes

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1381670 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2012-09-06 16:37:07 +00:00
parent 24131743a8
commit f89a1b58ed
2 changed files with 20 additions and 1 deletions

View File

@ -1655,5 +1655,17 @@ public class Bytes {
return toString(b, 0, n);
}
/**
* Copy the byte array given in parameter and return an instance
* of a new byte array with the same length and the same content.
* @param bytes the byte array to duplicate
* @return a copy of the given byte array
*/
public static byte [] copy(byte [] bytes) {
if (bytes == null) return null;
byte [] result = new byte[bytes.length];
System.arraycopy(bytes, 0, result, 0, bytes.length);
return result;
}
}

View File

@ -283,5 +283,12 @@ public class TestBytes extends TestCase {
assertEquals("World", Bytes.readStringFixedSize(dis, 18));
assertEquals("", Bytes.readStringFixedSize(dis, 9));
}
public void testCopy() throws Exception {
byte [] bytes = Bytes.toBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
byte [] copy = Bytes.copy(bytes);
assertFalse(bytes == copy);
assertTrue(Bytes.equals(bytes, copy));
}
}