Decode base64 encoded data. - Android File Input Output

Android examples for File Input Output:Base64

Description

Decode base64 encoded data.

Demo Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import java.io.Reader;

public class Main {
    /**//from   ww w.ja  va  2s  . c  o m
     * Charset used for base64 encoded data (7-bit ASCII).
     */
    private final static String CHARSET = "US-ASCII";
    /**
     * Decoding table.
     */
    private final static byte[] DECODETABLE = new byte[128];
    /**
     * pad character.
     */
    private static final char BASE64PAD = '=';

    /**
     * Decode base64 encoded data.
     */
    public static void decode(Reader reader, OutputStream out)
            throws IOException {
        char[] chunk = new char[8192];
        int read;
        while ((read = reader.read(chunk)) > -1) {
            decode(chunk, 0, read, out);
        }
    }

    /**
     * Decode base64 encoded data. The data read from the inputstream is assumed to be of charset "US-ASCII".
     */
    public static void decode(InputStream in, OutputStream out)
            throws IOException {
        decode(new InputStreamReader(in, CHARSET), out);
    }

    /**
     * Decode base64 encoded data.
     */
    public static void decode(String data, OutputStream out)
            throws IOException {
        char[] chars = data.toCharArray();
        decode(chars, 0, chars.length, out);
    }

    public static byte[] decode(String data) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(
                data.length());
        try {
            decode(data, baos);
        } catch (IOException ioe) {
            throw new IllegalStateException(ioe);
        }
        return baos.toByteArray();
    }

    /**
     * Decode base64 encoded data.
     */
    public static void decode(char[] chars, OutputStream out)
            throws IOException {
        decode(chars, 0, chars.length, out);
    }

    /**
     * Decode base64 encoded data.
     */
    public static void decode(char[] chars, int off, int len,
            OutputStream out) throws IOException {
        if (len == 0) {
            return;
        }
        if (len < 0 || off >= chars.length || len + off > chars.length) {
            throw new IllegalArgumentException();
        }
        char[] chunk = new char[4];
        byte[] dec = new byte[3];
        int posChunk = 0;
        // decode in chunks of 4 characters
        for (int i = off; i < (off + len); i++) {
            char c = chars[i];
            if (c < DECODETABLE.length && DECODETABLE[c] != 0x7f
                    || c == BASE64PAD) {
                chunk[posChunk++] = c;
                if (posChunk == chunk.length) {
                    int b0 = DECODETABLE[chunk[0]];
                    int b1 = DECODETABLE[chunk[1]];
                    int b2 = DECODETABLE[chunk[2]];
                    int b3 = DECODETABLE[chunk[3]];
                    if (chunk[3] == BASE64PAD && chunk[2] == BASE64PAD) {
                        dec[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                        out.write(dec, 0, 1);
                    } else if (chunk[3] == BASE64PAD) {
                        dec[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                        dec[1] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
                        out.write(dec, 0, 2);
                    } else {
                        dec[0] = (byte) (b0 << 2 & 0xfc | b1 >> 4 & 0x3);
                        dec[1] = (byte) (b1 << 4 & 0xf0 | b2 >> 2 & 0xf);
                        dec[2] = (byte) (b2 << 6 & 0xc0 | b3 & 0x3f);
                        out.write(dec, 0, 3);
                    }
                    posChunk = 0;
                }
            } else {
                throw new IllegalArgumentException(
                        "specified data is not base64 encoded");
            }
        }
    }
}

Related Tutorials