Base64public class Base64 extends Object This class contains a Base64 encoder and decoder. It cannot be
instantiated. The encoder and decoder are based on RFC 1521, except that
encoder do not break up lines and the decoder will treat a line break as
as invalid characters. This done since MIDP JAD's can not have line breaks
with in an attribute. Also different RFC's use different lengths for
base64 strings such as; 76 for 1521, 64 for 1421, and no breaks for 2228.
For example to decode:
String encodedData;
byte binaryData[];
binaryData = Base64.decode(encodedData);
This will decode the String in encodedData and give you an array
of bytes in the array binaryData.
On errors, this class throws an IOException with the following detail
strings:
"Base64 string not a mulitple of 4"
"Invalid character in Base64 string"
|
Fields Summary |
---|
private static final char[] | ALPHABETThis character array provides the alphabet map from RFC1521. | private static int[] | valueDecodingDecodes a 7 bit Base64 character into its binary value. |
Constructors Summary |
---|
private Base64()prevents anyone from instantiating this class
|
Methods Summary |
---|
public static byte[] | decode(java.lang.String encoded)Converts a Base64 encoded string to a byte array.
return decode(encoded, 0, encoded.length());
| public static byte[] | decode(java.lang.String encoded, int offset, int length)Converts an embedded Base64 encoded string to a byte array.
int i;
int decodedLen;
byte[] decoded;
// the input must be a multiple of 4
if (length % 4 != 0) {
throw new IOException(
"Base64 string length is not multiple of 4");
}
// 4 chars for 3 bytes, but there may have been pad bytes
decodedLen = length / 4 * 3;
if (encoded.charAt(offset + length - 1) == '=") {
decodedLen--;
if (encoded.charAt(offset + length - 2) == '=") {
decodedLen--;
}
}
decoded = new byte [decodedLen];
for (i = 0, decodedLen = 0; i < length; i += 4, decodedLen += 3) {
decodeQuantum(encoded.charAt(offset + i),
encoded.charAt(offset + i + 1),
encoded.charAt(offset + i + 2),
encoded.charAt(offset + i + 3),
decoded, decodedLen);
}
return decoded;
| private static void | decodeQuantum(char in1, char in2, char in3, char in4, byte[] out, int outOffset)Decode 4 Base64 chars as 1, 2, or 3 bytes of data.
int a = 0, b = 0, c = 0, d = 0;
int pad = 0;
a = valueDecoding[in1 & 127];
b = valueDecoding[in2 & 127];
if (in4 == '=") {
pad++;
if (in3 == '=") {
pad++;
} else {
c = valueDecoding[in3 & 127];
}
} else {
c = valueDecoding[in3 & 127];
d = valueDecoding[in4 & 127];
}
if (a < 0 || b < 0 || c < 0 || d < 0) {
throw new IOException("Invalid character in Base64 string");
}
// the first byte is the 6 bits of a and 2 bits of b
out[outOffset] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3));
if (pad < 2) {
// the second byte is 4 bits of b and 4 bits of c
out[outOffset + 1] = (byte)(((b << 4) & 0xf0) | ((c >>> 2) & 0xf));
if (pad < 1) {
// the third byte is 2 bits of c and 4 bits of d
out[outOffset + 2] =
(byte)(((c << 6) & 0xc0) | (d & 0x3f));
}
}
| public static java.lang.String | encode(byte[] data, int offset, int length)Converts a byte array into a Base64 encoded string.
for (int i = 0; i < valueDecoding.length; i++) {
valueDecoding[i] = -1;
}
for (int i = 0; i < ALPHABET.length; i++) {
valueDecoding[ALPHABET[i]] = i;
}
int i;
int encodedLen;
char[] encoded;
// 4 chars for 3 bytes, run input up to a multiple of 3
encodedLen = (length + 2) / 3 * 4;
encoded = new char [encodedLen];
for (i = 0, encodedLen = 0; encodedLen < encoded.length;
i += 3, encodedLen += 4) {
encodeQuantum(data, offset + i, length - i, encoded, encodedLen);
}
return new String(encoded);
| private static void | encodeQuantum(byte[] in, int inOffset, int len, char[] out, int outOffset)Encodes 1, 2, or 3 bytes of data as 4 Base64 chars.
byte a = 0, b = 0, c = 0;
a = in[inOffset];
out[outOffset] = ALPHABET[(a >>> 2) & 0x3F];
if (len > 2) {
b = in[inOffset + 1];
c = in[inOffset + 2];
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) +
((b >>> 4) & 0xf)];
out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) +
((c >>> 6) & 0x3)];
out[outOffset + 3] = ALPHABET[c & 0x3F];
} else if (len > 1) {
b = in[inOffset + 1];
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) +
((b >>> 4) & 0xf)];
out[outOffset + 2] = ALPHABET[((b << 2) & 0x3c) +
((c >>> 6) & 0x3)];
out[outOffset + 3] = '=";
} else {
out[outOffset + 1] = ALPHABET[((a << 4) & 0x30) +
((b >>> 4) & 0xf)];
out[outOffset + 2] = '=";
out[outOffset + 3] = '=";
}
|
|