{
  public static final String toBase64(byte[] buffer)  {    int len = buffer.length, pos = len % 3;    byte b0 = 0, b1 = 0, b2 = 0;    switch (pos)      {      case 1:        b2 = buffer[0];        break;      case 2:        b1 = buffer[0];        b2 = buffer[1];        break;      }    StringBuffer sb = new StringBuffer();    int c;    boolean notleading = false;    do      {        c = (b0 & 0xFC) >>> 2;        if (notleading || c != 0)          {            sb.append(BASE64_CHARSET[c]);            notleading = true;          }        c = ((b0 & 0x03) << 4) | ((b1 & 0xF0) >>> 4);        if (notleading || c != 0)          {            sb.append(BASE64_CHARSET[c]);            notleading = true;          }        c = ((b1 & 0x0F) << 2) | ((b2 & 0xC0) >>> 6);        if (notleading || c != 0)          {            sb.append(BASE64_CHARSET[c]);            notleading = true;          }        c = b2 & 0x3F;        if (notleading || c != 0)          {            sb.append(BASE64_CHARSET[c]);            notleading = true;          }        if (pos >= len)          {            break;          }        else          {            try              {                b0 = buffer[pos++];                b1 = buffer[pos++];                b2 = buffer[pos++];              }            catch (ArrayIndexOutOfBoundsException x)              {                break;              }          }      }    while (true);    if (notleading)      {        return sb.toString();      }    return "0";  }