public static String hex( ByteBuffer bytes, int offset, int length, int bytesPerBlock, String groupSep ) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); new HexPrinter( out, bytesPerBlock, groupSep ).append( bytes, offset, length ); out.flush(); return outStream.toString(); }
@Test public void shouldPrintACoupleOfLines() { // GIVEN ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); HexPrinter printer = new HexPrinter( out ); // WHEN for ( byte value = 0; value < 40; value++ ) { printer.append( value ); } // THEN out.flush(); assertEquals( format( "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F " + "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F%n" + "20 21 22 23 24 25 26 27" ), outStream.toString() ); }
@Test public void shouldPrintUserSpecifiedBytesGroupingFormat() { // GIVEN ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); HexPrinter printer = new HexPrinter( out ).withBytesGroupingFormat( 12, 4, ", " ); // WHEN for ( byte value = 0; value < 30; value++ ) { printer.append( value ); } // THEN out.flush(); assertEquals( format( "00 01 02 03, 04 05 06 07, 08 09 0A 0B%n" + "0C 0D 0E 0F, 10 11 12 13, 14 15 16 17%n" + "18 19 1A 1B, 1C 1D" ), outStream.toString() ); }
@Test public void shouldPrintUserSpecifiedLineNumberFormat() { // GIVEN ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); HexPrinter printer = new HexPrinter( out ).withLineNumberFormat( 5, "[", "]" ); // WHEN for ( byte value = 0; value < 40; value++ ) { printer.append( value ); } // THEN out.flush(); assertEquals( format( "[0x00000]" + "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F " + "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F%n" + "[0x00001]" + "20 21 22 23 24 25 26 27" ), outStream.toString() ); }
@Test public void shouldNotGroupingWhenBytesPerGroupIsGreaterThanBytesPerLine() { // GIVEN ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); HexPrinter printer = new HexPrinter( out ).withBytesPerLine( 12 ).withBytesPerGroup( 100 ); // WHEN for ( byte value = 0; value < 30; value++ ) { printer.append( value ); } // THEN out.flush(); assertEquals( format( "00 01 02 03 04 05 06 07 08 09 0A 0B%n" + "0C 0D 0E 0F 10 11 12 13 14 15 16 17%n" + "18 19 1A 1B 1C 1D" ), outStream.toString() ); }
@Test public void shouldStartFromUserSpecifiedLineNumber() { // GIVEN ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); HexPrinter printer = new HexPrinter( out ).withLineNumberDigits( 2 ).withLineNumberOffset( 0xA8 ); // WHEN for ( byte value = 0; value < 40; value++ ) { printer.append( value ); } // THEN out.flush(); assertEquals( format( "@ 0xA8: " + "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F " + "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F%n" + "@ 0xA9: " + "20 21 22 23 24 25 26 27" ), outStream.toString() ); }
public static String hex( ByteBuffer bytes, int offset, int length, int bytesPerBlock, String groupSep ) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); PrintStream out = new PrintStream( outStream ); new HexPrinter( out, bytesPerBlock, groupSep ).append( bytes, offset, length ); out.flush(); return outStream.toString(); }
protected static String beginningOfBufferAsHexString( ChannelBuffer buffer, int maxBytesToPrint ) { // read buffer from pos 0 - writeIndex int prevIndex = buffer.readerIndex(); buffer.readerIndex( 0 ); try { ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream( buffer.readableBytes() ); PrintStream stream = new PrintStream( byteArrayStream ); HexPrinter printer = new HexPrinter( stream ).withLineNumberDigits( 4 ); for ( int i = 0; buffer.readable() && i < maxBytesToPrint; i++ ) { printer.append( buffer.readByte() ); } stream.flush(); return byteArrayStream.toString(); } finally { buffer.readerIndex( prevIndex ); } }