/** * Parses a byte array into a space separated hex string where each byte is represented in the * format {@code 0x%02x}. * * @param bytes the byte array to be transformed * @return the string representation of the byte array */ public static String byteArrayToHexString(byte[] bytes) { return byteArrayToHexString(bytes, "0x", " "); }
/** * Tests the {@link FormatUtils#byteArrayToHexString(byte[])} method. */ @Test public void byteArrayToHexString() { Assert.assertEquals("", FormatUtils.byteArrayToHexString(new byte[0])); Assert.assertEquals("0x01", FormatUtils.byteArrayToHexString(new byte[]{1})); Assert.assertEquals("0x01 0xac", FormatUtils.byteArrayToHexString(new byte[]{1, (byte) 0xac})); Assert.assertEquals("01ac", FormatUtils.byteArrayToHexString(new byte[] {1, (byte) 0xac}, "", "")); }
/** * Parses a byte array into a space separated hex string where each byte is represented in the * format {@code 0x%02x}. * * @param bytes the byte array to be transformed * @return the string representation of the byte array */ public static String byteArrayToHexString(byte[] bytes) { return byteArrayToHexString(bytes, "0x", " "); }
private static void show(KeyValuePair pair, String scope) { String key = FormatUtils.byteArrayToHexString(BufferUtils.newByteArrayFromByteBuffer( pair.getKey())); String value = FormatUtils.byteArrayToHexString(BufferUtils.newByteArrayFromByteBuffer( pair.getValue())); switch (scope) { case "key": System.out.println(key); break; case "value": System.out.println(value); break; case "all": System.out.printf("%s %s%n", key, value); break; default: throw new RuntimeException( String.format("Unknown scope: %s, should be one of key/value/all", scope)); } }
private boolean areTheSameStores(KeyValueStoreReader reader1, KeyValueStoreReader reader2) throws Exception { // The two stores are the same if the number of key-value pairs are the same, and the stores // have the same value for all the keys in store1. int size1 = reader1.size(); int size2 = reader2.size(); if (size1 != size2) { LOG.error("store {} has {} pairs while another has {} pairs", mStoreUri1, size1, size2); return false; } KeyValueIterator iterator = reader1.iterator(); while (iterator.hasNext()) { KeyValuePair pair1 = iterator.next(); byte[] key1 = BufferUtils.newByteArrayFromByteBuffer(pair1.getKey()); byte[] value1 = BufferUtils.newByteArrayFromByteBuffer(pair1.getValue()); byte[] value2 = reader2.get(key1); if (value2 == null) { LOG.error("store {} has value for key {} while another has no value for the same key", mStoreUri1, FormatUtils.byteArrayToHexString(key1)); return false; } if (!ByteBuffer.wrap(value1).equals(ByteBuffer.wrap(value2))) { LOG.error("value for key {} are different in the two stores", FormatUtils.byteArrayToHexString(key1)); return false; } } return true; }