Decode a Base64-encoded string to a byte array and writes decoded data to output stream. - Java java.lang

Java examples for java.lang:String Base64

Description

Decode a Base64-encoded string to a byte array and writes decoded data to output stream.

Demo Code


//package com.java2s;
import java.io.OutputStream;

import java.io.IOException;

public class Main {
    /**/*from  w  w w .  j  a v a2  s .  co m*/
     * <p>Decode a Base64-encoded string to a byte array
     * and writes decoded data to output stream. Returns
     * the number of bytes from input data used. Caller must
     * pass in the unused bytes on the next call.
     * </p>
     * @param base64 <code>String</code> encoded string. Whitespace will
     * be ignored.
     * @param out output stream to write decoded data
     * @param bLastBlock true if this is the last block of input data
     * @return number of handled bytes from input data
     */
    public static int decodeBlock(String base64, OutputStream out,
            boolean bLastBlock) {
        int nUsed = 0, nPos = 0, nDec = 0;
        StringBuffer sbBlock = null;
        do {
            // collect the next 4 characters, skip whitespace
            sbBlock = new StringBuffer();
            while (nPos < base64.length() && sbBlock.length() < 4) {
                char ch = base64.charAt(nPos);
                if (ch != ' ' && ch != '\n' && ch != '\t' && ch != '\r')
                    sbBlock.append(ch);
                nPos++;
            }
            // if last block then pad
            while (bLastBlock && sbBlock.length() < 4)
                sbBlock.append('=');
            // decode if possible
            if (sbBlock.length() == 4) {
                //byte[] decdata = decodeWithoutWhitespace(sbBlock.toString());
                int block = (getValue(sbBlock.charAt(0)) << 18)
                        + (getValue(sbBlock.charAt(1)) << 12)
                        + (getValue(sbBlock.charAt(2)) << 6)
                        + (getValue(sbBlock.charAt(3)));
                byte[] decdata = new byte[3];
                for (int j = 2; j >= 0; j--) {
                    decdata[j] = (byte) (block & 0xff);
                    block >>= 8;
                }
                nDec += decdata.length;
                try {
                    out.write(decdata);
                } catch (IOException ex) {

                }
                nUsed = nPos;
            }

        } while (nPos < base64.length());
        //       if(m_logger.isDebugEnabled())
        //          m_logger.debug("Decoding: " + base64.length() + " last: " + bLastBlock + " used: " + nUsed + " decoded: " + nDec);
        return nUsed;
    }

    /**
     * Method getValue
     *
     * @param c
     * @return
     */
    protected static int getValue(char c) {
        if ((c >= 'A') && (c <= 'Z'))
            return c - 'A';
        if ((c >= 'a') && (c <= 'z'))
            return c - 'a' + 26;
        if ((c >= '0') && (c <= '9'))
            return c - '0' + 52;
        if (c == '+')
            return 62;
        if (c == '/')
            return 63;
        if (c == '=')
            return 0;
        return -1;
    }
}

Related Tutorials