Android String Decode decode(String base64)

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

Description

decode

Declaration

public static byte[] decode(String base64) 

Method Source Code

//package com.java2s;

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

        for (int i = base64.length() - 1; base64.charAt(i) == '='; i--) {
            pad++;//from ww  w .  j a v  a  2  s  .  c  o m
        }

        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 source)
  2. decode(String str)
  3. decode(String str)
  4. decode(String s)
  5. decode(String str)
  6. decodeYUV420SPQuarterRes(int[] rgb, byte[] yuv420sp, int width, int height)
  7. decode(String s)