ByteBufferDumper { public static String dump(ByteBuffer buffer, int nbBytes, boolean toAscii) { byte data[]; int start; int size = Math.min(buffer.remaining(), nbBytes >= 0 ? nbBytes : Integer.MAX_VALUE); int length = buffer.remaining(); if (buffer.hasArray() && !buffer.isReadOnly()) { start = buffer.position(); data = buffer.array(); } else { data = new byte[size]; int oldpos = buffer.position(); buffer.get(data); buffer.position(oldpos); start = 0; length = data.length; } boolean binaryContent = false; if (toAscii) { for (int i = start; i < start + size; i++) { byte b = data[i]; if (((b < 32) || (b > 126)) && (b != 13) && (b != 10)) { binaryContent = true; break; } } } if (!toAscii || binaryContent) { StringBuilder out = new StringBuilder(size * 3 + 30); out.append("ByteBuffer[len=").append(length).append(",bytes='"); int byteValue = data[start] & 0xFF; boolean isFirst = true; for (int i = start; i < start + size; i++) { if (isFirst) { isFirst = false; } else { out.append(' '); } byteValue = data[i] & 0xFF; out.append(new String(new byte[] { '0', 'x', HEX_CHAR[(byteValue & 0x00F0) >> 4], HEX_CHAR[byteValue & 0x000F] })); } out.append("']"); return out.toString(); } else { StringBuilder sb = new StringBuilder(size); sb.append("ByteBuffer[len=").append(length).append(",str='").append(new String(data, start, size)) .append("']"); return sb.toString(); } }  static String dump(ByteBuffer buffer, int nbBytes, boolean toAscii); static String dump(ByteBuffer buffer); static String toHex(ByteBuffer buffer); static ByteBuffer fromHexString(String hex);  }
@Test public void stringTest() { String toTest = "yopYOP\n\r"; byte[] charData = toTest.getBytes(); assertEquals(toTest.length(), charData.length); ByteBuffer myBuffer = ByteBuffer.allocate(toTest.length()); for (int i = 0; i < toTest.length(); i++) { myBuffer.put(charData[i]); } myBuffer.flip(); int remaining = myBuffer.remaining(); int pos = myBuffer.position(); String dump = ByteBufferDumper.dump(myBuffer); assertEquals("ByteBuffer[len=8,str='" + toTest + "']", dump); assertEquals(remaining, myBuffer.remaining()); assertEquals(pos, myBuffer.position()); }
@Test public void binaryTest() { ByteBuffer myBuffer = ByteBuffer.allocate(4); myBuffer.put((byte) 0x88); myBuffer.put((byte) 0x03); myBuffer.put((byte) 0xFF); myBuffer.flip(); int remaining = myBuffer.remaining(); int pos = myBuffer.position(); String dump = ByteBufferDumper.dump(myBuffer); System.err.println(dump); assertEquals("ByteBuffer[len=3,bytes='0x88 0x03 0xFF']", dump); assertEquals(remaining, myBuffer.remaining()); assertEquals(pos, myBuffer.position()); }
@Test public void testWithSizeLimit() { ByteBuffer bb = ByteBuffer.allocate(10); bb.put(new byte[] { 0x01, (byte) 0x8F, 0x04, 0x7A, (byte) 0xc2, 0x23, (byte) 0xA0, 0x08, 0x44 }); bb.flip(); assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']", ByteBufferDumper.dump(bb, 5, false)); assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2']", ByteBufferDumper.dump(bb, 5, true)); assertEquals("ByteBuffer[len=9,str='']", ByteBufferDumper.dump(bb, 0, true)); assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23 0xA0 0x08 0x44']", ByteBufferDumper.dump(bb, 10, true)); assertEquals("ByteBuffer[len=9,bytes='0x01 0x8F 0x04 0x7A 0xC2 0x23 0xA0 0x08 0x44']", ByteBufferDumper.dump(bb, -1, false)); }