FileDocCategorySizeDatePackage
Response.javaAPI DocJavaMail 1.4.313815Tue Nov 17 10:38:10 GMT 2009com.sun.mail.iap

Response

public class Response extends Object
This class represents a response obtained from the input stream of an IMAP server.
author
John Mani

Fields Summary
protected int
index
protected int
pindex
protected int
size
protected byte[]
buffer
protected int
type
protected String
tag
private static final int
increment
public static final int
TAG_MASK
public static final int
CONTINUATION
public static final int
TAGGED
public static final int
UNTAGGED
public static final int
TYPE_MASK
public static final int
OK
public static final int
NO
public static final int
BAD
public static final int
BYE
public static final int
SYNTHETIC
Constructors Summary
public Response(String s)


       
	buffer = ASCIIUtility.getBytes(s);
	size = buffer.length;
	parse();
    
public Response(Protocol p)
Read a new Response from the given Protocol

param
p the Protocol object

	// read one response into 'buffer'
	ByteArray ba = p.getResponseBuffer();
	ByteArray response = p.getInputStream().readResponse(ba);
	buffer = response.getBytes();
	size = response.getCount() - 2; // Skip the terminating CRLF

	parse();
    
public Response(Response r)
Copy constructor.

	index = r.index;
	size = r.size;
	buffer = r.buffer;
	type = r.type;
	tag = r.tag;
    
