一、前言
关于icefaces源码包(ice-3.4.2.jar)中org.icefaces.impl.util.Base64类,对于基于base64的字符串/二进制数组进行加密encode等处理,详情参见源码说明。
二、源码说明
package org.icefaces.impl.util;@b@@b@public class Base64@b@{@b@ private static final byte[] BASE64_ALPHABET_ARRAY = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 43, 47 };@b@ private static final byte[] BASE64_FOR_URL_ALPHABET_ARRAY = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 45, 95 };@b@ private static final byte PAD = 61;@b@@b@ public static byte[] encode(byte[] bytes)@b@ {@b@ return encode(bytes, BASE64_ALPHABET_ARRAY, true);@b@ }@b@@b@ public static String encode(String string)@b@ {@b@ if (string == null)@b@ return null;@b@@b@ return new String(encode(string.getBytes()));@b@ }@b@@b@ public static byte[] encodeForURL(byte[] bytes)@b@ {@b@ return encode(bytes, BASE64_FOR_URL_ALPHABET_ARRAY, false);@b@ }@b@@b@ private static byte[] encode(byte[] bytes, byte[] alphabetArray, boolean usePadding)@b@ {@b@ byte[] _bytes;@b@ int _group;@b@ if (bytes == null)@b@ return null;@b@ if (bytes.length == 0)@b@ return bytes;@b@@b@ int _length = bytes.length;@b@ int _remainder = _length % 3;@b@@b@ if (usePadding)@b@ _bytes = new byte[(_length + 2) / 3 * 4];@b@ else {@b@ _bytes = new byte[(_length + 2) / 3 * 4 - ((_remainder != 0) ? 3 - _remainder : 0)];@b@ }@b@@b@ _length -= _remainder;@b@@b@ int _index = 0;@b@ for (int _i = 0; _i < _length; ) {@b@ _group = (bytes[(_i++)] & 0xFF) << 16 | (bytes[(_i++)] & 0xFF) << 8 | bytes[(_i++)] & 0xFF;@b@@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 18)];@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 12 & 0x3F)];@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 6 & 0x3F)];@b@ _bytes[(_index++)] = alphabetArray[(_group & 0x3F)];@b@ }@b@ switch (_remainder)@b@ {@b@ case 0:@b@ break;@b@ case 1:@b@ _group = (bytes[_i] & 0xFF) << 4;@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 6)];@b@ if (usePadding) {@b@ _bytes[(_index++)] = alphabetArray[(_group & 0x3F)];@b@ _bytes[(_index++)] = 61;@b@ _bytes[_index] = 61; break label408:@b@ }@b@ _bytes[_index] = alphabetArray[(_group & 0x3F)];@b@@b@ break;@b@ case 2:@b@ _group = ((bytes[(_i++)] & 0xFF) << 8 | bytes[_i] & 0xFF) << 2;@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 12)];@b@ _bytes[(_index++)] = alphabetArray[(_group >>> 6 & 0x3F)];@b@ if (usePadding) {@b@ _bytes[(_index++)] = alphabetArray[(_group & 0x3F)];@b@ _bytes[_index] = 61; break label408:@b@ }@b@ _bytes[_index] = alphabetArray[(_group & 0x3F)];@b@ }@b@@b@ label408: return _bytes;@b@ }@b@}