private static byte[] base64Alphabet = new byte[BASELENGTH];
private static byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH];
// Populating the lookup and character arrays
static {
for (int i = 0; i < BASELENGTH; i++) {
base64Alphabet[i] = (byte) -1;
}
for (int i = ‘Z‘; i >= ‘A‘; i--) {
base64Alphabet[i] = (byte) (i - ‘A‘);
}
for (int i = ‘z‘; i >= ‘a‘; i--) {
base64Alphabet[i] = (byte) (i - ‘a‘ + 26);
}
for (int i = ‘9‘; i >= ‘0‘; i--) {
base64Alphabet[i] = (byte) (i - ‘0‘ + 52);
}
/**
* Encodes binary data using the base64 algorithm, optionally chunking the output into 76 character blocks.
*
* @param binaryData Array containing binary data to encode.
* @return Base64-encoded data.
*/
public static byte[] encodeBase64(byte[] binaryData) {
int lengthDataBits = binaryData.length * 8;
int fewerThan24bits = lengthDataBits % 24;
int numberTriplets = lengthDataBits / 24;
byte encodedData[] = null;
int encodedDataLength = 0;
if (fewerThan24bits != 0) {
//data not divisible by 24 bit
encodedDataLength = (numberTriplets + 1) * 4;
} else {
// 16 or 8 bit
encodedDataLength = numberTriplets * 4;
}
encodedData = new byte[encodedDataLength];
byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
int encodedIndex = 0;
int dataIndex = 0;
int i = 0;
//log.debug("number of triplets = " + numberTriplets);
for (i = 0; i < numberTriplets; i++) {
dataIndex = i * 3;
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
b3 = binaryData[dataIndex + 2];