一、前言
关于http-2.2.1.jar源码包中sun.net.httpserver.Base64实现类,主要实现基于base64算法字节数组转字符串byteArrayToBase64/byteArrayToBase64方法实现,详情参见源码说明。
二、源码说明
package com.sun.net.httpserver;@b@@b@class Base64@b@{@b@ private static final char[] intToBase64 = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' };@b@ private static final char[] intToAltBase64 = { '!', '"', '#', '$', '%', '&', '\'', '(', ')', ',', '-', '.', ':', ';', '<', '>', '@', '[', ']', '^', ''', '_', '{', '|', '}', '~', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '?' };@b@ private static final byte[] base64ToInt = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };@b@ private static final byte[] altBase64ToInt = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, -1, 62, 9, 10, 11, -1, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 12, 13, 14, -1, 15, 63, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 17, -1, 18, 19, 21, 20, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 22, 23, 24, 25 };@b@@b@ static String byteArrayToBase64(byte[] paramArrayOfByte)@b@ {@b@ return byteArrayToBase64(paramArrayOfByte, false);@b@ }@b@@b@ static String byteArrayToAltBase64(byte[] paramArrayOfByte)@b@ {@b@ return byteArrayToBase64(paramArrayOfByte, true);@b@ }@b@@b@ private static String byteArrayToBase64(byte[] paramArrayOfByte, boolean paramBoolean) {@b@ int i3;@b@ int i = paramArrayOfByte.length;@b@ int j = i / 3;@b@ int k = i - 3 * j;@b@ int l = 4 * (i + 2) / 3;@b@ StringBuffer localStringBuffer = new StringBuffer(l);@b@ char[] arrayOfChar = (paramBoolean) ? intToAltBase64 : intToBase64;@b@@b@ int i1 = 0;@b@ for (int i2 = 0; i2 < j; ++i2) {@b@ i3 = paramArrayOfByte[(i1++)] & 0xFF;@b@ int i4 = paramArrayOfByte[(i1++)] & 0xFF;@b@ int i5 = paramArrayOfByte[(i1++)] & 0xFF;@b@ localStringBuffer.append(arrayOfChar[(i3 >> 2)]);@b@ localStringBuffer.append(arrayOfChar[(i3 << 4 & 0x3F | i4 >> 4)]);@b@ localStringBuffer.append(arrayOfChar[(i4 << 2 & 0x3F | i5 >> 6)]);@b@ localStringBuffer.append(arrayOfChar[(i5 & 0x3F)]);@b@ }@b@@b@ if (k != 0) {@b@ i2 = paramArrayOfByte[(i1++)] & 0xFF;@b@ localStringBuffer.append(arrayOfChar[(i2 >> 2)]);@b@ if (k == 1) {@b@ localStringBuffer.append(arrayOfChar[(i2 << 4 & 0x3F)]);@b@ localStringBuffer.append("==");@b@ }@b@ else {@b@ i3 = paramArrayOfByte[(i1++)] & 0xFF;@b@ localStringBuffer.append(arrayOfChar[(i2 << 4 & 0x3F | i3 >> 4)]);@b@ localStringBuffer.append(arrayOfChar[(i3 << 2 & 0x3F)]);@b@ localStringBuffer.append('=');@b@ }@b@@b@ }@b@@b@ return localStringBuffer.toString();@b@ }@b@@b@ static byte[] base64ToByteArray(String paramString)@b@ {@b@ return base64ToByteArray(paramString, false);@b@ }@b@@b@ static byte[] altBase64ToByteArray(String paramString)@b@ {@b@ return base64ToByteArray(paramString, true);@b@ }@b@@b@ private static byte[] base64ToByteArray(String paramString, boolean paramBoolean) {@b@ int i4;@b@ int i5;@b@ byte[] arrayOfByte1 = (paramBoolean) ? altBase64ToInt : base64ToInt;@b@ int i = paramString.length();@b@ int j = i / 4;@b@ if (4 * j != i)@b@ throw new IllegalArgumentException("String length must be a multiple of four.");@b@@b@ int k = 0;@b@ int l = j;@b@ if (i != 0) {@b@ if (paramString.charAt(i - 1) == '=') {@b@ ++k;@b@ --l;@b@ }@b@ if (paramString.charAt(i - 2) == '=')@b@ ++k;@b@ }@b@ byte[] arrayOfByte2 = new byte[3 * j - k];@b@@b@ int i1 = 0; int i2 = 0;@b@ for (int i3 = 0; i3 < l; ++i3) {@b@ i4 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ i5 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ int i6 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ int i7 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ arrayOfByte2[(i2++)] = (byte)(i4 << 2 | i5 >> 4);@b@ arrayOfByte2[(i2++)] = (byte)(i5 << 4 | i6 >> 2);@b@ arrayOfByte2[(i2++)] = (byte)(i6 << 6 | i7);@b@ }@b@@b@ if (k != 0) {@b@ i3 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ i4 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ arrayOfByte2[(i2++)] = (byte)(i3 << 2 | i4 >> 4);@b@@b@ if (k == 1) {@b@ i5 = base64toInt(paramString.charAt(i1++), arrayOfByte1);@b@ arrayOfByte2[(i2++)] = (byte)(i4 << 4 | i5 >> 2);@b@ }@b@@b@ }@b@@b@ return arrayOfByte2;@b@ }@b@@b@ private static int base64toInt(char paramChar, byte[] paramArrayOfByte)@b@ {@b@ int i = paramArrayOfByte[paramChar];@b@ if (i < 0)@b@ throw new IllegalArgumentException("Illegal character " + paramChar);@b@ return i;@b@ }@b@}