/** * Convert a full byte buffer to a human readable string of nicely formatted hex numbers using default hex format. * Output looks like: * * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * * @param bytes * @return formatted hex numbers in string */ public static String hex( ByteBuffer bytes ) { return hex( bytes, DEFAULT_BYTES_PER_GROUP, DEFAULT_GROUP_SEPARATOR ); }
/** * Convert a full byte buffer to a human readable string of nicely formatted hex numbers. * Output looks like: * * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * * @param bytes * @return formatted hex numbers in string */ public static String hex( byte[] bytes ) { return hex( bytes, DEFAULT_BYTES_PER_GROUP, DEFAULT_GROUP_SEPARATOR ); }
/** * Convert a subsection of a byte buffer to a human readable string of nicely formatted hex numbers. * Output looks like: * * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * * @param bytes * @param offset * @param length * @return formatted hex numbers in string */ public static String hex( ByteBuffer bytes, int offset, int length ) { return hex( bytes, offset, length, DEFAULT_BYTES_PER_GROUP, DEFAULT_GROUP_SEPARATOR ); }
public static String hex( byte[] bytes, int bytesPerBlock, String groupSep ) { return hex( wrap( bytes ), bytesPerBlock, groupSep ); }
public static String hex( ByteBuffer bytes, int bytesPerBlock, String groupSep ) { return hex( bytes, bytes.position(), bytes.limit(), bytesPerBlock, groupSep ); }
@Override public byte[] recv( int length ) throws IOException { byte[] bytes = new byte[length]; int left = length; int read; try { while ( left > 0 && (read = in.read( bytes, length - left, left )) != -1 ) { left -= read; } } catch ( SocketTimeoutException e ) { throw new SocketTimeoutException( "Reading data timed out, missing " + left + " bytes. Buffer: " + HexPrinter.hex( bytes ) ); } //all the bytes could not be read, fail if ( left != 0 ) { throw new IOException( "Failed to read " + length + " bytes, missing " + left + " bytes. Buffer: " + HexPrinter.hex( bytes ) ); } return bytes; }
@Test public void shouldPrintPartOfByteBuffer() { ByteBuffer bytes = ByteBuffer.allocate( 1024 ); for ( byte value = 0; value < 33; value++ ) { bytes.put( value ); } String hexString = HexPrinter.hex( bytes, 3, 8 ); assertEquals( format( "03 04 05 06 07 08 09 0A" ), hexString ); }
@Test public void shouldOnlyPrintBytesWrittenToBuffer() { // Given ByteBuffer bytes = ByteBuffer.allocate( 1024 ); for ( byte value = 0; value < 10; value++ ) { bytes.put( value ); } bytes.flip(); // When String hexString = HexPrinter.hex( bytes ); // Then assertEquals( format( "00 01 02 03 04 05 06 07 08 09" ), hexString ); } }
public static ResponseMessage responseMessage( Neo4jPack neo4jPack, byte[] bytes ) throws IOException { BoltResponseMessageReader unpacker = responseReader( neo4jPack, bytes ); BoltResponseMessageRecorder consumer = new BoltResponseMessageRecorder(); try { unpacker.read( consumer ); return consumer.asList().get( 0 ); } catch ( Throwable e ) { throw new IOException( "Failed to deserialize response, '" + e.getMessage() + "'.\n" + "Raw data: \n" + HexPrinter.hex( bytes ), e ); } }
private String serialized( AnyValue object ) throws IOException { RecordMessage message = new RecordMessage( record( object ) ); return HexPrinter.hex( serialize( neo4jPack, message ), 4, " " ); }
private String serialized( AnyValue object ) throws IOException { RecordMessage message = new RecordMessage( record( object ) ); return HexPrinter.hex( serialize( neo4jPack, message ), 4, " " ); }
@SuppressWarnings( "unchecked" ) private <T extends ResponseMessage> T unpack( BoltResponseMessageReader reader, RecordingByteChannel channel ) { // Unpack String serialized = HexPrinter.hex( channel.getBytes() ); BoltResponseMessageRecorder messages = new BoltResponseMessageRecorder(); try { reader.read( messages ); } catch ( Throwable e ) { throw new AssertionError( "Failed to unpack message, wire data was:\n" + serialized + "[" + channel .getBytes().length + "b]", e ); } return (T) messages.asList().get( 0 ); }
String serialized = HexPrinter.hex( bytes ); Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker( new PackedInputArray( bytes ) ); try
"Serialized data delivered in fragments: " + describeFragments( fragments ) + "\n" + "Unfragmented data: " + HexPrinter.hex( unfragmented ) + "\n", e );
@Override public String toString() { TransactionRepresentation tr = this.transactionRepresentation; return "Transaction #" + transactionId + (logPosition != null ? " at log position " + logPosition : " (no log position)") + " {started " + date( tr.getTimeStarted() ) + ", committed " + date( tr.getTimeCommitted() ) + ", with " + countCommands() + " commands in this transaction" + ", authored by " + tr.getAuthorId() + ", with master id " + tr.getMasterId() + ", lock session " + tr.getLockSessionId() + ", latest committed transaction id when started was " + tr.getLatestCommittedTxWhenStarted() + ", additional header bytes: " + HexPrinter.hex( tr.additionalHeader(), Integer.MAX_VALUE, "" ) + "}"; }
/** * Convert a full byte buffer to a human readable string of nicely formatted hex numbers using default hex format. * Output looks like: * * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 01 02 03 04 05 06 07 08 * * @param bytes * @return formatted hex numbers in string */ public static String hex( ByteBuffer bytes ) { return hex( bytes, DEFAULT_BYTES_PER_GROUP, DEFAULT_GROUP_SEPARATOR ); }
public static String hex( ByteBuffer bytes, int bytesPerBlock, String groupSep ) { return hex( bytes, bytes.position(), bytes.limit(), bytesPerBlock, groupSep ); }
@Test public void shouldChunkDataWhoseSizeIsGreaterThanOutputBufferCapacity() throws IOException { // Given out.writeBytes( new byte[16], 0, 16 ); // 2 + 16 is greater than the default max size 16 out.messageBoundaryHook().run(); out.flush(); // When & Then assertThat( writtenData.limit(), equalTo( 22 ) ); assertThat( HexPrinter.hex( writtenData, 0, 22 ), equalTo( "00 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 00 00 00" ) ); }
@Test public void shouldChunkSingleMessage() throws Throwable { // When out.writeByte( (byte) 1 ).writeShort( (short) 2 ); out.messageBoundaryHook().run(); out.flush(); // Then assertThat( writtenData.limit(), equalTo( 7 ) ); assertThat( HexPrinter.hex( writtenData, 0, 7 ), equalTo( "00 03 01 00 02 00 00" ) ); }
private Object unpacked( byte[] bytes ) throws IOException { System.out.println( HexPrinter.hex( bytes ) ); PackedInputArray input = new PackedInputArray( bytes ); Neo4jPack.Unpacker unpacker = new Neo4jPack.Unpacker( input ); return unpacker.unpack(); }