public static char[] encode(byte[] data, int length) throws Base85FormatException {
    char[] out = new char[(length / 4) * 5 + ((length % 4 != 0) ? length % 4 + 1 : 0)];
    int k = 0;
    // 64 bit integer
    long b;
    int c1, c2, c3, c4, c5;
    int rest;
    int i;

    for (i = 0; i + 4 <= length; i += 4) {
      b = 0L;
      b |= (int)data[i] & 0xFF;
      b <<= 8;
      b |= (int)data[i + 1] & 0xFF;
      b <<= 8;
      b |= (int)data[i + 2] & 0xFF;
      b <<= 8;
      b |= (int)data[i + 3] & 0xFF;

      c5 = (int)(b % 85);
      b /= 85;
      c4 = (int)(b % 85);
      b /= 85;
      c3 = (int)(b % 85);
      b /= 85;
      c2 = (int)(b % 85);
      b /= 85;
      c1 = (int)(b % 85);

      out[k] = encodeChar(c1);
      k++;
      out[k] = encodeChar(c2);
      k++;
      out[k] = encodeChar(c3);
      k++;
      out[k] = encodeChar(c4);
      k++;
      out[k] = encodeChar(c5);
      k++;
    }
    if ((rest = length % 4) != 0) {
      int j;
      byte[] block = {'~', '~', '~', '~'};
      for (j = 0; j < rest; j++) {
        block[j] = data[i + j];
      }
      char[] out_rest = encode(block, block.length);
      for (j = 0; j < rest + 1; j++) {
        out[k] = out_rest[j];
        k++;
      }
    }
    return out;
  }
public static char[] encode(byte[] data, int length)