Java Base64 Encode base64Encode(byte[] data)

Here you can find the source of base64Encode(byte[] data)

Description

base Encode

License

Open Source License

Declaration

public static String base64Encode(byte[] data) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final char[] base64EncodeChars = new char[] { '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', '+', '/' };

    public static String base64Encode(byte[] data) {
        StringBuilder sb = new StringBuilder();
        int r = data.length % 3;
        int len = data.length - r;
        int i = 0;
        int c;/*from w  w w.j  a  v a  2s .  co  m*/
        while (i < len) {
            c = (0x000000ff & data[i++]) << 16 | (0x000000ff & data[i++]) << 8 | (0x000000ff & data[i++]);
            sb.append(base64EncodeChars[c >> 18]);
            sb.append(base64EncodeChars[c >> 12 & 0x3f]);
            sb.append(base64EncodeChars[c >> 6 & 0x3f]);
            sb.append(base64EncodeChars[c & 0x3f]);
        }
        if (r == 1) {
            c = 0x000000ff & data[i++];
            sb.append(base64EncodeChars[c >> 2]);
            sb.append(base64EncodeChars[(c & 0x03) << 4]);
            sb.append("==");
        } else if (r == 2) {
            c = (0x000000ff & data[i++]) << 8 | (0x000000ff & data[i++]);
            sb.append(base64EncodeChars[c >> 10]);
            sb.append(base64EncodeChars[c >> 4 & 0x3f]);
            sb.append(base64EncodeChars[(c & 0x0f) << 2]);
            sb.append("=");
        }
        return sb.toString();
    }
}

Related

  1. base64Encode(byte[] bytes)
  2. base64Encode(byte[] bytes)
  3. base64Encode(byte[] bytes)
  4. base64encode(byte[] bytes)
  5. base64encode(byte[] data)
  6. base64Encode(byte[] data)
  7. base64Encode(byte[] in)
  8. base64Encode(byte[] in)
  9. base64Encode(byte[] in)