Hex { public static String dump(byte[] arr, int offset, int length, int outOffset, int bpl, int addressLength) { int end = offset + length; if (((offset | length | end) < 0) || (end > arr.length)) { throw new IndexOutOfBoundsException("arr.length " + arr.length + "; " + offset + "..!" + end); } if (outOffset < 0) { throw new IllegalArgumentException("outOffset < 0"); } if (length == 0) { return ""; } StringBuffer sb = new StringBuffer(length * 4 + 6); boolean bol = true; int col = 0; while (length > 0) { if (col == 0) { String astr; switch (addressLength) { case 2: astr = Hex.u1(outOffset); break; case 4: astr = Hex.u2(outOffset); break; case 6: astr = Hex.u3(outOffset); break; default: astr = Hex.u4(outOffset); break; } sb.append(astr); sb.append(": "); } else if ((col & 1) == 0) { sb.append(' '); } sb.append(Hex.u1(arr[offset])); outOffset++; offset++; col++; if (col == bpl) { sb.append('\n'); col = 0; } length--; } if (col != 0) { sb.append('\n'); } return sb.toString(); } private  Hex(); static String u8(long v); static String u4(int v); static String u3(int v); static String u2(int v); static String u2or4(int v); static String u1(int v); static String uNibble(int v); static String s8(long v); static String s4(int v); static String s2(int v); static String s1(int v); static String dump(byte[] arr, int offset, int length,
                              int outOffset, int bpl, int addressLength);  }
@Test public void testDump() { byte[] bytes1 = new byte[]{17, 16, 17, 17, 17}; byte[] bytes2 = new byte[]{1, 1, 1, 1, 1, 1, 1, 0, 1, 1}; Assert.assertEquals("", Hex.dump(bytes1, 5, 0, 0, 1, 3)); Assert.assertEquals("ba: 0101 00\nbd: 0101\n", Hex.dump(bytes2, 5, 5, 186, 3, 2)); Assert.assertEquals("00: 10\n", Hex.dump(new byte[]{16}, 0, 1, 0, 1, 2)); Assert.assertEquals("00000000: 10\n", Hex.dump(new byte[]{16}, 0, 1, 0, 0, 3)); Assert.assertEquals("0000: 10\n", Hex.dump(new byte[]{16}, 0, 1, 0, 0, 4)); Assert.assertEquals("000000: 10\n", Hex.dump(new byte[]{16}, 0, 1, 0, 1, 6)); }
@Test public void testDumpthrowIllegalArgumentException() { byte[] bytes = new byte[]{17, 16, 17, 17, 17, 16, 17, 17, 17, 17}; thrown.expect(IllegalArgumentException.class); Hex.dump(bytes, 4, 1, -2147483648, 1, 5); }
@Test public void testDumpthrowIndexOutOfBoundsException() { thrown.expect(IndexOutOfBoundsException.class); Hex.dump(new byte[0], 4, 1, -2147483648, 1, 4); }