public static String encode(byte[] pData) {
        int offset = 0;
        int len;
        StringBuilder buf = new StringBuilder();

        while ((pData.length - offset) > 0) {
            byte a, b, c;
            if ((pData.length - offset) > 2) {
                len = 3;
            }
            else {
                len = pData.length - offset;
            }

            switch (len) {
                case 1:
                    a = pData[offset];
                    b = 0;
                    buf.append(PEM_ARRAY[(a >>> 2) & 0x3F]);
                    buf.append(PEM_ARRAY[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
                    buf.append('=');
                    buf.append('=');
                    offset++;
                    break;
                case 2:
                    a = pData[offset];
                    b = pData[offset + 1];
                    c = 0;
                    buf.append(PEM_ARRAY[(a >>> 2) & 0x3F]);
                    buf.append(PEM_ARRAY[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
                    buf.append(PEM_ARRAY[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
                    buf.append('=');
                    offset += offset + 2; // ???
                    break;
                default:
                    a = pData[offset];
                    b = pData[offset + 1];
                    c = pData[offset + 2];
                    buf.append(PEM_ARRAY[(a >>> 2) & 0x3F]);
                    buf.append(PEM_ARRAY[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
                    buf.append(PEM_ARRAY[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
                    buf.append(PEM_ARRAY[c & 0x3F]);
                    offset = offset + 3;
                    break;
            }

        }
        return buf.toString();
    }
public static String encode ( byte [ ] pData ) { int offset = 0 ; int len ; StringBuilder buf = new StringBuilder ( ) ; while ( ( pData . length - offset ) > 0 ) { byte a , b , c ; if ( ( pData . length - offset ) > 2 ) { len = 3 ; } else { len = pData . length - offset ; } switch ( len ) { case 1 : a = pData [ offset ] ; b = 0 ; buf . append ( PEM_ARRAY [ ( a > > > 2 ) & 0x3F ] ) ; buf . append ( PEM_ARRAY [ ( ( a << 4 ) & 0x30 ) + ( ( b > > > 4 ) & 0xf ) ] ) ; buf . append ( '=' ) ; buf . append ( '=' ) ; offset ++ ; break ; case 2 : a = pData [ offset ] ; b = pData [ offset + 1 ] ; c = 0 ; buf . append ( PEM_ARRAY [ ( a > > > 2 ) & 0x3F ] ) ; buf . append ( PEM_ARRAY [ ( ( a << 4 ) & 0x30 ) + ( ( b > > > 4 ) & 0xf ) ] ) ; buf . append ( PEM_ARRAY [ ( ( b << 2 ) & 0x3c ) + ( ( c > > > 6 ) & 0x3 ) ] ) ; buf . append ( '=' ) ; offset += offset + 2 ; break ; default : a = pData [ offset ] ; b = pData [ offset + 1 ] ; c = pData [ offset + 2 ] ; buf . append ( PEM_ARRAY [ ( a > > > 2 ) & 0x3F ] ) ; buf . append ( PEM_ARRAY [ ( ( a << 4 ) & 0x30 ) + ( ( b > > > 4 ) & 0xf ) ] ) ; buf . append ( PEM_ARRAY [ ( ( b << 2 ) & 0x3c ) + ( ( c > > > 6 ) & 0x3 ) ] ) ; buf . append ( PEM_ARRAY [ c & 0x3F ] ) ; offset = offset + 3 ; break ; } } return buf . toString ( ) ; }
Encodes the input data using the standard base64 encoding scheme.

@param pData the bytes to encode to base64
@return a string with base64 encoded data