FileDocCategorySizeDatePackage
POP3Message.javaAPI DocJavaMail 1.4.316062Tue Nov 17 10:38:10 GMT 2009com.sun.mail.pop3

POP3Message

public class POP3Message extends MimeMessage
A POP3 Message. Just like a MimeMessage except that some things are not supported.
author
Bill Shannon

Fields Summary
static final String
UNKNOWN
private POP3Folder
folder
private int
hdrSize
private int
msgSize
String
uid
Constructors Summary
public POP3Message(Folder folder, int msgno)

	// controlled by folder lock

        
			  
	super(folder, msgno);
	this.folder = (POP3Folder)folder;
    
Methods Summary
public voidaddHeader(java.lang.String name, java.lang.String value)
Add this value to the existing values for this header_name. Throws IllegalWriteException because POP3 messages are read-only.

param
name header name
param
value header value
see
javax.mail.internet.MimeUtility
exception
IllegalWriteException because the underlying implementation does not support modification
exception
IllegalStateException if this message is obtained from a READ_ONLY folder.

	// XXX - should check for read-only folder?
	throw new IllegalWriteException("POP3 messages are read-only");
    
public voidaddHeaderLine(java.lang.String line)
Add a raw RFC822 header-line. Throws IllegalWriteException because POP3 messages are read-only.

exception
IllegalWriteException because the underlying implementation does not support modification
exception
IllegalStateException if this message is obtained from a READ_ONLY folder.

	// XXX - should check for read-only folder?
	throw new IllegalWriteException("POP3 messages are read-only");
    
public java.util.EnumerationgetAllHeaderLines()
Get all header lines as an Enumeration of Strings. A Header line is a raw RFC822 header-line, containing both the "name" and "value" field.

exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getAllHeaderLines();
    
public java.util.EnumerationgetAllHeaders()
Return all the headers from this Message as an enumeration of Header objects.

Note that certain headers may be encoded as per RFC 2047 if they contain non US-ASCII characters and these should be decoded.

return
array of header objects
exception
MessagingException
see
javax.mail.internet.MimeUtility

	if (headers == null)
	    loadHeaders();
	return headers.getAllHeaders();	
    
protected java.io.InputStreamgetContentStream()
Produce the raw bytes of the content. The data is fetched using the POP3 RETR command.