Methods Summary
public static com.sun.mail.iap.ResponsebyeResponse(java.lang.Exception ex)
Return a Response object that looks like a BYE protocol response. Include the details of the exception in the response string.

	String err = "* BYE JavaMail Exception: " + ex.toString();
	err = err.replace('\r", ' ").replace('\n", ' ");
	Response r = new Response(err);
	r.type |= SYNTHETIC;
	return r;
    
public java.lang.StringgetRest()
Return the rest of the response as a string, usually used to return the arbitrary message text after a NO response.

	skipSpaces();
	return ASCIIUtility.toString(buffer, index, size);
    
public java.lang.StringgetTag()
Return the tag, if this is a tagged statement.

return
tag of this tagged statement

	return tag;
    
public intgetType()

	return type;
    
public booleanisBAD()

	return ((type & TYPE_MASK) == BAD);
    
public booleanisBYE()

	return ((type & TYPE_MASK) == BYE);
    
public booleanisContinuation()

	return ((type & TAG_MASK) == CONTINUATION);
    
public booleanisNO()

	return ((type & TYPE_MASK) == NO);
    
public booleanisOK()

	return ((type & TYPE_MASK) == OK);
    
public booleanisSynthetic()

	return ((type & SYNTHETIC) == SYNTHETIC);
    
public booleanisTagged()

	return ((type & TAG_MASK) == TAGGED);
    
public booleanisUnTagged()

	return ((type & TAG_MASK) == UNTAGGED);
    
private voidparse()

	index = 0; // position internal index at start

	if (buffer[index] == '+") { // Continuation statement
	    type |= CONTINUATION;
	    index += 1; // Position beyond the '+'
	    return;	// return
	} else if (buffer[index] == '*") { // Untagged statement
	    type |= UNTAGGED;
	    index += 1; // Position beyond the '*'
	} else {  // Tagged statement
	    type |= TAGGED;
	    tag = readAtom();	// read the TAG, index positioned beyond tag
	}

	int mark = index; // mark
	String s = readAtom();	// updates index
	if (s == null)
	    s = "";		// avoid possible NPE
	if (s.equalsIgnoreCase("OK"))
	    type |= OK;
	else if (s.equalsIgnoreCase("NO"))
	    type |= NO;
	else if (s.equalsIgnoreCase("BAD"))
	    type |= BAD;
	else if (s.equalsIgnoreCase("BYE"))
	    type |= BYE;
	else
	    index = mark; // reset

	pindex = index;
	return;
    
private java.lang.ObjectparseString(boolean parseAtoms, boolean returnString)
Generic parsing routine that can parse out a Quoted-String, Literal or Atom and return the parsed token as a String or a ByteArray. Errors or NIL data will return null.

	byte b;

	// Skip leading spaces
	skipSpaces();
	
	b = buffer[index];
	if (b == '"") { // QuotedString
	    index++; // skip the quote
	    int start = index;
	    int copyto = index;

	    while ((b = buffer[index]) != '"") {
		if (b == '\\") // skip escaped byte
		    index++;
		if (index != copyto) { // only copy if we need to
		    // Beware: this is a destructive copy. I'm 
		    // pretty sure this is OK, but ... ;>
		    buffer[copyto] = buffer[index];
		}
		copyto++;
		index++;
	    }
	    index++; // skip past the terminating quote

	    if (returnString) 
		return ASCIIUtility.toString(buffer, start, copyto);
	    else
		return new ByteArray(buffer, start, copyto-start);
	} else if (b == '{") { // Literal
	    int start = ++index; // note the start position

	    while (buffer[index] != '}")
		index++;

	    int count = 0;
	    try {
		count = ASCIIUtility.parseInt(buffer, start, index);
	    } catch (NumberFormatException nex) { 
	   	// throw new ParsingException();
		return null;
	    }

	    start = index + 3; // skip "}\r\n"
	    index = start + count; // position index to beyond the literal

	    if (returnString) // return as String
		return ASCIIUtility.toString(buffer, start, start + count);
	    else
	    	return new ByteArray(buffer, start, count);
	} else if (parseAtoms) { // parse as an ATOM
	    int start = index;	// track this, so that we can use to
				// creating ByteArrayInputStream below.
	    String s = readAtom();
	    if (returnString)
		return s;
	    else  // *very* unlikely
		return new ByteArray(buffer, start, index);
	} else if (b == 'N" || b == 'n") { // the only valid value is 'NIL'
	    index += 3; // skip past NIL
	    return null;
	}
	return null; // Error
    
public bytepeekByte()

	if (index < size)
	    return buffer[index];
	else
	    return 0;		// XXX - how else to signal error?
    
public java.lang.StringreadAtom()
Extract an ATOM, starting at the current position. Updates the internal index to beyond the Atom.

return
an Atom

	return readAtom('\0");
    
public java.lang.StringreadAtom(char delim)
Extract an ATOM, but stop at the additional delimiter (if not NUL). Used to parse a response code inside [].

	skipSpaces();

	if (index >= size) // already at end of response
	    return null;

	/*
	 * An ATOM is any CHAR delimited by :
	 * SPACE | CTL | '(' | ')' | '{' | '%' | '*' | '"' | '\'
	 */
	byte b;
	int start = index;
	while (index < size && ((b = buffer[index]) > ' ") &&
	       b != '(" && b != ')" && b != '%" && b != '*" && 
	       b != '"" && b != '\\" && b != 0x7f &&
	       (delim == '\0" || b != delim))
	    index++;

	return ASCIIUtility.toString(buffer, start, index);
    
public java.lang.StringreadAtomString()
Extract an ASTRING, starting at the current position and return as a String. An ASTRING can be a QuotedString, a Literal or an Atom Any errors in parsing returns null ASTRING := QuotedString | Literal | Atom

return
a String

	return (String)parseString(true, true);
    
public bytereadByte()
Return the next byte from this Statement.

return
the next byte.

	if (index < size)
	    return buffer[index++];
	else
	    return 0;		// XXX - how else to signal error?
    
public com.sun.mail.iap.ByteArrayreadByteArray()
Extract a NSTRING, starting at the current position. Return it as a ByteArray. The sequence 'NIL' is returned as null NSTRING := QuotedString | Literal | "NIL"

return
a ByteArray

	/*
	 * Special case, return the data after the continuation uninterpreted.
	 * It's usually a challenge for an AUTHENTICATE command.
	 */
	if (isContinuation()) {
	    skipSpaces();
	    return new ByteArray(buffer, index, size - index);
	}
	return (ByteArray)parseString(false, false);
    
public java.io.ByteArrayInputStreamreadBytes()
Extract a NSTRING, starting at the current position. Return it as a ByteArrayInputStream. The sequence 'NIL' is returned as null NSTRING := QuotedString | Literal | "NIL"

return
a ByteArrayInputStream

	ByteArray ba = readByteArray();
	if (ba != null)
	    return ba.toByteArrayInputStream();
	else
	    return null;
    
public longreadLong()
Extract a long number, starting at the current position. Updates the internal index to beyond the number. Returns -1 if a long number was not found.

return
a long

	// Skip leading spaces
	skipSpaces();

        int start = index;
        while (index < size && Character.isDigit((char)buffer[index]))
            index++;

        if (index > start) {
	    try {
		return ASCIIUtility.parseLong(buffer, start, index);
	    } catch (NumberFormatException nex) { }
	}

	return -1;
    
public intreadNumber()
Extract an integer, starting at the current position. Updates the internal index to beyond the number. Returns -1 if a number was not found.

return
a number

	// Skip leading spaces
	skipSpaces();

        int start = index;
        while (index < size && Character.isDigit((char)buffer[index]))
            index++;

        if (index > start) {
	    try {
		return ASCIIUtility.parseInt(buffer, start, index);
	    } catch (NumberFormatException nex) { }
	}

	return -1;
    
public java.lang.StringreadString(char delim)
Read a string as an arbitrary sequence of characters, stopping at the delimiter Used to read part of a response code inside [].

	skipSpaces();

	if (index >= size) // already at end of response
	    return null;

	int start = index;
	while (index < size && buffer[index] != delim)
	    index++;

	return ASCIIUtility.toString(buffer, start, index);
    
public java.lang.StringreadString()
Extract a NSTRING, starting at the current position. Return it as a String. The sequence 'NIL' is returned as null NSTRING := QuotedString | Literal | "NIL"

return
a String

	return (String)parseString(false, true);
    
public java.lang.String[]readStringList()

	skipSpaces();

	if (buffer[index] != '(") // not what we expected
	    return null;
	index++; // skip '('

	Vector v = new Vector();
	do {
	    v.addElement(readString());
	} while (buffer[index++] != ')");

	int size = v.size();
	if (size > 0) {
	    String[] s = new String[size];
	    v.copyInto(s);
	    return s;
	} else  // empty list
	    return null;
    
public voidreset()
Reset pointer to beginning of response.

	index = pindex;
    
public voidskip(int count)

	index += count;
    
public voidskipSpaces()

	while (index < size && buffer[index] == ' ")
	    index++;
    
public voidskipToken()
Skip to the next space, for use in error recovery while parsing.

	while (index < size && buffer[index] != ' ")
	    index++;
    
public java.lang.StringtoString()

	return ASCIIUtility.toString(buffer, 0, size);