FileDocCategorySizeDatePackage
Base64InputStream.javaAPI DocAndroid 1.5 API5480Wed May 06 22:42:46 BST 2009org.apache.james.mime4j.decoder

Base64InputStream

public class Base64InputStream extends InputStream
Performs Base-64 decoding on an underlying stream.
version
$Id: Base64InputStream.java,v 1.3 2004/11/29 13:15:47 ntherning Exp $

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 voidclose()
Closes the underlying stream.

throws
IOException on I/O errors.

        s.close();
    
private voiddecodeAndEnqueue(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 voidfillBuffer()
Retrieve data from the underlying stream, decode it, and put the results in the byteq.

throws
IOException

        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 intread()

        if (byteq.count() == 0) {
            fillBuffer();
            if (byteq.count() == 0) {
                return -1;
            }
        }

        byte val = byteq.dequeue();
        if (val >= 0)
            return val;
        else
            return val & 0xFF;