Java Base64 Convert to fromBase64(String str)

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

Description

The inverse function of the above.

Converts a string representing the encoding of some bytes in Base-64 to their original form.

License

Open Source License

Parameter

Parameter Description
str the Base-64 encoded representation of some byte(s).

Return

the bytes represented by the str.

Declaration

public static final byte[] fromBase64(String str) 

Method Source Code

//package com.java2s;
// it under the terms of the GNU General Public License as published by

public class Main {
    /** The Constant BASE64_CHARS. */
    private static final String BASE64_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";

    /**/*  w ww .  j a va 2 s .  co m*/
     * <p>The inverse function of the above.</p>
     * 
     * <p>Converts a string representing the encoding of some bytes in Base-64
     * to their original form.</p>
     *
     * @param str the Base-64 encoded representation of some byte(s).
     * @return the bytes represented by the <code>str</code>.
     * @see #toBase64(byte[])
     */
    public static final byte[] fromBase64(String str) {
        int len = str.length();
        if (len == 0) {
            throw new NumberFormatException("Empty string");
        }
        byte[] a = new byte[len + 1];
        int i, j;
        for (i = 0; i < len; i++) {
            try {
                a[i] = (byte) BASE64_CHARS.indexOf(str.charAt(i));
            } catch (ArrayIndexOutOfBoundsException x) {
                throw new NumberFormatException("Illegal character at #" + i);
            }
        }
        i = len - 1;
        j = len;
        try {
            while (true) {
                a[j] = a[i];
                if (--i < 0) {
                    break;
                }
                a[j] |= (a[i] & 0x03) << 6;
                j--;
                a[j] = (byte) ((a[i] & 0x3C) >>> 2);
                if (--i < 0) {
                    break;
                }
                a[j] |= (a[i] & 0x0F) << 4;
                j--;
                a[j] = (byte) ((a[i] & 0x30) >>> 4);
                if (--i < 0) {
                    break;
                }
                a[j] |= (a[i] << 2);
                j--;
                a[j] = 0;
                if (--i < 0) {
                    break;
                }
            }
        } catch (Exception ignored) {
        }

        try { // ignore leading 0-bytes
            while (a[j] == 0) {
                j++;
            }
        } catch (Exception x) {
            return new byte[1]; // one 0-byte
        }
        byte[] result = new byte[len - j + 1];
        System.arraycopy(a, j, result, 0, len - j + 1);
        return result;
    }
}

Related

  1. fromBase64(final String input)
  2. fromBase64(String base64)
  3. fromBase64(String base64Text)
  4. fromBase64(String data)
  5. fromBase64(String s)
  6. fromBase64decodedImageToByte(String base64encoded)
  7. fromBase64Impl(String data)
  8. fromBase64String(String base64)
  9. fromBase64String(String encoded)