public static String encode(byte source[]){
  int offset=0;
  int num=0;
  int numBytes=0;
  StringBuilder sb=new StringBuilder();
  for (int i=0; i < source.length; i++) {
    int b=source[offset++];
    if (b < 0)     b+=256;
    num=(num << 8) + b;
    if (++numBytes != 3)     continue;
    sb.append(intToBase64[num >> 18]);
    sb.append(intToBase64[num >> 12 & 0x3f]);
    sb.append(intToBase64[num >> 6 & 0x3f]);
    sb.append(intToBase64[num & 0x3f]);
    num=0;
    numBytes=0;
  }
  if (numBytes > 0) {
    if (numBytes == 1) {
      sb.append(intToBase64[num >> 2]);
      sb.append(intToBase64[num << 4 & 0x3f]);
      sb.append("==");
    }
 else {
      sb.append(intToBase64[num >> 10]);
      sb.append(intToBase64[num >> 4 & 0x3f]);
      sb.append(intToBase64[num << 2 & 0x3f]);
      sb.append('=');
    }
  }
  return sb.toString();
}

public static String encode ( byte source [ ] ) { int offset = 0 ; int num = 0 ; int numBytes = 0 ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < source . length ; i ++ ) { int b = source [ offset ++ ] ; if ( b < 0 ) b += 256 ; num = ( num << 8 ) + b ; if ( ++ numBytes != 3 ) continue ; sb . append ( intToBase64 [ num > > 18 ] ) ; sb . append ( intToBase64 [ num > > 12 & 0x3f ] ) ; sb . append ( intToBase64 [ num > > 6 & 0x3f ] ) ; sb . append ( intToBase64 [ num & 0x3f ] ) ; num = 0 ; numBytes = 0 ; } if ( numBytes > 0 ) { if ( numBytes == 1 ) { sb . append ( intToBase64 [ num > > 2 ] ) ; sb . append ( intToBase64 [ num << 4 & 0x3f ] ) ; sb . append ( "==" ) ; } else { sb . append ( intToBase64 [ num > > 10 ] ) ; sb . append ( intToBase64 [ num > > 4 & 0x3f ] ) ; sb . append ( intToBase64 [ num << 2 & 0x3f ] ) ; sb . append ( '=' ) ; } } return sb . toString ( ) ; }
converts a byte array to a bae64 - encoded string .
