public class MimeBodyPart extends BodyPart implements MimePart
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.
Modifier and Type | Field and Description |
---|---|
protected java.lang.Object |
cachedContent
Cached return value from
getContent() . |
protected byte[] |
content
The bytes of the content of this part, if the part can be stored in
memory.
|
protected java.io.InputStream |
contentStream
A SharedInputStream containing the bytes of this part, if it cannot be
stored in memory.
|
protected javax.activation.DataHandler |
dh
The data handler managing this part's content.
|
protected InternetHeaders |
headers
The headers of this body part.
|
ATTACHMENT, INLINE
Constructor and Description |
---|
MimeBodyPart()
Constructor for an empty MIME body part.
|
MimeBodyPart(java.io.InputStream is)
Constructor with an input stream.
|
MimeBodyPart(InternetHeaders headers,
byte[] content)
Constructor with headers and byte content.
|
Modifier and Type | Method and Description |
---|---|
void |
addHeader(java.lang.String name,
java.lang.String value)
Adds the specified header.
|
void |
addHeaderLine(java.lang.String line)
Adds an RFC 822 header-line to this part.
|
void |
attachFile(java.io.File file)
Use the specified file as the content for this part.
|
void |
attachFile(java.io.File file,
java.lang.String contentType,
java.lang.String encoding)
Use the specified file as the content for this part, with the specified
content type and encoding.
|
void |
attachFile(java.lang.String file)
Use the specified file as the content for this part.
|
void |
attachFile(java.lang.String file,
java.lang.String contentType,
java.lang.String encoding)
Use the specified file as the content for this part, with the specified
content type and encoding.
|
java.util.Enumeration |
getAllHeaderLines()
Returns all the header-lines.
|
java.util.Enumeration |
getAllHeaders()
Returns all the headers.
|
java.lang.Object |
getContent()
Returns this part's content as a Java object.
|
java.lang.String |
getContentID()
Returns the value of the Content-ID header field.
|
java.lang.String[] |
getContentLanguage()
Returns the languages specified in the Content-Language header of this
part, as defined by RFC 1766.
|
java.lang.String |
getContentMD5()
Returns the value of the Content-MD5 header field.
|
protected java.io.InputStream |
getContentStream()
Returns the unencoded bytes of the content.
|
java.lang.String |
getContentType()
Returns the value of the RFC 822 Content-Type header field, or
"text/plain" if the header is not available.
|
javax.activation.DataHandler |
getDataHandler()
Returns a data handler for accessing this part's content.
|
java.lang.String |
getDescription()
Returns the Content-Description header field of this part.
|
java.lang.String |
getDisposition()
Returns the value of the RFC 822 Content-Disposition header field, or
null if the header is not available. |
java.lang.String |
getEncoding()
Returns the value of the Content-Transfer-Encoding header field.
|
java.lang.String |
getFileName()
Returns the filename associated with this body part.
|
java.lang.String[] |
getHeader(java.lang.String name)
Returns all the values for the specified header name.
|
java.lang.String |
getHeader(java.lang.String name,
java.lang.String delimiter)
Returns all the values for the specified header name as a single
string, with headers separated by the given delimiter.
|
java.io.InputStream |
getInputStream()
Returns a decoded input stream for this part's content.
|
int |
getLineCount()
Returns the number of lines in the content of this body part, or -1 if
this number cannot be determined.
|
java.util.Enumeration |
getMatchingHeaderLines(java.lang.String[] names)
Returns all the header-lines with any of the given names.
|
java.util.Enumeration |
getMatchingHeaders(java.lang.String[] names)
Returns all the headers with any of the given names.
|
java.util.Enumeration |
getNonMatchingHeaderLines(java.lang.String[] names)
Returns all the header-lines without any of the given names.
|
java.util.Enumeration |
getNonMatchingHeaders(java.lang.String[] names)
Returns all the headers without any of the given names.
|
java.io.InputStream |
getRawInputStream()
Returns the unencoded bytes of the content without applying any
content transfer decoding.
|
int |
getSize()
Returns the size of the content of this body part in bytes, or -1 if
the size cannot be determined.
|
boolean |
isMimeType(java.lang.String mimeType)
Indicates whether this part is of the specified MIME type.
|
void |
removeHeader(java.lang.String name)
Removes all headers with the specified name.
|
void |
saveFile(java.io.File file)
Saves the content of this part to the specified file.
|
void |
saveFile(java.lang.String file)
Saves the content of this part to the specified file.
|
void |
setContent(Multipart mp)
Sets the content of this part to be the specified multipart.
|
void |
setContent(java.lang.Object o,
java.lang.String type)
Sets the content of this part using the specified Java object and MIME
type.
|
void |
setContentID(java.lang.String cid)
Sets the Content-ID header field of this part.
|
void |
setContentLanguage(java.lang.String[] languages)
Sets the Content-Language header of this part.
|
void |
setContentMD5(java.lang.String md5)
Sets the Content-MD5 header field of this part.
|
void |
setDataHandler(javax.activation.DataHandler dh)
Sets the content of this part using the specified data handler.
|
void |
setDescription(java.lang.String description)
Sets the Content-Description header field for this part.
|
void |
setDescription(java.lang.String description,
java.lang.String charset)
Sets the Content-Description header field for this part.
|
void |
setDisposition(java.lang.String disposition)
Sets the Content-Disposition header field of this part.
|
void |
setFileName(java.lang.String filename)
Sets the filename associated with this body part.
|
void |
setHeader(java.lang.String name,
java.lang.String value)
Sets the specified header.
|
void |
setText(java.lang.String text)
Sets the content of this part using the specified text, and with a
MIME type of "text/plain".
|
void |
setText(java.lang.String text,
java.lang.String charset)
Sets the content of this part using the specified text, and with a
MIME type of "text/plain".
|
void |
setText(java.lang.String text,
java.lang.String charset,
java.lang.String subtype)
Sets the content of this part using the specified text, and with a
text MIME type of the specified subtype.
|
protected void |
updateHeaders()
Updates the headers of this part, based on the content.
|
void |
writeTo(java.io.OutputStream os)
Writes this body part to the specified stream in RFC 822 format.
|
protected javax.activation.DataHandler dh
protected byte[] content
protected java.io.InputStream contentStream
protected InternetHeaders headers
protected java.lang.Object cachedContent
getContent()
.
This field is cleared by getDataHandler()
.public MimeBodyPart()
public MimeBodyPart(java.io.InputStream is) throws MessagingException
is
- the input streamMessagingException
public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException
headers
- the headercontent
- the byte content of this partMessagingException
public int getSize() throws MessagingException
Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.
getSize
in interface Part
MessagingException
public int getLineCount() throws MessagingException
Note that this number may not be an exact measure, but if not -1, it will be suitable for display to the user.
getLineCount
in interface Part
MessagingException
public java.lang.String getContentType() throws MessagingException
getContentType
in interface Part
MessagingException
public boolean isMimeType(java.lang.String mimeType) throws MessagingException
If the subtype of mimeType
is the special character '*',
the subtype is ignored during the comparison.
isMimeType
in interface Part
MessagingException
public java.lang.String getDisposition() throws MessagingException
null
if the header is not available.getDisposition
in interface Part
MessagingException
public void setDisposition(java.lang.String disposition) throws MessagingException
setDisposition
in interface Part
disposition
- the disposition of this partIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.lang.String getEncoding() throws MessagingException
getEncoding
in interface MimePart
MessagingException
public java.lang.String getContentID() throws MessagingException
getContentID
in interface MimePart
MessagingException
public void setContentID(java.lang.String cid) throws MessagingException
IllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.lang.String getContentMD5() throws MessagingException
getContentMD5
in interface MimePart
MessagingException
public void setContentMD5(java.lang.String md5) throws MessagingException
setContentMD5
in interface MimePart
IllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.lang.String[] getContentLanguage() throws MessagingException
null
if
this header is not available.getContentLanguage
in interface MimePart
MessagingException
public void setContentLanguage(java.lang.String[] languages) throws MessagingException
setContentLanguage
in interface MimePart
languages
- the array of language tagsIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.lang.String getDescription() throws MessagingException
If the Content-Description field is encoded as per RFC 2047, it is decoded and converted into Unicode.
getDescription
in interface Part
MessagingException
public void setDescription(java.lang.String description) throws MessagingException
If description
contains non US-ASCII characters, it will
be encoded using the platform default charset.
setDescription
in interface Part
description
- the content descriptionIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void setDescription(java.lang.String description, java.lang.String charset) throws MessagingException
If description
contains non US-ASCII characters, it will
be encoded using the specified charset.
description
- the content descriptioncharset
- the charset used for encodingIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.lang.String getFileName() throws MessagingException
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.
getFileName
in interface Part
MessagingException
public void setFileName(java.lang.String filename) throws MessagingException
setFileName
in interface Part
filename
- the filename to associate with this partIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.io.InputStream getInputStream() throws java.io.IOException, MessagingException
getInputStream
in interface Part
java.io.IOException
- if an error occurs in the data handler layerMessagingException
protected java.io.InputStream getContentStream() throws MessagingException
MessagingException
public java.io.InputStream getRawInputStream() throws MessagingException
MessagingException
public javax.activation.DataHandler getDataHandler() throws MessagingException
getDataHandler
in interface Part
MessagingException
public java.lang.Object getContent() throws java.io.IOException, MessagingException
getContent
in interface Part
java.io.IOException
- if an error occurred in the data handler layerMessagingException
public void setDataHandler(javax.activation.DataHandler dh) throws MessagingException
setDataHandler
in interface Part
dh
- the data handler for the contentIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void setContent(java.lang.Object o, java.lang.String type) throws MessagingException
setContent
in interface Part
o
- the content objecttype
- the MIME type of the objectIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void setText(java.lang.String text) throws MessagingException
If the text contains non US-ASCII characters, it will be encoded using the platform default charset.
setText
in interface MimePart
setText
in interface Part
text
- the text contentIllegalWriteException
- if the underlying implementation
does not support modificationMessagingException
public void setText(java.lang.String text, java.lang.String charset) throws MessagingException
If the text contains non US-ASCII characters, it will be encoded using the specified charset.
setText
in interface MimePart
text
- the text contentcharset
- the charset used for any encodingMessagingException
public void setText(java.lang.String text, java.lang.String charset, java.lang.String subtype) throws MessagingException
If the text contains non US-ASCII characters, it will be encoded using the specified charset.
setText
in interface MimePart
text
- the text contentcharset
- the charset used for any encodingsubtype
- the MIME text subtype (e.g. "plain", "html")MessagingException
public void setContent(Multipart mp) throws MessagingException
setContent
in interface Part
mp
- the multipart contentIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void writeTo(java.io.OutputStream os) throws java.io.IOException, MessagingException
writeTo
in interface Part
java.io.IOException
- if an error occurs writing to the stream or in
the data handler layerMessagingException
- if an error occurs fetching the data
to be writtenpublic java.lang.String[] getHeader(java.lang.String name) throws MessagingException
getHeader
in interface Part
name
- the header nameMessagingException
public java.lang.String getHeader(java.lang.String name, java.lang.String delimiter) throws MessagingException
null
, only the first header is
returned.getHeader
in interface MimePart
name
- the header namedelimiter
- the delimiterMessagingException
public void setHeader(java.lang.String name, java.lang.String value) throws MessagingException
setHeader
in interface Part
name
- the header namevalue
- the header valueIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void addHeader(java.lang.String name, java.lang.String value) throws MessagingException
addHeader
in interface Part
name
- the header namevalue
- the header valueIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void removeHeader(java.lang.String name) throws MessagingException
removeHeader
in interface Part
name
- the header nameIllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.util.Enumeration getAllHeaders() throws MessagingException
getAllHeaders
in interface Part
MessagingException
public java.util.Enumeration getMatchingHeaders(java.lang.String[] names) throws MessagingException
getMatchingHeaders
in interface Part
names
- the header names to matchMessagingException
public java.util.Enumeration getNonMatchingHeaders(java.lang.String[] names) throws MessagingException
getNonMatchingHeaders
in interface Part
names
- the header names to ignoreMessagingException
public void addHeaderLine(java.lang.String line) throws MessagingException
addHeaderLine
in interface MimePart
IllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public java.util.Enumeration getAllHeaderLines() throws MessagingException
getAllHeaderLines
in interface MimePart
MessagingException
public java.util.Enumeration getMatchingHeaderLines(java.lang.String[] names) throws MessagingException
getMatchingHeaderLines
in interface MimePart
MessagingException
public java.util.Enumeration getNonMatchingHeaderLines(java.lang.String[] names) throws MessagingException
getNonMatchingHeaderLines
in interface MimePart
MessagingException
protected void updateHeaders() throws MessagingException
IllegalWriteException
- if the underlying implementation
does not support modificationjava.lang.IllegalStateException
- if this body part is obtained
from a READ_ONLY folderMessagingException
public void attachFile(java.io.File file) throws java.io.IOException, MessagingException
file
- the filejava.io.IOException
MessagingException
public void attachFile(java.io.File file, java.lang.String contentType, java.lang.String encoding) throws java.io.IOException, MessagingException
file
- the filecontentType
- the Content-Typeencoding
- the Content-Transfer-Encodingjava.io.IOException
MessagingException
public void attachFile(java.lang.String file) throws java.io.IOException, MessagingException
file
- the filejava.io.IOException
MessagingException
public void attachFile(java.lang.String file, java.lang.String contentType, java.lang.String encoding) throws java.io.IOException, MessagingException
file
- the filecontentType
- the Content-Typeencoding
- the Content-Transfer-Encodingjava.io.IOException
MessagingException
public void saveFile(java.io.File file) throws java.io.IOException, MessagingException
file
- the filejava.io.IOException
MessagingException
public void saveFile(java.lang.String file) throws java.io.IOException, MessagingException
file
- the filejava.io.IOException
MessagingException