Here you can find the source of base64Decode(final String s)
Parameter | Description |
---|---|
s | a parameter |
public static byte[] base64Decode(final String s)
//package com.java2s; public class Main { private static final char CH_EQUAL = '='; private static final int BLENGTH = 128; private static final int BYTE4 = 4; private static final byte[] BASE64ALP = new byte[BLENGTH]; /**// w ww. j a va2s . co m * * @param s * @return */ public static byte[] base64Decode(final String s) { return base64Decode(s.toCharArray()); } /** * * @param sEnc * @return */ private static byte[] base64Decode(final char[] charArr) { int len = removeWhiteSpace(charArr); if (len % BYTE4 != 0) { return null; } int numberQuadruple = (len / BYTE4); if (numberQuadruple == 0) { return new byte[0]; } byte decodedData[] = null; int i = 0; int encodedIndex = 0; int dataIndex = 0; decodedData = new byte[(numberQuadruple) * 3]; byte b1 = 0, b2 = 0, b3 = 0, b4 = 0; char d1 = 0, d2 = 0, d3 = 0, d4 = 0; for (; i < numberQuadruple - 1; i++) { if (!isData((d1 = charArr[dataIndex++])) || !isData((d2 = charArr[dataIndex++])) || !isData((d3 = charArr[dataIndex++])) || !isData((d4 = charArr[dataIndex++]))) { return null; } b1 = BASE64ALP[d1]; b2 = BASE64ALP[d2]; b3 = BASE64ALP[d3]; b4 = BASE64ALP[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); } if (!isData((d1 = charArr[dataIndex++])) || !isData((d2 = charArr[dataIndex++]))) { return null; } b1 = BASE64ALP[d1]; b2 = BASE64ALP[d2]; d3 = charArr[dataIndex++]; d4 = charArr[dataIndex++]; if (!isData((d3)) || !isData((d4))) { if (isPad(d3) && isPad(d4)) { if ((b2 & 0xf) != 0) { return null; } byte[] tmp = new byte[i * 3 + 1]; System.arraycopy(decodedData, 0, tmp, 0, i * 3); tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); return tmp; } else if (!isPad(d3) && isPad(d4)) { b3 = BASE64ALP[d3]; if ((b3 & 0x3) != 0) { return null; } byte[] tmp = new byte[i * 3 + 2]; System.arraycopy(decodedData, 0, tmp, 0, i * 3); tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); return tmp; } else { return null; } } else { b3 = BASE64ALP[d3]; b4 = BASE64ALP[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); } return decodedData; } /** * * @param chars * @return */ private static int removeWhiteSpace(final char[] chars) { if (chars == null) { return 0; } int newSize = 0; final int len = chars.length; for (int i = 0; i < len; i++) { if (!isWhiteSpace(chars[i])) { chars[newSize++] = chars[i]; } } return newSize; } /** * * @param octect * @return */ private static final boolean isData(final char octect) { return (octect < BLENGTH && BASE64ALP[octect] != -1); } /** * * @param octect * @return */ public static final boolean isPad(final char octect) { return (octect == CH_EQUAL); } /** * * @param octect * @return */ public static boolean isWhiteSpace(final char octect) { return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); } /** * * @param ch * @param start * @param length * @return */ public static boolean isWhiteSpace(final char ch[], final int start, final int length) { int end = start + length; for (int s = start; s < end; s++) { if (!isWhiteSpace(ch[s])) { return false; } } return true; } }