Java Base64 Decode base64decode(String str)

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

Description

basedecode

License

Open Source License

Declaration

public static String base64decode(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.UnsupportedEncodingException;

public class Main {
    static byte[] revBase64;

    public static String base64decode(String str) {
        int len = str.length();
        if ((len & 3) != 0)
            throw new IllegalArgumentException("Not Base64: " + str);
        int padstart = str.indexOf('=', len - 3);
        int padding = padstart == -1 ? 0 : len - padstart;
        byte[] utf8 = new byte[len / 4 * 3];

        for (int i = 0; i < len / 4; i++) {
            int value = (revBase64[str.charAt(4 * i)] << 18)
                    + (revBase64[str.charAt(4 * i + 1)] << 12)
                    + (revBase64[str.charAt(4 * i + 2)] << 6)
                    + revBase64[str.charAt(4 * i + 3)];
            utf8[3 * i + 0] = (byte) (value >> 16);
            utf8[3 * i + 1] = (byte) (value >> 8);
            utf8[3 * i + 2] = (byte) (value);
        }/*from w  ww .  ja v a 2s .co  m*/
        try {
            return new String(utf8, 0, utf8.length - padding, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new InternalError(ex.toString());
        }
    }
}

Related

  1. base64decode(String s)
  2. base64Decode(String str)
  3. base64Decode(String str)
  4. Base64Decode(String str)
  5. base64Decode(String str)
  6. base64Decode(String string)
  7. base64decode(String string)
  8. base64decode(String strMi)
  9. base64decode(String text)