Java Base64 Convert to fromBase64(byte[] buf, int start, int length)

Here you can find the source of fromBase64(byte[] buf, int start, int length)

Description

from Base

License

Open Source License

Declaration

static byte[] fromBase64(byte[] buf, int start, int length) 

Method Source Code

//package com.java2s;

public class Main {
    private static final byte[] b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
            .getBytes();/*from w  w w .  j  a  v  a  2 s . co  m*/

    static byte[] fromBase64(byte[] buf, int start, int length) {
        byte[] foo = new byte[length];
        int j = 0;
        for (int i = start; i < start + length; i += 4) {
            foo[j] = (byte) ((val(buf[i]) << 2) | ((val(buf[i + 1]) & 0x30) >>> 4));
            if (buf[i + 2] == (byte) '=') {
                j++;
                break;
            }
            foo[j + 1] = (byte) (((val(buf[i + 1]) & 0x0f) << 4) | ((val(buf[i + 2]) & 0x3c) >>> 2));
            if (buf[i + 3] == (byte) '=') {
                j += 2;
                break;
            }
            foo[j + 2] = (byte) (((val(buf[i + 2]) & 0x03) << 6) | (val(buf[i + 3]) & 0x3f));
            j += 3;
        }
        byte[] bar = new byte[j];
        System.arraycopy(foo, 0, bar, 0, j);
        return bar;
    }

    private static byte val(byte foo) {
        if (foo == '=')
            return 0;
        for (int j = 0; j < b64.length; j++) {
            if (foo == b64[j])
                return (byte) j;
        }
        return 0;
    }
}

Related

  1. base642byte(String base64)
  2. base64StrToStream(String base64Str)
  3. base64ToByte(String data)
  4. base64ToByteArray(String data)
  5. base64ToFile(String base64Code, String targetPath)
  6. fromBase64(final String encoded)
  7. fromBase64(final String input)
  8. fromBase64(String base64)
  9. fromBase64(String base64Text)