sun.misc
public class: BASE64Decoder [javadoc |
source]
java.lang.Object
sun.misc.CharacterDecoder
sun.misc.BASE64Decoder
This class implements a BASE64 Character decoder as specified in RFC1521.
This RFC is part of the MIME specification which is published by the
Internet Engineering Task Force (IETF). Unlike some other encoding
schemes there is nothing in this encoding that tells the decoder
where a buffer starts or stops, so to use it you will need to isolate
your encoded data into a single chunk and then feed them this decoder.
The simplest way to do that is to read all of the encoded data into a
string and then use:
byte mydata[];
BASE64Decoder base64 = new BASE64Decoder();
mydata = base64.decodeBuffer(bufferString);
This will decode the String in
bufferString and give you an array
of bytes in the array
myData.
On errors, this class throws a CEFormatException with the following detail
strings:
"BASE64Decoder: Not enough bytes for an atom."
| Field Summary |
|---|
| byte[] | decode_buffer | |
| Methods from sun.misc.CharacterDecoder: |
|---|
|
bytesPerAtom, bytesPerLine, decodeAtom, decodeBuffer, decodeBuffer, decodeBuffer, decodeBufferPrefix, decodeBufferSuffix, decodeBufferToByteBuffer, decodeBufferToByteBuffer, decodeLinePrefix, decodeLineSuffix, readFully |
| Method from sun.misc.BASE64Decoder Detail: |
protected int bytesPerAtom() {
return (4);
}
This class has 4 bytes per atom |
protected int bytesPerLine() {
return (72);
}
Any multiple of 4 will do, 72 might be common |
protected void decodeAtom(PushbackInputStream inStream,
OutputStream outStream,
int rem) throws IOException {
int i;
byte a = -1, b = -1, c = -1, d = -1;
if (rem < 2) {
throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
}
do {
i = inStream.read();
if (i == -1) {
throw new CEStreamExhausted();
}
} while (i == '\n" || i == '\r");
decode_buffer[0] = (byte) i;
i = readFully(inStream, decode_buffer, 1, rem-1);
if (i == -1) {
throw new CEStreamExhausted();
}
if (rem > 3 && decode_buffer[3] == '=") {
rem = 3;
}
if (rem > 2 && decode_buffer[2] == '=") {
rem = 2;
}
switch (rem) {
case 4:
d = pem_convert_array[decode_buffer[3] & 0xff];
// NOBREAK
case 3:
c = pem_convert_array[decode_buffer[2] & 0xff];
// NOBREAK
case 2:
b = pem_convert_array[decode_buffer[1] & 0xff];
a = pem_convert_array[decode_buffer[0] & 0xff];
break;
}
switch (rem) {
case 2:
outStream.write( (byte)(((a < < 2) & 0xfc) | ((b > > > 4) & 3)) );
break;
case 3:
outStream.write( (byte) (((a < < 2) & 0xfc) | ((b > > > 4) & 3)) );
outStream.write( (byte) (((b < < 4) & 0xf0) | ((c > > > 2) & 0xf)) );
break;
case 4:
outStream.write( (byte) (((a < < 2) & 0xfc) | ((b > > > 4) & 3)) );
outStream.write( (byte) (((b < < 4) & 0xf0) | ((c > > > 2) & 0xf)) );
outStream.write( (byte) (((c < < 6) & 0xc0) | (d & 0x3f)) );
break;
}
return;
}
Decode one BASE64 atom into 1, 2, or 3 bytes of data. |