Base64InputStreampublic class Base64InputStream extends InputStream Performs Base-64 decoding on an underlying stream. |
Fields Summary |
---|
private static Log | log | private final InputStream | s | private final ByteQueue | byteq | private boolean | done | private static byte[] | TRANSLATION |
Constructors Summary |
---|
public Base64InputStream(InputStream s)
this.s = s;
|
Methods Summary |
---|
public void | close()Closes the underlying stream.
s.close();
| private void | decodeAndEnqueue(byte[] data, int len)
int accum = 0;
accum |= data[0] << 18;
accum |= data[1] << 12;
accum |= data[2] << 6;
accum |= data[3];
byte b1 = (byte)(accum >>> 16);
byteq.enqueue(b1);
if (len > 2) {
byte b2 = (byte)((accum >>> 8) & 0xFF);
byteq.enqueue(b2);
if (len > 3) {
byte b3 = (byte)(accum & 0xFF);
byteq.enqueue(b3);
}
}
| private void | fillBuffer()Retrieve data from the underlying stream, decode it,
and put the results in the byteq.
byte[] data = new byte[4];
int pos = 0;
int i;
while (!done) {
switch (i = s.read()) {
case -1:
if (pos > 0) {
log.warn("Unexpected EOF in MIME parser, dropping "
+ pos + " sextets");
}
return;
case '=":
decodeAndEnqueue(data, pos);
done = true;
break;
default:
byte sX = TRANSLATION[i];
if (sX < 0)
continue;
data[pos++] = sX;
if (pos == data.length) {
decodeAndEnqueue(data, pos);
return;
}
break;
}
}
| public int | read()
if (byteq.count() == 0) {
fillBuffer();
if (byteq.count() == 0) {
return -1;
}
}
byte val = byteq.dequeue();
if (val >= 0)
return val;
else
return val & 0xFF;
|
|