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

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

Description

Outputs base64 representation of the specified stream data to a String.

Declaration

public static String encode(byte[] data) 

Method Source Code

//package com.java2s;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.StringWriter;
import java.io.Writer;

public class Main {
    /**/*from  w  w w . ja  v a  2s  .  c om*/
     * Charset used for base64 encoded data (7-bit ASCII).
     */
    private final static String CHARSET = "US-ASCII";
    /**
     * Encoding table (the 64 valid base64 characters).
     */
    private final static char[] BASE64CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
            .toCharArray();
    /**
     * pad character.
     */
    private static final char BASE64PAD = '=';

    /**
     * Outputs base64 representation of the specified stream data to a <code>Writer</code>.
     */
    public static void encode(InputStream in, Writer writer)
            throws IOException {
        byte[] buffer = new byte[9 * 1024];
        int read;

        while ((read = in.read(buffer)) > 0) {
            encode(buffer, 0, read, writer);
        }
    }

    /**
     * Outputs base64 representation of the specified stream data to a <code>String</code>.
     */
    public static String encode(byte[] data) {
        StringWriter writer = new StringWriter();
        try {
            encode(data, 0, data.length, writer);
        } catch (IOException ioe) {
            throw new IllegalStateException(ioe);
        }
        return writer.toString();
    }

    /**
     * Outputs base64 representation of the specified stream data to an <code>OutputStream</code>.
     */
    public static void encode(InputStream in, OutputStream out)
            throws IOException {
        Writer writer = new OutputStreamWriter(out, CHARSET);
        encode(in, writer);
    }

    /**
     * Outputs base64 representation of the specified data to a <code>Writer</code>.
     */
    public static void encode(byte[] data, int off, int len, Writer writer)
            throws IOException {
        if (len == 0) {
            return;
        }
        if (len < 0 || off >= data.length || len + off > data.length) {
            throw new IllegalArgumentException();
        }
        char[] enc = new char[4];
        while (len >= 3) {
            int i = ((data[off] & 0xff) << 16)
                    + ((data[off + 1] & 0xff) << 8)
                    + (data[off + 2] & 0xff);
            enc[0] = BASE64CHARS[i >> 18];
            enc[1] = BASE64CHARS[(i >> 12) & 0x3f];
            enc[2] = BASE64CHARS[(i >> 6) & 0x3f];
            enc[3] = BASE64CHARS[i & 0x3f];
            writer.write(enc, 0, 4);
            off += 3;
            len -= 3;
        }
        // add padding if necessary
        if (len == 1) {
            int i = data[off] & 0xff;
            enc[0] = BASE64CHARS[i >> 2];
            enc[1] = BASE64CHARS[(i << 4) & 0x3f];
            enc[2] = BASE64PAD;
            enc[3] = BASE64PAD;
            writer.write(enc, 0, 4);
        } else if (len == 2) {
            int i = ((data[off] & 0xff) << 8) + (data[off + 1] & 0xff);
            enc[0] = BASE64CHARS[i >> 10];
            enc[1] = BASE64CHARS[(i >> 4) & 0x3f];
            enc[2] = BASE64CHARS[(i << 2) & 0x3f];
            enc[3] = BASE64PAD;
            writer.write(enc, 0, 4);
        }
    }
}

Related

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