Android Base64 Encode encodeBase64(byte abyte0[])

Here you can find the source of encodeBase64(byte abyte0[])

Description

encode Base

Declaration

public static String encodeBase64(byte abyte0[]) 

Method Source Code

//package com.java2s;

public class Main {
    private static final char ENCODE64[] = { 'A', 'B', 'C', 'D', 'E', 'F',
            'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
            'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
            'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1',
            '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };
    private static char sPad = '=';

    public static String encodeBase64(byte abyte0[]) {
        if (abyte0 == null)
            return "";
        char ac[] = new char[4 * ((2 + abyte0.length) / 3)];
        int i = 0;
        for (int j = 0; j < -2 + abyte0.length; j += 3) {
            int k2 = ((0xff & abyte0[j]) << 16)
                    + ((0xff & abyte0[j + 1]) << 8)
                    + (0xff & abyte0[j + 2]);
            int l2 = i + 1;
            ac[i] = ENCODE64[k2 >> 18];
            int i3 = l2 + 1;
            ac[l2] = ENCODE64[0x3f & k2 >> 12];
            int j3 = i3 + 1;
            ac[i3] = ENCODE64[0x3f & k2 >> 6];
            i = j3 + 1;/*  w  w  w .j a v  a 2s . com*/
            ac[j3] = ENCODE64[k2 & 0x3f];
        }

        switch (abyte0.length % 3) {
        case 2: // '\002'
            int k1 = ((0xff & abyte0[-2 + abyte0.length]) << 16)
                    + ((0xff & abyte0[-1 + abyte0.length]) << 8);
            int l1 = i + 1;
            ac[i] = ENCODE64[k1 >> 18];
            int i2 = l1 + 1;
            ac[l1] = ENCODE64[0x3f & k1 >> 12];
            int j2 = i2 + 1;
            ac[i2] = ENCODE64[0x3f & k1 >> 6];
            int _tmp = j2 + 1;
            ac[j2] = sPad;
            break;

        case 1: // '\001'
            int k = (0xff & abyte0[-1 + abyte0.length]) << 16;
            int l = i + 1;
            ac[i] = ENCODE64[k >> 18];
            int i1 = l + 1;
            ac[l] = ENCODE64[0x3f & k >> 12];
            int j1 = i1 + 1;
            ac[i1] = sPad;
            int _tmp1 = j1 + 1;
            ac[j1] = sPad;
            break;
        }
        return new String(ac);
    }
}

Related

  1. base64Digest(String input)
  2. base64Encode(final byte[] data)
  3. encode(byte[] src)
  4. to64(String a)
  5. toBase64(byte[] barray)
  6. encodeBase64(String input)
  7. encryptToBase64Text(String key, String src)
  8. generateBase64String(String inString)
  9. ImageBase64Decode(String base64_str)