javax.mail.internet

Class MimeBodyPart

public class MimeBodyPart extends BodyPart implements MimePart

A MIME body part. Body parts are components of multipart parts.

The string representation of RFC822 and MIME header fields must contain only US-ASCII characters. Non US-ASCII characters must be encoded as per the rules in RFC 2047. This class does not enforce those rules; the caller is expected to use MimeUtility to ensure that header values are correctly encoded.

Version: 1.3

Author: Chris Burdess

Field Summary
protected byte[]content
The bytes of the content of this part, if the part can be stored in memory.
protected InputStreamcontentStream
A SharedInputStream containing the bytes of this part, if it cannot be stored in memory.
protected DataHandlerdh
The data handler managing this part's content.
protected InternetHeadersheaders
The headers of this body part.
Constructor Summary
MimeBodyPart()
Constructor for an empty MIME body part.
MimeBodyPart(InputStream is)
Constructor with an input stream.
MimeBodyPart(InternetHeaders headers, byte[] content)
Constructor with headers and byte content.
Method Summary
voidaddHeader(String name, String value)
Adds the specified header.
voidaddHeaderLine(String line)
Adds an RFC 822 header-line to this part.
EnumerationgetAllHeaderLines()
Returns all the header-lines.
EnumerationgetAllHeaders()
Returns all the headers.
ObjectgetContent()
Returns this part's content as a Java object.
StringgetContentID()
Returns the value of the Content-ID header field.
String[]getContentLanguage()
Returns the languages specified in the Content-Language header of this part, as defined by RFC 1766.
StringgetContentMD5()
Returns the value of the Content-MD5 header field.
protected InputStreamgetContentStream()
Returns the unencoded bytes of the content.
StringgetContentType()
Returns the value of the RFC 822 Content-Type header field, or "text/plain" if the header is not available.
DataHandlergetDataHandler()
Returns a data handler for accessing this part's content.
StringgetDescription()
Returns the Content-Description header field of this part.
StringgetDisposition()
Returns the value of the RFC 822 Content-Disposition header field, or null if the header is not available.
StringgetEncoding()
Returns the value of the Content-Transfer-Encoding header field.
StringgetFileName()
Returns the filename associated with this body part.
String[]getHeader(String name)
Returns all the values for the specified header name.
StringgetHeader(String name, String delimiter)
Returns all the values for the specified header name as a single string, with headers separated by the given delimiter.
InputStreamgetInputStream()
Returns a decoded input stream for this part's content.
intgetLineCount()
Returns the number of lines in the content of this body part, or -1 if this number cannot be determined.
EnumerationgetMatchingHeaderLines(String[] names)
Returns all the header-lines with any of the given names.
EnumerationgetMatchingHeaders(String[] names)
Returns all the headers with any of the given names.
EnumerationgetNonMatchingHeaderLines(String[] names)
Returns all the header-lines without any of the given names.
EnumerationgetNonMatchingHeaders(String[] names)
Returns all the headers without any of the given names.
InputStreamgetRawInputStream()
Returns the unencoded bytes of the content without applying any content transfer decoding.
intgetSize()
Returns the size of the content of this body part in bytes, or -1 if the size cannot be determined.
booleanisMimeType(String mimeType)
Indicates whether this part is of the specified MIME type.
voidremoveHeader(String name)
Removes all headers with the specified name.
voidsetContent(Object o, String type)
Sets the content of this part using the specified Java object and MIME type.
voidsetContent(Multipart mp)
Sets the content of this part to be the specified multipart.
voidsetContentID(String cid)
Sets the Content-ID header field of this part.
voidsetContentLanguage(String[] languages)
Sets the Content-Language header of this part.
voidsetContentMD5(String md5)
Sets the Content-MD5 header field of this part.
voidsetDataHandler(DataHandler dh)
Sets the content of this part using the specified data handler.
voidsetDescription(String description)
Sets the Content-Description header field for this part.
voidsetDescription(String description, String charset)
Sets the Content-Description header field for this part.
voidsetDisposition(String disposition)
Sets the Content-Disposition header field of this part.
voidsetFileName(String filename)
Sets the filename associated with this body part.
voidsetHeader(String name, String value)
Sets the specified header.
voidsetText(String text)
Sets the content of this part using the specified text, and with a MIME type of "text/plain".
voidsetText(String text, String charset)
Sets the content of this part using the specified text, and with a MIME type of "text/plain".
protected voidupdateHeaders()
Updates the headers of this part, based on the content.
voidwriteTo(OutputStream os)
Writes this body part to the specified stream in RFC 822 format.

Field Detail

content

protected byte[] content
The bytes of the content of this part, if the part can be stored in memory.

contentStream

protected InputStream contentStream
A SharedInputStream containing the bytes of this part, if it cannot be stored in memory.

dh

protected DataHandler dh
The data handler managing this part's content.

headers

protected InternetHeaders headers
The headers of this body part.

Constructor Detail

MimeBodyPart

public MimeBodyPart()
Constructor for an empty MIME body part.

MimeBodyPart

public MimeBodyPart(InputStream is)
Constructor with an input stream. The stream must be positioned at the start of a valid MIME body part and terminate at the end of that body part: the boundary string must not be included in the stream.

Parameters: is the input stream

MimeBodyPart

public MimeBodyPart(InternetHeaders headers, byte[] content)
Constructor with headers and byte content.

Parameters: headers the header content the byte content of this part

Method Detail

addHeader

public void addHeader(String name, String value)
Adds the specified header.

Parameters: name the header name value the header value

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

addHeaderLine

public void addHeaderLine(String line)
Adds an RFC 822 header-line to this part.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

getAllHeaderLines

