protected static String formatMessage ( String message , byte [ ] hostData , int start , int curPos , int bytesLen ) { StringBuilder sb = new StringBuilder ( ) ; sb . append ( message ) ; if ( hostData != null && hostData . length > 0 && curPos < hostData . length && curPos >= start && bytesLen > 0 && start + bytesLen <= hostData . length ) { sb . append ( ". Error at offset " ) ; sb . append ( curPos ) ; sb . append ( " : [0x" ) ; int spyStart = Math . max ( start - SPYBUF_MAX_LEN , 0 ) ; appendData ( sb , hostData , spyStart , start - spyStart ) ; sb . append ( start > spyStart ? "->" : "" ) ; if ( curPos > start ) { appendData ( sb , hostData , start , curPos - start ) ; sb . append ( "^" ) ; appendData ( sb , hostData , curPos , bytesLen + start - curPos ) ; } else { appendData ( sb , hostData , start , bytesLen ) ; } int spyStop = Math . min ( start + bytesLen + SPYBUF_MAX_LEN , hostData . length ) ; sb . append ( spyStop > start + bytesLen ? "<-" : "" ) ; appendData ( sb , hostData , start + bytesLen , spyStop - start - bytesLen ) ; sb . append ( "]" ) ; } else { sb . append ( ". Position is " ) ; sb . append ( curPos ) ; } return sb . toString ( ) ; }
Format the data to make it easy to locate the error in the incoming byte stream .