Java Base64 base64ToBytes(final String base64)

Here you can find the source of base64ToBytes(final String base64)

Description

Converts the Base64 (RFC 2045) String into a byte array.

License

Apache License

Parameter

Parameter Description
base64 The Base64 string to convert.

Return

The byte[] version.

Declaration

public static byte[] base64ToBytes(final String base64) 

Method Source Code

//package com.java2s;
/*//from  w w  w.j  a  v a2 s  . com
 * #%L
 * IOUtils.java - mongodb-async-driver - Allanbank Consulting, Inc.
 * %%
 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
 * %%
 * 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.
 * #L%
 */

public class Main {
    /**
     * The mapping from a character (ascii) value to the corresponding Base64
     * (RFC-2045) 6-bit value.
     */
    private static final byte CHAR_TO_BASE_64_BITS[] = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2,
            3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
            -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
            51 };

    /**
     * Converts the Base64 (RFC 2045) String into a byte array.
     *
     * @param base64
     *            The Base64 string to convert.
     * @return The byte[] version.
     */
    public static byte[] base64ToBytes(final String base64) {
        final int base64Length = base64.length();
        final int numGroups = base64Length / 4;
        if ((4 * numGroups) != base64Length) {
            throw new IllegalArgumentException("String length must be a multiple of four.");
        }

        int missingBytesInLastGroup = 0;
        int numFullGroups = numGroups;
        if (base64Length != 0) {
            if (base64.charAt(base64Length - 1) == '=') {
                missingBytesInLastGroup++;
                numFullGroups--;
            }
            if (base64.charAt(base64Length - 2) == '=') {
                missingBytesInLastGroup++;
            }
        }
        final byte[] result = new byte[(3 * numGroups) - missingBytesInLastGroup];

        // Translate all 4 character groups from base64 to byte array elements
        int base64Index = 0;
        int resultIndex = 0;
        for (int i = 0; i < numFullGroups; i++) {
            final int b1 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            final int b2 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            final int b3 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            final int b4 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            result[resultIndex++] = (byte) ((b1 << 2) + (b2 >> 4));
            result[resultIndex++] = (byte) ((b2 << 4) + (b3 >> 2));
            result[resultIndex++] = (byte) ((b3 << 6) + b4);
        }

        // Translate partial group, if present
        if (missingBytesInLastGroup != 0) {
            final int b1 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            final int b2 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
            result[resultIndex++] = (byte) ((b1 << 2) + (b2 >> 4));

            if (missingBytesInLastGroup == 1) {
                final int b3 = alphabetToBits(CHAR_TO_BASE_64_BITS, base64.charAt(base64Index++));
                result[resultIndex++] = (byte) ((b2 << 4) + (b3 >> 2));
            }
        }

        return result;
    }

    /**
     * Uses the provided alphabet to convert the character to a set of bits.
     *
     * @param alphabet
     *            The alphabet for the conversion.
     * @param c
     *            The character to convert.
     * @return The bits for the character from the alphabet.
     * @throws IllegalArgumentException
     *             If the character is not in the alphabet.
     */
    private static int alphabetToBits(final byte[] alphabet, final char c) {
        int v = -1;
        if (c < alphabet.length) {
            v = alphabet[c];
        }
        if (v < 0) {
            throw new IllegalArgumentException("Invalid character in the encoded string: '" + c + "'.");
        }
        return v;
    }
}

Related

  1. base64enc(byte[] in)
  2. base64Pad(String s)
  3. base64ReadChunk(InputStream in, byte[] chunk)
  4. base64ToBits(char data)
  5. base64ToByteArray(String s)
  6. base64ToBytes(String value)
  7. base64toInt(char c, byte[] alphaToInt)
  8. base64UrlFriendly(String base)
  9. base64Value(char digit)