public Enumeration getAllHeaderLines()
Returns all the header-lines.

Returns: an Enumeration of Strings

getAllHeaders

public Enumeration getAllHeaders()
Returns all the headers.

Returns: an Enumeration of Header objects

getContent

public Object getContent()
Returns this part's content as a Java object.

Throws: IOException if an error occurred in the data handler layer

getContentID

public String getContentID()
Returns the value of the Content-ID header field.

getContentLanguage

public String[] getContentLanguage()
Returns the languages specified in the Content-Language header of this part, as defined by RFC 1766. This method returns null if this header is not available.

getContentMD5

public String getContentMD5()
Returns the value of the Content-MD5 header field.

getContentStream

protected InputStream getContentStream()
Returns the unencoded bytes of the content.

getContentType

public String getContentType()
Returns the value of the RFC 822 Content-Type header field, or "text/plain" if the header is not available.

getDataHandler

public DataHandler getDataHandler()
Returns a data handler for accessing this part's content.

getDescription

public String getDescription()
Returns the Content-Description header field of this part.

If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode.

getDisposition

public String getDisposition()
Returns the value of the RFC 822 Content-Disposition header field, or null if the header is not available.

getEncoding

public String getEncoding()
Returns the value of the Content-Transfer-Encoding header field.

getFileName

public String getFileName()
Returns the filename associated with this body part.

This method returns the value of the "filename" parameter from the Content-Disposition header field. If the latter is not available, it returns the value of the "name" parameter from the Content-Type header field.

getHeader

public String[] getHeader(String name)
Returns all the values for the specified header name. Note that headers may be encoded as per RFC 2047 if they contain non-US-ASCII characters: these should be decoded.

Parameters: name the header name

getHeader

public String getHeader(String name, String delimiter)
Returns all the values for the specified header name as a single string, with headers separated by the given delimiter. If the delimiter is null, only the first header is returned.

Parameters: name the header name delimiter the delimiter

getInputStream

public InputStream getInputStream()
Returns a decoded input stream for this part's content.

Throws: IOException if an error occurs in the data handler layer

getLineCount

public int getLineCount()
Returns the number of lines in the content of this body part, or -1 if this number cannot be determined.

Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.

getMatchingHeaderLines

public Enumeration getMatchingHeaderLines(String[] names)
Returns all the header-lines with any of the given names.

Returns: an Enumeration of Strings

getMatchingHeaders

public Enumeration getMatchingHeaders(String[] names)
Returns all the headers with any of the given names.

Returns: an Enumeration of Header objects

getNonMatchingHeaderLines

public Enumeration getNonMatchingHeaderLines(String[] names)
Returns all the header-lines without any of the given names.

Returns: an Enumeration of Strings

getNonMatchingHeaders

public Enumeration getNonMatchingHeaders(String[] names)
Returns all the headers without any of the given names.

Returns: an Enumeration of Header objects

getRawInputStream

public InputStream getRawInputStream()
Returns the unencoded bytes of the content without applying any content transfer decoding.

getSize

public int getSize()
Returns the size of the content of this body part in bytes, or -1 if the size cannot be determined.

Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.

isMimeType

public boolean isMimeType(String mimeType)
Indicates whether this part is of the specified MIME type.

If the subtype of mimeType is the special character '*', the subtype is ignored during the comparison.

removeHeader

public void removeHeader(String name)
Removes all headers with the specified name.

Parameters: name the header name

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setContent

public void setContent(Object o, String type)
Sets the content of this part using the specified Java object and MIME type. Note that a data content handler for the MIME type must be installed and accept objects of the type given.

Parameters: o the content object type the MIME type of the object

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setContent

public void setContent(Multipart mp)
Sets the content of this part to be the specified multipart.

Parameters: mp the multipart content

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setContentID

public void setContentID(String cid)
Sets the Content-ID header field of this part.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

Since: JavaMail 1.3

setContentLanguage

public void setContentLanguage(String[] languages)
Sets the Content-Language header of this part.

Parameters: languages the array of language tags

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setContentMD5

public void setContentMD5(String md5)
Sets the Content-MD5 header field of this part.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setDataHandler

public void setDataHandler(DataHandler dh)
Sets the content of this part using the specified data handler.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setDescription

public void setDescription(String description)
Sets the Content-Description header field for this part.

If description contains non US-ASCII characters, it will be encoded using the platform default charset.

Parameters: description the content description

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setDescription

public void setDescription(String description, String charset)
Sets the Content-Description header field for this part.

If description contains non US-ASCII characters, it will be encoded using the specified charset.

Parameters: description the content description charset the charset used for encoding

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setDisposition

public void setDisposition(String disposition)
Sets the Content-Disposition header field of this part.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setFileName

public void setFileName(String filename)
Sets the filename associated with this body part.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setHeader

public void setHeader(String name, String value)
Sets the specified header.

Parameters: name the header name value the header value

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

setText

public void setText(String text)
Sets the content of this part using the specified text, and with a MIME type of "text/plain".

If the text contains non US-ASCII characters, it will be encoded using the platform default charset.

Parameters: text the text content

setText

public void setText(String text, String charset)
Sets the content of this part using the specified text, and with a MIME type of "text/plain".

If the text contains non US-ASCII characters, it will be encoded using the specified charset.

Parameters: text the text content charset the charset used for any encoding

updateHeaders

protected void updateHeaders()
Updates the headers of this part, based on the content.

Throws: IllegalWriteException if the underlying implementation does not support modification IllegalStateException if this body part is obtained from a READ_ONLY folder

writeTo

public void writeTo(OutputStream os)
Writes this body part to the specified stream in RFC 822 format.

Throws: IOException if an error occurs writing to the stream or in the data handler layer

©