Android String Base64 Encode fromBase64(String b64Data)

Here you can find the source of fromBase64(String b64Data)

Description

Converts a Base64-encoded string to the original byte data.

License

Open Source License

Parameter

Parameter Description
b64Data a Base64-encoded string to decode.

Return

bytes decoded from a Base64 string.

Declaration

public static byte[] fromBase64(String b64Data) 

Method Source Code

//package com.java2s;
/*//from  w  w w .ja  v  a2s  .  co m
 * Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Portions copyright 2006-2009 James Murty. Please see LICENSE.txt
 * for applicable license terms and NOTICE.txt for applicable notices.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.binary.Base64;
import android.util.Log;

public class Main {
    /** Default encoding when extracting binary data from a String */
    private static final String DEFAULT_ENCODING = "UTF-8";
    private static final String TAG = "###BinaryUtils###";

    /**
     * Converts a Base64-encoded string to the original byte data.
     * 
     * @param b64Data
     *            a Base64-encoded string to decode.
     * 
     * @return bytes decoded from a Base64 string.
     */
    public static byte[] fromBase64(String b64Data) {
        byte[] decoded;
        try {
            decoded = Base64.decodeBase64(b64Data
                    .getBytes(DEFAULT_ENCODING));
        } catch (UnsupportedEncodingException uee) {
            // Shouldn't happen if the string is truly Base64 encoded.
            Log.w(TAG,
                    "Tried to Base64-decode a String with the wrong encoding: ",
                    uee);
            decoded = Base64.decodeBase64(b64Data.getBytes());
        }
        return decoded;
    }
}