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

Argument

public class Argument extends Object
author
John Mani

Fields Summary
protected Vector
items
Constructors Summary
public Argument()
Constructor

	items = new Vector(1);
    
Methods Summary
public voidappend(com.sun.mail.iap.Argument arg)
append the given Argument to this Argument. All items from the source argument are copied into this destination argument.

	items.ensureCapacity(items.size() + arg.items.size());
	for (int i=0; i < arg.items.size(); i++)
	    items.addElement(arg.items.elementAt(i));
    
private voidastring(byte[] bytes, com.sun.mail.iap.Protocol protocol)
Write out given String as either an Atom, QuotedString or Literal

	DataOutputStream os = (DataOutputStream)protocol.getOutputStream();
	int len = bytes.length;

	// If length is greater than 1024 bytes, send as literal
	if (len > 1024) {
	    literal(bytes, protocol);
	    return;
	}

        // if 0 length, send as quoted-string
        boolean quote = len == 0 ? true: false;
	boolean escape = false;
 	
	byte b;
	for (int i = 0; i < len; i++) {
	    b = bytes[i];
	    if (b == '\0" || b == '\r" || b == '\n" || ((b & 0xff) > 0177)) {
		// NUL, CR or LF means the bytes need to be sent as literals
		literal(bytes, protocol);
		return;
	    }
	    if (b == '*" || b == '%" || b == '(" || b == ')" || b == '{" ||
		b == '"" || b == '\\" || ((b & 0xff) <= ' ")) {
		quote = true;
		if (b == '"" || b == '\\") // need to escape these characters
		    escape = true;
	    }
	}

	if (quote) // start quote
	    os.write('"");

        if (escape) {
            // already quoted
            for (int i = 0; i < len; i++) {
                b = bytes[i];
                if (b == '"" || b == '\\")
                    os.write('\\");
                os.write(b);
            }
        } else 
            os.write(bytes);
 

	if (quote) // end quote
	    os.write('"");
    
private voidliteral(byte[] b, com.sun.mail.iap.Protocol protocol)
Write out given byte[] as a literal

	startLiteral(protocol, b.length).write(b);
    
private voidliteral(java.io.ByteArrayOutputStream b, com.sun.mail.iap.Protocol protocol)
Write out given ByteArrayOutputStream as a literal.

	b.writeTo(startLiteral(protocol, b.size()));
    
private voidliteral(com.sun.mail.iap.Literal b, com.sun.mail.iap.Protocol protocol)
Write out given Literal as a literal.

	b.writeTo(startLiteral(protocol, b.size()));
    
private java.io.OutputStreamstartLiteral(com.sun.mail.iap.Protocol protocol, int size)

	DataOutputStream os = (DataOutputStream)protocol.getOutputStream();
	boolean nonSync = protocol.supportsNonSyncLiterals();

	os.write('{");
	os.writeBytes(Integer.toString(size));
	if (nonSync) // server supports non-sync literals
	    os.writeBytes("+}\r\n");
	else
	    os.writeBytes("}\r\n");
	os.flush();

	// If we are using synchronized literals, wait for the server's
	// continuation signal
	if (!nonSync) {
	    for (; ;) {
		Response r = protocol.readResponse();
		if (r.isContinuation())
		    break;
		if (r.isTagged())
		    throw new LiteralException(r);
		// XXX - throw away untagged responses;
		//	 violates IMAP spec, hope no servers do this
	    }
	}
	return os;
    
public voidwrite(com.sun.mail.iap.Protocol protocol)

	int size = items != null ? items.size() : 0;
	DataOutputStream os = (DataOutputStream)protocol.getOutputStream();

	for (int i=0; i < size; i++) {
	    if (i > 0)	// write delimiter if not the first item
		os.write(' ");

	    Object o = items.elementAt(i);
	    if (o instanceof Atom) {
		os.writeBytes(((Atom)o).string);
	    } else if (o instanceof Number) {
		os.writeBytes(((Number)o).toString());
	    } else if (o instanceof AString) {
		astring(((AString)o).bytes, protocol);
	    } else if (o instanceof byte[]) {
		literal((byte[])o, protocol);
	    } else if (o instanceof ByteArrayOutputStream) {
		literal((ByteArrayOutputStream)o, protocol);
	    } else if (o instanceof Literal) {
		literal((Literal)o, protocol);
	    } else if (o instanceof Argument) {
		os.write('("); // open parans
		((Argument)o).write(protocol);
		os.write(')"); // close parans
	    }
	}
    
public voidwriteArgument(com.sun.mail.iap.Argument c)
Write out as parenthesised list.

param
s statement

	items.addElement(c);
    
public voidwriteAtom(java.lang.String s)
Write out given string as an Atom. Note that an Atom can contain only certain US-ASCII characters. No validation is done on the characters in the string.

param
s String

	items.addElement(new Atom(s));
    
public voidwriteBytes(byte[] b)
Write out given byte[] as a Literal.

param
b byte[] to write out

	items.addElement(b);
    
public voidwriteBytes(java.io.ByteArrayOutputStream b)
Write out given ByteArrayOutputStream as a Literal.

param
b ByteArrayOutputStream to be written out.

	items.addElement(b);
    
public voidwriteBytes(com.sun.mail.iap.Literal b)
Write out given data as a literal.

param
b Literal representing data to be written out.

	items.addElement(b);
    
public voidwriteNumber(long i)
Write out number.

param
i number

	items.addElement(new Long(i));
    
public voidwriteNumber(int i)
Write out number.

param
i number

	items.addElement(new Integer(i));
    
public voidwriteString(java.lang.String s)
Write out given string as an ASTRING, depending on the type of the characters inside the string. The string should contain only ASCII characters.

XXX: Hmm .. this should really be called writeASCII()

param
s String to write out

	items.addElement(new AString(ASCIIUtility.getBytes(s)));
    
public voidwriteString(java.lang.String s, java.lang.String charset)
Convert the given string into bytes in the specified charset, and write the bytes out as an ASTRING

	if (charset == null) // convenience
	    writeString(s);
	else
	    items.addElement(new AString(s.getBytes(charset)));