Java Base64 Decode base64Decoder(char[] src, int start)

Here you can find the source of base64Decoder(char[] src, int start)

Description

base Decoder

License

Open Source License

Declaration

public final static byte[] base64Decoder(char[] src, int start) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
    final static String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    final static char pad = '=';

    public final static byte[] base64Decoder(char[] src, int start) throws IOException {
        if (src == null || src.length == 0)
            return null;
        char[] four = new char[4];
        int i = 0, l, aux;
        char c;//w  w w. ja  v a  2s  .  c o m
        boolean padded;
        ByteArrayOutputStream dst = new ByteArrayOutputStream(src.length >> 1);
        while (start < src.length) {
            i = 0;
            do {
                if (start >= src.length) {
                    if (i > 0)
                        throw new IOException("bad BASE 64 In->");
                    else
                        return dst.toByteArray();
                }
                c = src[start++];
                if (chars.indexOf(c) != -1 || c == pad)
                    four[i++] = c;
                else if (c != '\r' && c != '\n')
                    throw new IOException("bad BASE 64 In->");
            } while (i < 4);
            padded = false;
            for (i = 0; i < 4; i++) {
                if (four[i] != pad && padded)
                    throw new IOException("bad BASE 64 In->");
                else if (!padded && four[i] == pad)
                    padded = true;
            }
            if (four[3] == pad) {
                if (start < src.length)
                    throw new IOException("bad BASE 64 In->");
                l = four[2] == pad ? 1 : 2;
            } else
                l = 3;
            for (i = 0, aux = 0; i < 4; i++)
                if (four[i] != pad)
                    aux |= chars.indexOf(four[i]) << (6 * (3 - i));

            for (i = 0; i < l; i++)
                dst.write((aux >>> (8 * (2 - i))) & 0xFF);
        }
        dst.flush();
        byte[] result = dst.toByteArray();
        dst.close();
        dst = null;
        return result;
    }
}

Related

  1. base64Decode(String string)
  2. base64decode(String string)
  3. base64decode(String strMi)
  4. base64decode(String text)
  5. base64decodebyte(String txt)
  6. base64decodeString(String inputString)
  7. decode(String base64)
  8. decode(String base64Code)
  9. decodeBase64(char[] data)