Outputs base64 representation of the specified stream data to a Writer. - Android File Input Output

Android examples for File Input Output:Base64

Description

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

Demo 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 ww .j  a  va 2  s  .  c  o m*/
     * 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 Tutorials