@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)); }
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(); } }
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(); } } }
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(); } }  }
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); }
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);  }