FileDocCategorySizeDatePackage
ImapResponseParser.javaAPI DocAndroid 1.5 API12497Wed May 06 22:42:46 BST 2009com.android.email.mail.store

ImapResponseParser

public class ImapResponseParser extends Object

Fields Summary
private static boolean
DEBUG_LOG_RAW_STREAM
SimpleDateFormat
mDateTimeFormat
com.android.email.PeekableInputStream
mIn
InputStream
mActiveLiteral
Constructors Summary
public ImapResponseParser(InputStream in)


       
        if (DEBUG_LOG_RAW_STREAM && Config.LOGD && Email.DEBUG) {
            in = new LoggingInputStream(in);
        }
        this.mIn = new PeekableInputStream(in);
    
Methods Summary
private intexpect(char ch)

        int d;
        if ((d = mIn.read()) != ch) {
            if (d == -1 && Config.LOGD && Email.DEBUG) {
                Log.d(Email.LOG_TAG, "expect(): end of stream reached");
            }
            throw new IOException(String.format("Expected %04x (%c) but got %04x (%c)", (int)ch,
                    ch, d, (char)d));
        }
        return d;
    
private java.lang.StringparseAtom()

        StringBuffer sb = new StringBuffer();
        int ch;
        while (true) {
            ch = mIn.peek();
            if (ch == -1) {
                if (Config.LOGD && Email.DEBUG) {
                    Log.d(Email.LOG_TAG, "parseAtom(): end of stream reached");
                }
                throw new IOException("parseAtom(): end of stream reached");
            } else if (ch == '(" || ch == ')" || ch == '{" || ch == ' " ||
            // docs claim that flags are \ atom but atom isn't supposed to
                    // contain
                    // * and some falgs contain *
                    // ch == '%' || ch == '*' ||
                    ch == '%" ||
                    // TODO probably should not allow \ and should recognize
                    // it as a flag instead
                    // ch == '"' || ch == '\' ||
                    ch == '"" || (ch >= 0x00 && ch <= 0x1f) || ch == 0x7f) {
                if (sb.length() == 0) {
                    throw new IOException(String.format("parseAtom(): (%04x %c)", (int)ch, ch));
                }
                return sb.toString();
            } else {
                sb.append((char)mIn.read());
            }
        }
    
private booleanparseCommandContinuationRequest()

        expect('+");
        expect(' ");
        return true;
    
private com.android.email.mail.store.ImapResponseParser$ImapListparseList()

        expect('(");
        ImapList list = new ImapList();
        Object token;
        while (true) {
            token = parseToken();
            if (token == null) {
                break;
            } else if (token instanceof InputStream) {
                list.add(token);
                break;
            } else if (token.equals(")")) {
                break;
            } else {
                list.add(token);
            }
        }
        return list;
    
private java.io.InputStreamparseLiteral()
A { has been read, read the rest of the size string, the space and then notify the listener with an InputStream.

param
mListener
throws
IOException

        expect('{");
        int size = Integer.parseInt(readStringUntil('}"));
        expect('\r");
        expect('\n");
        FixedLengthInputStream fixed = new FixedLengthInputStream(mIn, size);
        return fixed;
    
private java.lang.StringparseQuoted()
A " has been read, read to the end of the quoted string and notify the listener.

param
mListener
throws
IOException

        expect('"");
        return readStringUntil('"");
    
private java.lang.StringparseTaggedResponse()

        String tag = readStringUntil(' ");
        return tag;
    
private java.lang.ObjectparseToken()

        if (mActiveLiteral != null) {
            while (mActiveLiteral.read() != -1)
                ;
            mActiveLiteral = null;
        }
        while (true) {
            int ch = mIn.peek();
            if (ch == '(") {
                return parseList();
            } else if (ch == ')") {
                expect(')");
                return ")";
            } else if (ch == '"") {
                return parseQuoted();
            } else if (ch == '{") {
                mActiveLiteral = parseLiteral();
                return mActiveLiteral;
            } else if (ch == ' ") {
                expect(' ");
            } else if (ch == '\r") {
                expect('\r");
                expect('\n");
                return null;
            } else if (ch == '\n") {
                expect('\n");
                return null;
            } else {
                return parseAtom();
            }
        }
    
private voidparseUntaggedResponse()

        expect('*");
        expect(' ");
    
public com.android.email.mail.store.ImapResponseParser$ImapResponsereadResponse()
Reads the next response available on the stream and returns an ImapResponse object that represents it.

return
throws
IOException

        ImapResponse response = new ImapResponse();
        if (mActiveLiteral != null) {
            while (mActiveLiteral.read() != -1)
                ;
            mActiveLiteral = null;
        }
        int ch = mIn.peek();
        if (ch == '*") {
            parseUntaggedResponse();
            readTokens(response);
        } else if (ch == '+") {
            response.mCommandContinuationRequested =
                    parseCommandContinuationRequest();
            readTokens(response);
        } else {
            response.mTag = parseTaggedResponse();
            readTokens(response);
        }
        if (Config.LOGD) {
            if (Email.DEBUG) {
                Log.d(Email.LOG_TAG, "<<< " + response.toString());
            }
        }
        return response;
    
private java.lang.StringreadStringUntil(char end)

        StringBuffer sb = new StringBuffer();
        int ch;
        while ((ch = mIn.read()) != -1) {
            if (ch == end) {
                return sb.toString();
            } else {
                sb.append((char)ch);
            }
        }
        if (Config.LOGD && Email.DEBUG) {
            Log.d(Email.LOG_TAG, "readQuotedString(): end of stream reached");
        }
        throw new IOException("readQuotedString(): end of stream reached");
    
public java.lang.ObjectreadToken()
Reads the next token of the response. The token can be one of: String - for NIL, QUOTED, NUMBER, ATOM. InputStream - for LITERAL. InputStream.available() returns the total length of the stream. ImapResponseList - for PARENTHESIZED LIST. Can contain any of the above elements including List.

return
The next token in the response or null if there are no more tokens.
throws
IOException

        while (true) {
            Object token = parseToken();
            if (token == null || !token.equals(")")) {
                return token;
            }
        }
    
private voidreadTokens(com.android.email.mail.store.ImapResponseParser$ImapResponse response)

        response.clear();
        Object token;
        while ((token = readToken()) != null) {
            if (response != null) {
                response.add(token);
            }
            if (mActiveLiteral != null) {
                break;
            }
        }
        response.mCompleted = token == null;