static final String arrayToRLEString(short[] a){
  StringBuffer buffer=new StringBuffer();
  buffer.append((char)(a.length >> 16));
  buffer.append((char)a.length);
  short runValue=a[0];
  int runLength=1;
  for (int i=1; i < a.length; ++i) {
    short s=a[i];
    if (s == runValue && runLength < 0xFFFF)     ++runLength;
 else {
      encodeRun(buffer,runValue,runLength);
      runValue=s;
      runLength=1;
    }
  }
  encodeRun(buffer,runValue,runLength);
  return buffer.toString();
}

static final String arrayToRLEString ( short [ ] a ) { StringBuffer buffer = new StringBuffer ( ) ; buffer . append ( ( char ) ( a . length > > 16 ) ) ; buffer . append ( ( char ) a . length ) ; short runValue = a [ 0 ] ; int runLength = 1 ; for ( int i = 1 ; i < a . length ; ++ i ) { short s = a [ i ] ; if ( s == runValue && runLength < 0xFFFF ) ++ runLength ; else { encodeRun ( buffer , runValue , runLength ) ; runValue = s ; runLength = 1 ; } } encodeRun ( buffer , runValue , runLength ) ; return buffer . toString ( ) ; }
construct a string representing a short array . use run - length encoding . a character represents itself , unless it is the escape character . then the following notations are possible : escape escape escape literal escape n c n instances of character c since an encoded run occupies 3 characters , we only encode runs of 4 or more characters . thus we have n > 0 and n ! = escape and n < = 0xffff . if we encounter a run where n = = escape , we represent this as : c escape n - 1 c the escape value is chosen so as not to collide with commonly seen values .
