public static String base64Encode(byte[] src) {
        int unpadded = (src.length * 8 + 5) / 6;
        int padding = 4 - (unpadded % 4);
        int d = 0;
        int e = 3;
        long buffer = 0;
        if (padding == 4)
            padding = 0;
        char[] encoded = new char[unpadded + padding];

        while (d < src.length) {
            // Push into buffer in 8-bit chunks
            for (int i = 0; i < 3; i++) {
                buffer <<= 8;
                buffer |= ((d < src.length ? src[d++] & 0x00ff : 0));
            }
            // Pop from buffer in 6-bit chunks
            for (int i = 0; i < 4; i++) {
                encoded[e--] = e64[(int) (buffer & 0x3f)];
                buffer >>>= 6;
            }
            e += 8;
        }
        while (padding > 0) {
            encoded[unpadded + --padding] = '=';
        }
        return new String(encoded);
    }