javax.mail.internet
Class InternetHeaders

java.lang.Object
  |
  +--javax.mail.internet.InternetHeaders

public class InternetHeaders
extends java.lang.Object

InternetHeaders is a utility class that manages RFC822 style headers. Given a rfc822 format message stream, it reads lines till the blank line that indicates end of header. The input stream is positioned at the start of the body. The lines are stored within the object and can be extracted as either Strings or Header objects.

This class is mostly intended for service providers. MimeMessage and MimeBody use this class for holding their headers.

Note that RFC822 header fields can contain only US-ASCII characters. Non US-ASCII characters must be encoded using the conventions specified in RFC2047; the MimeUtility class can be used for this.

See Also:
MimeUtility

Constructor Summary
InternetHeaders()
          Create an empty InternetHeaders object.
InternetHeaders(java.io.InputStream is)
          Read and parse the given rfc822 message stream till the blank line separating the header from the body.
 
Method Summary
 void addHeader(java.lang.String name, java.lang.String value)
          Add a header with the specified name and value to the header list.
 void addHeaderLine(java.lang.String line)
          Add an RFC822 header line to the header store.
 java.util.Enumeration getAllHeaderLines()
          Return all the header lines as an Enumeration of Strings.
 java.util.Enumeration getAllHeaders()
          Return all the headers as an Enumeration of Header objects
 java.lang.String[] getHeader(java.lang.String name)
          Return all the values for the specified header.
 java.lang.String getHeader(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.
 java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
          Return all matching header lines as an Enumeration of Strings.
 java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
          Return all matching Header objects
 java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
          Return all non-matching header lines
 java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
          Return all non-matching Header objects
 void load(java.io.InputStream is)
          Read and parse the given rfc822 message stream till the blank line separating the header from the body.
 void removeHeader(java.lang.String name)
          Remove all header entries that match the given name
 void setHeader(java.lang.String name, java.lang.String value)
          Change the first header line that matches name to have value, adding a new header if no existing header matches.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

InternetHeaders

public InternetHeaders()
Create an empty InternetHeaders object.

InternetHeaders

public InternetHeaders(java.io.InputStream is)
                throws MessagingException
Read and parse the given rfc822 message stream till the blank line separating the header from the body. The input stream is left positioned at the start of the body. The header lines are stored internally.

For efficiency, wrap a BufferedInputStream around the actual input stream and pass it as the parameter.

Parameters:
is - rfc822 input stream
Method Detail

load

public void load(java.io.InputStream is)
          throws MessagingException
Read and parse the given rfc822 message stream till the blank line separating the header from the body. Store the header lines inside this InternetHeaders object.

Note that the header lines are added into this InternetHeaders object, so any existing headers in this object will not be affected.

Parameters:
is - rfc822 input stream

getHeader

public java.lang.String[] getHeader(java.lang.String name)
Return all the values for the specified header. The values are String objects.
Parameters:
name - header name

getHeader

public java.lang.String getHeader(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.
Parameters:
name - header name
delimiter - delimiter
Returns:
the value fields for all headers with this name

setHeader

public void setHeader(java.lang.String name,
                      java.lang.String value)
Change the first header line that matches name to have value, adding a new header if no existing header matches. Remove all matching headers but the first.

Note that RFC822 headers can only contain US-ASCII characters

Parameters:
name - header name
value - header value

addHeader

public void addHeader(java.lang.String name,
                      java.lang.String value)
Add a header with the specified name and value to the header list.

Note that RFC822 headers can only contain US-ASCII characters.

Parameters:
name - header name
value - header value

removeHeader

public void removeHeader(java.lang.String name)
Remove all header entries that match the given name
Parameters:
name - header name

getAllHeaders

public java.util.Enumeration getAllHeaders()
Return all the headers as an Enumeration of Header objects
Returns:
Header objects

getMatchingHeaders

public java.util.Enumeration getMatchingHeaders(java.lang.String[] names)
Return all matching Header objects
Returns:
matching Header objects

getNonMatchingHeaders

public java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names)
Return all non-matching Header objects
Returns:
non-matching Header objects

addHeaderLine

public void addHeaderLine(java.lang.String line)
Add an RFC822 header line to the header store. If the line starts with a space or tab (a continuation line), add it to the last header line in the list.

Note that RFC822 headers can only contain US-ASCII characters

Parameters:
line - raw rfc822 header line

getAllHeaderLines

public java.util.Enumeration getAllHeaderLines()
Return all the header lines as an Enumeration of Strings.

getMatchingHeaderLines

public java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names)
Return all matching header lines as an Enumeration of Strings.

getNonMatchingHeaderLines

public java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names)
Return all non-matching header lines