see
#contentStream

	try {
	synchronized(this) {
	    if (contentStream == null) {
		InputStream rawcontent = folder.getProtocol().retr(msgnum,
					msgSize > 0 ? msgSize + hdrSize : 0);
		if (rawcontent == null) {
		    expunged = true;
		    throw new MessageRemovedException();    //  XXX - what else?
		}
		if (headers == null ||
			((POP3Store)(folder.getStore())).forgetTopHeaders) {
		    headers = new InternetHeaders(rawcontent);
		    hdrSize =
			(int)((SharedInputStream)rawcontent).getPosition();
		} else {
		    /*
		     * Already have the headers, have to skip the headers
		     * in the content array and return the body.
		     *
		     * XXX - It seems that some mail servers return slightly
		     * different headers in the RETR results than were returned
		     * in the TOP results, so we can't depend on remembering
		     * the size of the headers from the TOP command and just
		     * skipping that many bytes.  Instead, we have to process
		     * the content, skipping over the header until we come to
		     * the empty line that separates the header from the body.
		     */
		    int offset = 0;
		    for (;;) {
			int len = 0;	// number of bytes in this line
			int c1;
			while ((c1 = rawcontent.read()) >= 0) {
			    if (c1 == '\n")	// end of line
				break;
			    else if (c1 == '\r") {
				// got CR, is the next char LF?
				if (rawcontent.available() > 0) {
				    rawcontent.mark(1);
				    if (rawcontent.read() != '\n")
					rawcontent.reset();
				}
				break;	// in any case, end of line
			    }

			    // not CR, NL, or CRLF, count the byte
			    len++;
			}
			// here when end of line or out of data

			// if out of data, we're done
			if (rawcontent.available() == 0)
			    break;
			
			// if it was an empty line, we're done
			if (len == 0)
			    break;
		    }
		    hdrSize =
			(int)((SharedInputStream)rawcontent).getPosition();
		}
		contentStream =
		    ((SharedInputStream)rawcontent).newStream(hdrSize, -1);
		rawcontent = null;	// help GC
	    }
	}
	} catch (EOFException eex) {
	    folder.close(false);
	    throw new FolderClosedException(folder, eex.toString());
	} catch (IOException ex) {
	    throw new MessagingException("error fetching POP3 content", ex);
	}
	return super.getContentStream();
    
public java.lang.String[]getHeader(java.lang.String name)
Get all the headers for this header_name. Note that certain headers may be encoded as per RFC 2047 if they contain non US-ASCII characters and these should be decoded.

param
name name of header
return
array of headers
exception
MessagingException
see
javax.mail.internet.MimeUtility

	if (headers == null)
	    loadHeaders();
	return headers.getHeader(name);
    
public java.lang.StringgetHeader(java.lang.String name, java.lang.String delimiter)
Get all the headers for this header name, returned as a single String, with headers separated by the delimiter. If the delimiter is null, only the first header is returned.

param
name the name of this header
param
delimiter delimiter between returned headers
return
the value fields for all headers with this name
exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getHeader(name, delimiter);
    
public java.util.EnumerationgetMatchingHeaderLines(java.lang.String[] names)
Get matching header lines as an Enumeration of Strings. A Header line is a raw RFC822 header-line, containing both the "name" and "value" field.

exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getMatchingHeaderLines(names);
    
public java.util.EnumerationgetMatchingHeaders(java.lang.String[] names)
Return matching headers from this Message as an Enumeration of Header objects.

exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getMatchingHeaders(names);
    
public java.util.EnumerationgetNonMatchingHeaderLines(java.lang.String[] names)
Get non-matching header lines as an Enumeration of Strings. A Header line is a raw RFC822 header-line, containing both the "name" and "value" field.

exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getNonMatchingHeaderLines(names);
    
public java.util.EnumerationgetNonMatchingHeaders(java.lang.String[] names)
Return non-matching headers from this Message as an Enumeration of Header objects.

exception
MessagingException

	if (headers == null)
	    loadHeaders();
	return headers.getNonMatchingHeaders(names);
    
public intgetSize()
Return the size of the content of this message in bytes. Returns -1 if the size cannot be determined.

Note that this number may not be an exact measure of the content size and may or may not account for any transfer encoding of the content.

return
size of content in bytes
exception
MessagingException

	try {
	    synchronized (this) {
		if (msgSize >= 0)
		    return msgSize;
		if (msgSize < 0) {
		    /*
		     * Use LIST to determine the entire message
		     * size and subtract out the header size
		     * (which may involve loading the headers,
		     * which may load the content as a side effect).
		     * If the content is loaded as a side effect of
		     * loading the headers, get the size directly.
		     */
		    if (headers == null)
			loadHeaders();
		    if (contentStream != null)
			msgSize = contentStream.available();
		    else
			msgSize = folder.getProtocol().list(msgnum) - hdrSize;
		}
		return msgSize;
	    }
	} catch (EOFException eex) {
	    folder.close(false);
	    throw new FolderClosedException(folder, eex.toString());
	} catch (IOException ex) {
	    throw new MessagingException("error getting size", ex);
	}
    
public synchronized voidinvalidate(boolean invalidateHeaders)
Invalidate the cache of content for this message object, causing it to be fetched again from the server the next time it is needed. If invalidateHeaders is true, invalidate the headers as well.

param
invalidateHeaders invalidate the headers as well?

	content = null;
	contentStream = null;
	msgSize = -1;
	if (invalidateHeaders) {
	    headers = null;
	    hdrSize = -1;
	}
    
private voidloadHeaders()
Load the headers for this message into the InternetHeaders object. The headers are fetched using the POP3 TOP command.

	try {
	    synchronized (this) {
		if (headers != null)    // check again under lock
		    return;
		InputStream hdrs = null;
		if (((POP3Store)(folder.getStore())).disableTop ||
			(hdrs = folder.getProtocol().top(msgnum, 0)) == null) {
		    // possibly because the TOP command isn't supported,
		    // load headers as a side effect of loading the entire
		    // content.
		    InputStream cs = getContentStream();
		    cs.close();
		} else {
		    hdrSize = hdrs.available();
		    headers = new InternetHeaders(hdrs);
		}
	    }
	} catch (EOFException eex) {
	    folder.close(false);
	    throw new FolderClosedException(folder, eex.toString());
	} catch (IOException ex) {
	    throw new MessagingException("error loading POP3 headers", ex);
	}
    
public voidremoveHeader(java.lang.String name)
Remove all headers with this name. Throws IllegalWriteException because POP3 messages are read-only.

exception
IllegalWriteException because the underlying implementation does not support modification
exception
IllegalStateException if this message is obtained from a READ_ONLY folder.

	// XXX - should check for read-only folder?
	throw new IllegalWriteException("POP3 messages are read-only");
    
public voidsaveChanges()
POP3 message can't be changed. This method throws IllegalWriteException.

exception
IllegalWriteException because the underlying implementation does not support modification

	// POP3 Messages are read-only
	throw new IllegalWriteException("POP3 messages are read-only");
    
public voidsetFlags(javax.mail.Flags newFlags, boolean set)
Set the specified flags on this message to the specified value.

param
newFlags the flags to be set
param
set the value to be set

	Flags oldFlags = (Flags)flags.clone();
	super.setFlags(newFlags, set);
	if (!flags.equals(oldFlags))
	    folder.notifyMessageChangedListeners(
				MessageChangedEvent.FLAGS_CHANGED, this);
    
public voidsetHeader(java.lang.String name, java.lang.String value)
Set the value for this header_name. Throws IllegalWriteException because POP3 messages are read-only.

param
name header name
param
value header value
see
javax.mail.internet.MimeUtility
exception
IllegalWriteException because the underlying implementation does not support modification
exception
IllegalStateException if this message is obtained from a READ_ONLY folder.

	// XXX - should check for read-only folder?
	throw new IllegalWriteException("POP3 messages are read-only");
    
public java.io.InputStreamtop(int n)
Fetch the header of the message and the first n lines of the raw content of the message. The headers and data are available in the returned InputStream.

param
n number of lines of content to fetch
return
InputStream containing the message headers and n content lines

	try {
	    synchronized (this) {
		return folder.getProtocol().top(msgnum, n);
	    }
	} catch (EOFException eex) {
	    folder.close(false);
	    throw new FolderClosedException(folder, eex.toString());
	} catch (IOException ex) {
	    throw new MessagingException("error getting size", ex);
	}