Java Base64 Decode decodeBase64(String data)

Here you can find the source of decodeBase64(String data)

Description

Decodes a base64 String.

License

LGPL

Parameter

Parameter Description
data a base64 encoded String to decode.

Exception

Parameter Description
UnsupportedEncodingException an exception

Return

the decoded String.

Declaration

public static String decodeBase64(String data) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/**/* w w  w .j  av  a  2 s .com*/
 * Converts a line of text into an array of lower case words using a
 * BreakIterator.wordInstance(). <p>
 *
 * This method is under the Jive Open Source Software License and was
 * written by Mark Imbriaco.
 *
 * @param text a String of text to convert into an array of words
 * @return text broken up into an array of words.
 */

import java.io.UnsupportedEncodingException;

public class Main {
    private static final int fillchar = '=';
    private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789+/";

    /**
     * Decodes a base64 String.
     *
     * @param data a base64 encoded String to decode.
     * @return the decoded String.
     * @throws UnsupportedEncodingException 
     */
    public static String decodeBase64(String data) throws UnsupportedEncodingException {
        return decodeBase64(data.getBytes("GBK"));
    }

    /**
     * Decodes a base64 aray of bytes.
     *
     * @param data a base64 encode byte array to decode.
     * @return the decoded String.
     */
    public static String decodeBase64(byte[] data) {
        int c, c1;
        int len = data.length;
        StringBuffer ret = new StringBuffer((len * 3) / 4);
        for (int i = 0; i < len; ++i) {
            c = cvt.indexOf(data[i]);
            ++i;
            c1 = cvt.indexOf(data[i]);
            c = ((c << 2) | ((c1 >> 4) & 0x3));
            ret.append((char) c);
            if (++i < len) {
                c = data[i];
                if (fillchar == c)
                    break;

                c = cvt.indexOf((char) c);
                c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
                ret.append((char) c1);
            }

            if (++i < len) {
                c1 = data[i];
                if (fillchar == c1)
                    break;

                c1 = cvt.indexOf((char) c1);
                c = ((c << 6) & 0xc0) | c1;
                ret.append((char) c);
            }
        }
        return ret.toString();
    }

    public static int indexOf(CharSequence sb, char[] buff) {
        return indexOf(sb, buff, 0);
    }

    public static int indexOf(CharSequence sb, char[] buff, int fromIndex) {
        outer: for (int i = fromIndex; i <= sb.length() - buff.length; i++) {
            for (int j = 0; j < buff.length; j++) {
                if (sb.charAt(i + j) != buff[j]) {
                    continue outer;
                }
            }
            return i;
        }
        return -1;
    }
}

Related

  1. decodeBase64(char[] data)
  2. decodeBASE64(InputStream in, OutputStream out)
  3. decodeBase64(InputStream is, OutputStream os)
  4. decodeBase64(String base64Data)
  5. decodeBase64(String data)
  6. decodeBASE64(String pEncoded)
  7. decodeBase64(String str)
  8. decodeBase64AsUtf8(String data)
  9. decodeBase64Mime(String encoded)