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

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