Android Base64 Decode decodes(String base64)

Here you can find the source of decodes(String base64)

Description

decodes

Declaration

public static String decodes(String base64) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   ww w .  j  av a  2s .c o m
     * @Title: decode
     * @Description:
     * @param @param base64
     * @param @return
     * @return String
     * @throws
     */
    public static String decodes(String base64) {
        return new String(decode(base64));
    }

    public static byte[] decode(String base64) {
        int pad = 0;

        for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
            pad++;
        }

        int length = (base64.length() * 6) / 8 - pad;
        byte raw[] = new byte[length];
        int rawindex = 0;

        for (int i = 0; i < base64.length(); i += 4) {
            int block = (getValue(base64.charAt(i)) << 18)
                    + (getValue(base64.charAt(i + 1)) << 12)
                    + (getValue(base64.charAt(i + 2)) << 6)
                    + getValue(base64.charAt(i + 3));

            for (int j = 0; j < 3 && rawindex + j < raw.length; j++) {
                raw[rawindex + j] = (byte) (block >> 8 * (2 - j) & 0xff);
            }

            rawindex += 3;
        }

        return raw;
    }

    protected static int getValue(char c) {
        if (c >= 'A' && c <= 'Z') {
            return c - 65;
        }

        if (c >= 'a' && c <= 'z') {
            return (c - 97) + 26;
        }

        if (c >= '0' && c <= '9') {
            return (c - 48) + 52;
        }

        if (c == '+') {
            return 62;
        }

        if (c == '/') {
            return 63;
        }

        return c != '=' ? -1 : 0;
    }
}

Related

  1. decode(String data)
  2. decode(String str)
  3. decode(String s)
  4. decode(String str)
  5. fromBase64(String data)
  6. decodeBase64(String encodedString)
  7. decoderBase64File(String base64Code, String type)
  8. decode(String data, OutputStream out)
  9. decode(Reader reader, OutputStream out)