Decodes the supplied base 64 encoded string into its original byte array, using the standard base 64 decoding algorithm. - Java java.lang

Java examples for java.lang:String Base64

Description

Decodes the supplied base 64 encoded string into its original byte array, using the standard base 64 decoding algorithm.

Demo Code

/**/* w  w w.j  ava  2  s.  c o  m*/
 * Copyright (c) 2000-2016 Liferay, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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.
 */
//package com.java2s;
import java.io.ByteArrayOutputStream;

public class Main {
    private static byte[] sByteLookup;

    /**
     * Decodes the supplied base 64 encoded string into its original byte array, using the standard base 64 decoding
     * algorithm.
     *
     * @param   string  The base 64 encoded string to decode
     *
     * @return  The decoded byte array
     */
    public static byte[] base64Decode(String string) {

        // Blocks of encoded data may have newline characters which
        // must be ignored
        string = string.replace("\n", "");
        string = string.replace("\r", "");

        char[] chars = string.toCharArray();
        int i = 0;
        int charsToWrite = chars.length;
        ByteArrayOutputStream buff = new ByteArrayOutputStream(chars.length);
        byte[] b = new byte[4];

        while (charsToWrite >= 4) {

            try {

                // If we can't get one complete byte, then something has gone
                // wrong
                if (((b[0] = sByteLookup[chars[i++]]) == -1)
                        || ((b[1] = sByteLookup[chars[i++]]) == -1)) {
                    throw new IllegalArgumentException(string);
                }

                buff.write((b[0] << 2) | (b[1] >>> 4));

                if ((b[2] = sByteLookup[chars[i++]]) == -1) {
                    charsToWrite -= 4;

                    break;
                }

                buff.write((b[1] << 4) | (b[2] >>> 2));

                if ((b[3] = sByteLookup[chars[i++]]) == -1) {
                    charsToWrite -= 4;

                    break;
                }

                buff.write((b[2] << 6) | b[3]);
                charsToWrite -= 4;
            }

            // If any of the byte lookups go out of bounds, this can't be a
            // valid base 64 encoding
            catch (ArrayIndexOutOfBoundsException aiobe) {
                throw new IllegalArgumentException(string);
            }
        }

        // If we have any odd characters at the end, then something has gone
        // wrong
        if (charsToWrite > 0) {
            throw new IllegalArgumentException(string);
        }

        return buff.toByteArray();
    }
}

Related Tutorials