1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > java编解码base64

java编解码base64

时间:2024-03-16 03:41:47

相关推荐

java编解码base64

在alibaba的fastjson中base64上增加的一些方法

package littlehow.base64;import java.nio.charset.Charset;import java.util.Arrays;/*** 引用阿里巴巴fastjson中的base64工具* 自己实现encode方法* @authorwh* @createtime -11-19 上午11:48:38**/public class Base64 {public static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();public static final int[] IA = new int[256];static {Arrays.fill(IA, -1);for (int i = 0, iS = CA.length; i < iS; i++)IA[CA[i]] = i;IA['='] = 0;}/*** Decodes a BASE64 encoded char array that is known to be resonably well formatted. The method is about twice as* fast as {@link #decode(char[])}. The preconditions are:<br>* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>* + Line separator must be "\r\n", as specified in RFC 2045 + The array must not contain illegal characters within* the encoded string<br>* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>** @param chars The source array. Length 0 will return an empty array. <code>null</code> will throw an exception.* @return The decoded array of bytes. May be of length 0.*/public final static byte[] decodeFast(char[] chars, int offset, int charsLen) {// Check special caseif (charsLen == 0) {return new byte[0];}int sIx = offset, eIx = offset + charsLen - 1; // Start and end index after trimming.// Trim illegal chars from startwhile (sIx < eIx && IA[chars[sIx]] < 0)sIx++;// Trim illegal chars from endwhile (eIx > 0 && IA[chars[eIx]] < 0)eIx--;// get the padding count (=) (0, 1 or 2)int pad = chars[eIx] == '=' ? (chars[eIx - 1] == '=' ? 2 : 1) : 0; // Count '=' at end.int cCnt = eIx - sIx + 1; // Content count including possible separatorsint sepCnt = charsLen > 76 ? (chars[76] == '\r' ? cCnt / 78 : 0) << 1 : 0;int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytesbyte[] bytes = new byte[len]; // Preallocate byte[] of exact length// Decode all but the last 0 - 2 bytes.int d = 0;for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {// Assemble three bytes into an int from four "valid" characters.int i = IA[chars[sIx++]] << 18 | IA[chars[sIx++]] << 12 | IA[chars[sIx++]] << 6 | IA[chars[sIx++]];// Add the bytesbytes[d++] = (byte) (i >> 16);bytes[d++] = (byte) (i >> 8);bytes[d++] = (byte) i;// If line separator, jump over it.if (sepCnt > 0 && ++cc == 19) {sIx += 2;cc = 0;}}if (d < len) {// Decode last 1-3 bytes (incl '=') into 1-3 bytesint i = 0;for (int j = 0; sIx <= eIx - pad; j++)i |= IA[chars[sIx++]] << (18 - j * 6);for (int r = 16; d < len; r -= 8)bytes[d++] = (byte) (i >> r);}return bytes;}public final static byte[] decodeFast(String chars, int offset, int charsLen) {// Check special caseif (charsLen == 0) {return new byte[0];}int sIx = offset, eIx = offset + charsLen - 1; // Start and end index after trimming.// Trim illegal chars from startwhile (sIx < eIx && IA[chars.charAt(sIx)] < 0)sIx++;// Trim illegal chars from endwhile (eIx > 0 && IA[chars.charAt(eIx)] < 0)eIx--;// get the padding count (=) (0, 1 or 2)int pad = chars.charAt(eIx) == '=' ? (chars.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.int cCnt = eIx - sIx + 1; // Content count including possible separatorsint sepCnt = charsLen > 76 ? (chars.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytesbyte[] bytes = new byte[len]; // Preallocate byte[] of exact length// Decode all but the last 0 - 2 bytes.int d = 0;for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {// Assemble three bytes into an int from four "valid" characters.int i = IA[chars.charAt(sIx++)] << 18 | IA[chars.charAt(sIx++)] << 12 | IA[chars.charAt(sIx++)] << 6 | IA[chars.charAt(sIx++)];// Add the bytesbytes[d++] = (byte) (i >> 16);bytes[d++] = (byte) (i >> 8);bytes[d++] = (byte) i;// If line separator, jump over it.if (sepCnt > 0 && ++cc == 19) {sIx += 2;cc = 0;}}if (d < len) {// Decode last 1-3 bytes (incl '=') into 1-3 bytesint i = 0;for (int j = 0; sIx <= eIx - pad; j++)i |= IA[chars.charAt(sIx++)] << (18 - j * 6);for (int r = 16; d < len; r -= 8)bytes[d++] = (byte) (i >> r);}return bytes;}/*** Decodes a BASE64 encoded string that is known to be resonably well formatted. The method is about twice as fast* as {@link #decode(String)}. The preconditions are:<br>* + The array must have a line length of 76 chars OR no line separators at all (one line).<br>* + Line separator must be "\r\n", as specified in RFC 2045 + The array must not contain illegal characters within* the encoded string<br>* + The array CAN have illegal characters at the beginning and end, those will be dealt with appropriately.<br>** @param s The source string. Length 0 will return an empty array. <code>null</code> will throw an exception.* @return The decoded array of bytes. May be of length 0.*/public final static byte[] decodeFast(String s) {// Check special caseint sLen = s.length();if (sLen == 0) {return new byte[0];}int sIx = 0, eIx = sLen - 1; // Start and end index after trimming.// Trim illegal chars from startwhile (sIx < eIx && IA[s.charAt(sIx) & 0xff] < 0)sIx++;// Trim illegal chars from endwhile (eIx > 0 && IA[s.charAt(eIx) & 0xff] < 0)eIx--;// get the padding count (=) (0, 1 or 2)int pad = s.charAt(eIx) == '=' ? (s.charAt(eIx - 1) == '=' ? 2 : 1) : 0; // Count '=' at end.int cCnt = eIx - sIx + 1; // Content count including possible separatorsint sepCnt = sLen > 76 ? (s.charAt(76) == '\r' ? cCnt / 78 : 0) << 1 : 0;int len = ((cCnt - sepCnt) * 6 >> 3) - pad; // The number of decoded bytesbyte[] dArr = new byte[len]; // Preallocate byte[] of exact length// Decode all but the last 0 - 2 bytes.int d = 0;for (int cc = 0, eLen = (len / 3) * 3; d < eLen;) {// Assemble three bytes into an int from four "valid" characters.int i = IA[s.charAt(sIx++)] << 18 | IA[s.charAt(sIx++)] << 12 | IA[s.charAt(sIx++)] << 6| IA[s.charAt(sIx++)];// Add the bytesdArr[d++] = (byte) (i >> 16);dArr[d++] = (byte) (i >> 8);dArr[d++] = (byte) i;// If line separator, jump over it.if (sepCnt > 0 && ++cc == 19) {sIx += 2;cc = 0;}}if (d < len) {// Decode last 1-3 bytes (incl '=') into 1-3 bytesint i = 0;for (int j = 0; sIx <= eIx - pad; j++)i |= IA[s.charAt(sIx++)] << (18 - j * 6);for (int r = 16; d < len; r -= 8)dArr[d++] = (byte) (i >> r);}return dArr;}// ############################# 以下不属于fast json base64 内容 ##########################public static String decode(String s){try {return new String(decodeFast(s), UTF8);} catch (Exception e) {return null;}}public static String decodeGbk(String s){try {return new String(decodeFast(s), GBK);} catch (Exception e) {return null;}}/** 编码方式 */public static final Charset UTF8 = Charset.forName("UTF-8");public static final Charset GBK = Charset.forName("GBK");/*** 按照utf-8编码字符* @param s -- 待编码字符* @return*/public static String encode(String s){try{return encode(s.getBytes(UTF8));}catch(Exception e){e.printStackTrace();return null;}}/*** 按照gbk解码字符* @param s* @return*/public static String encodeGbk(String s){try{return encode(s.getBytes(GBK));}catch(Exception e){e.printStackTrace();return null;}}/*** base编码* @param target* @return*/public static String encode(byte[] target){if(target==null||target.length==0) return null;int length = target.length;/** 判断最后补位数 */int replenish = length%3;/** 判断target长度是否为总循环次数 */int loop = length/3 + (replenish==0?0:1);byte[] ret = new byte[4 * loop];/** 下标 */int index = 0;int retIndex = 0;int one,two,three;while((--loop)>0){one = target[index++] & 0xff;two = target[index++] & 0xff;three = target[index++] & 0xff;ret[retIndex++] = (byte)CA[one>>>2&0x3f];ret[retIndex++] = (byte)CA[(one<<4|two>>>4)&0x3f];ret[retIndex++] = (byte)CA[(two<<2|three>>>6)&0x3f];ret[retIndex++] = (byte)CA[three&0x3f];}//判断最后是否还有剩余,有补等号的情况出现switch(replenish){case 0:one = target[index++] & 0xff;two = target[index++] & 0xff;three = target[index++] & 0xff;ret[retIndex++] = (byte)CA[one>>>2&0x3f];ret[retIndex++] = (byte)CA[(one<<4|two>>>4)&0x3f];ret[retIndex++] = (byte)CA[(two<<2|three>>>6)&0x3f];ret[retIndex++] = (byte)CA[three&0x3f];break;case 1://补两个=one = target[length-1] & 0xff;ret[retIndex++] = (byte)CA[one>>>2&0x3f];ret[retIndex++] = (byte)CA[one<<4&0x3f];ret[retIndex++] = (byte) '=';ret[retIndex++] = (byte) '=';break;case 2://补一个=one = target[target.length - 2] & 0xff;two = target[target.length - 1] & 0xff;ret[retIndex++] = (byte)CA[one>>>2&0x3f];ret[retIndex++] = (byte)CA[(one<<4|two>>>4)&0x3f];ret[retIndex++] = (byte)CA[two<<2 & 0x3f];ret[retIndex++] = (byte) '=';break;}return new String(ret);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。