Android Base64 Byte Array Encode encode(byte[] data)

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

Description

encode

Declaration

public static String encode(byte[] data) 

Method Source Code

//package com.java2s;

public class Main {
    private static final char[] ENCODE_CHARS = 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 encode(byte[] data) {
        StringBuffer buffer = new StringBuffer();
        int length = data.length;
        int indexx = 0;
        int b1, b2, b3;
        while (indexx < length) {
            b1 = data[indexx++] & 0xff;
            if (indexx == length) {
                buffer.append(ENCODE_CHARS[b1 >>> 2]);
                buffer.append(ENCODE_CHARS[(b1 & 0x3) << 4]);
                buffer.append("==");
                break;
            }//from w  w w .  jav  a2 s.c om
            b2 = data[indexx++] & 0xff;
            if (indexx == length) {
                buffer.append(ENCODE_CHARS[b1 >>> 2]);
                buffer.append(ENCODE_CHARS[((b1 & 0x03) << 4)
                        | ((b2 & 0xf0) >>> 4)]);
                buffer.append(ENCODE_CHARS[(b2 & 0x0f) << 2]);
                buffer.append("=");
                break;
            }
            b3 = data[indexx++] & 0xff;
            buffer.append(ENCODE_CHARS[b1 >>> 2]);
            buffer.append(ENCODE_CHARS[((b1 & 0x03) << 4)
                    | ((b2 & 0xf0) >>> 4)]);
            buffer.append(ENCODE_CHARS[((b2 & 0x0f) << 2)
                    | ((b3 & 0xc0) >>> 6)]);
            buffer.append(ENCODE_CHARS[b3 & 0x3f]);
        }
        return buffer.toString();
    }
}

Related

  1. encode(byte[] bytes)
  2. encode(byte[] data)
  3. encode(byte[] data)
  4. encode(byte[] data)
  5. encode(byte[] data, int off, int len, Writer writer)
  6. base64(byte[] target)
  7. toBase64(byte[] data)
  8. encodeBlock(byte raw[], int offset)