question: static final String arrayToRLEString ( byte [ ] a ) { StringBuffer buffer = new StringBuffer ( ) ; buffer . append ( ( char ) ( a . length > > 16 ) ) ; buffer . append ( ( char ) a . length ) ; byte runValue = a [ 0 ] ; int runLength = 1 ; byte [ ] state = new byte [ 2 ] ; for ( int i = 1 ; i < a . length ; ++ i ) { byte b = a [ i ] ; if ( b == runValue && runLength < 0xFF ) ++ runLength ; else { encodeRun ( buffer , runValue , runLength , state ) ; runValue = b ; runLength = 1 ; } } encodeRun ( buffer , runValue , runLength , state ) ; if ( state [ 0 ] != 0 ) appendEncodedByte ( buffer , ( byte ) 0 , state ) ; return buffer . toString ( ) ; }
target:  Construct a string representing a byte array.  Use run-length encoding. Two bytes are packed into a single char, with a single extra zero byte at the end if needed.  A byte represents itself, unless it is the ESCAPE_BYTE.  Then the following notations are possible: ESCAPE_BYTE ESCAPE_BYTE   ESCAPE_BYTE literal ESCAPE_BYTE n b           n instances of byte b Since an encoded run occupies 3 bytes, we only encode runs of 4 or more bytes.  Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF. If we encounter a run where n == ESCAPE_BYTE, we represent this as: b ESCAPE_BYTE n-1 b The ESCAPE_BYTE value is chosen so as not to collide with commonly seen values.