/** * @param arrays * @return * @see #pack(java.util.List) */ public static byte[] pack(byte[]... arrays) { return pack(Arrays.asList(arrays)); }
public static byte[] packComparable(List<byte[]> arrays) { return packComparable( arrays, DEFAULT_DELIMITER, DEFAULT_TERMINAL_DELIMITER ); }
/** * reads from a DataInput a List<byte[]> * * @param in * @param delimiter * @param terminalDelimiter * @return * @throws IOException * @see #packComparable(java.util.List, byte, byte) for format */ public static List<byte[]> readByteArrayList( DataInput in, byte delimiter, byte terminalDelimiter ) throws IOException { // 256 magic number--just guessing it won't be bigger. If it is, // ArrayList will resize List<Byte> byteList = new ArrayList<Byte>(MAGIC_INITIAL_BYTE_ARRAY_SIZE); byte b; while ((b = in.readByte()) != terminalDelimiter) { byteList.add(b); } byteList.add(terminalDelimiter); return unpackComparable( byteListToArray(byteList), delimiter, terminalDelimiter ); }
@Test(groups = "fast") public void testConversions() throws Exception { byte[] packed = PackedByteArray.pack( original[0], original[1], original[2], original[3], original[4], original[5], original[6] ); byte[][] unpacked = PackedByteArray.unpack(packed); byte[] comparablePacked = PackedByteArray.packComparable(unpacked); List<byte[]> bytesList = PackedByteArray.unpackComparable(comparablePacked); for (int i = 0; i < unpacked.length; i++) { Assert.assertEquals(bytesList.get(i), unpacked[i]); } } }
@Test(groups = "fast") public void testSanity() throws Exception { byte[] packed = PackedByteArray.pack( original[0], original[1], original[2], original[3], original[4], original[5], original[6] ); byte[][] unpacked = PackedByteArray.unpack(packed); for (int i = 0; i < original.length; i++) { byte[] element = PackedByteArray.getElement(packed, i); Assert.assertTrue(Arrays.equals(element, original[i])); Assert.assertTrue(Arrays.equals(original[i], unpacked[i])); } }
public static List<byte[]> unpackComparable(byte[] packedArray) { return unpackComparable(packedArray, (byte)1, (byte)0); }
/** * reads from a DataInput a byte[] till a delimiter is encountered. * Return the bytes (not including the delimiter) * * @param in * @param terminalDelimiter * @return * @throws IOException */ public static byte[] readByteArray( DataInput in, byte terminalDelimiter ) throws IOException { // 256 magic number--just guessing it won't be bigger. If it is, // ArrayList will resize List<Byte> byteList = new ArrayList<Byte>(MAGIC_INITIAL_BYTE_ARRAY_SIZE); byte b; while ((b = in.readByte()) != terminalDelimiter) { byteList.add(b); } return byteListToArray(byteList); }
while (i < numItems) { int j = 2 + (i * 4); int len = byteToInt(packedArray, j);
@Test(groups = "fast") public void testConversions() throws Exception { byte[] packed = PackedByteArray.pack( original[0], original[1], original[2], original[3], original[4], original[5], original[6] ); byte[][] unpacked = PackedByteArray.unpack(packed); byte[] comparablePacked = PackedByteArray.packComparable(unpacked); List<byte[]> bytesList = PackedByteArray.unpackComparable(comparablePacked); for (int i = 0; i < unpacked.length; i++) { Assert.assertEquals(bytesList.get(i), unpacked[i]); } } }
@Test(groups = "fast") public void testSanity() throws Exception { byte[] packed = PackedByteArray.pack( original[0], original[1], original[2], original[3], original[4], original[5], original[6] ); byte[][] unpacked = PackedByteArray.unpack(packed); for (int i = 0; i < original.length; i++) { byte[] element = PackedByteArray.getElement(packed, i); Assert.assertTrue(Arrays.equals(element, original[i])); Assert.assertTrue(Arrays.equals(original[i], unpacked[i])); } }
public static List<byte[]> unpackComparable( byte[] packedArray, byte delimiter, byte terminalDelimiter ) { List<byte[]> results = new ArrayList<byte[]>(); List<Byte> currentToken = new ArrayList<Byte>(MAGIC_INITIAL_BYTE_ARRAY_SIZE); // very magic for (int i = 0; i < packedArray.length; i++) { if (packedArray[i] == terminalDelimiter) { results.add(byteListToArray(currentToken)); // end of entire byte array break; } else if (packedArray[i] == delimiter) { // end of an element, store and move to next results.add(byteListToArray(currentToken)); currentToken = new ArrayList<Byte>(MAGIC_INITIAL_BYTE_ARRAY_SIZE); } else { // put byte into current array currentToken.add(packedArray[i]); } } return results; }
public static byte[] packComparable(byte[]... arrays) { return packComparable(Arrays.asList(arrays)); }
@Test(groups = "fast") public void testCompare1() throws Exception { // {2, 3, 3} byte[] bytes1 = PackedByteArray.packComparable( original[0] ); // {2, 3, 2} byte[] bytes2 = PackedByteArray.packComparable( original[1] ); // => -1 Assert.assertEquals(Lists.compareArrays(bytes1, bytes2), -1); }
@Test(groups = "fast") public void testCompare2() throws Exception { // { {0, 0, 1} } byte[] bytes1 = PackedByteArray.packComparable( original[1] ); // {{0, 1}, {0} } byte[] bytes2 = PackedByteArray.packComparable( original[5], original[6] ); // => 1 Assert.assertEquals(Lists.compareArrays(bytes1, bytes2), 1); }
@Test(groups = "fast") public void testCompare1() throws Exception { // {2, 3, 3} byte[] bytes1 = PackedByteArray.packComparable( original[0] ); // {2, 3, 2} byte[] bytes2 = PackedByteArray.packComparable( original[1] ); // => -1 Assert.assertEquals(Lists.compareArrays(bytes1, bytes2), -1); }
@Test(groups = "fast") public void testCompare2() throws Exception { // { {0, 0, 1} } byte[] bytes1 = PackedByteArray.packComparable( original[1] ); // {{0, 1}, {0} } byte[] bytes2 = PackedByteArray.packComparable( original[5], original[6] ); // => 1 Assert.assertEquals(Lists.compareArrays(bytes1, bytes2), 1); }