Package javax.mail.internet
Class MimeUtility
- java.lang.Object
-
- javax.mail.internet.MimeUtility
-
public class MimeUtility extends java.lang.ObjectThis is a utility class providing micellaneous MIME-related functionality.- Version:
- 1.4
- Author:
- Chris Burdess
-
-
Field Summary
Fields Modifier and Type Field Description static intALL
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.InputStreamdecode(java.io.InputStream is, java.lang.String encoding)Decodes the given input stream.static java.lang.StringdecodeText(java.lang.String etext)Decodes headers that are defined as '*text' in RFC 822.static java.lang.StringdecodeWord(java.lang.String text)Decodes the specified string using the RFC 2047 rules for parsing an "encoded-word".static java.io.OutputStreamencode(java.io.OutputStream os, java.lang.String encoding)Encodes the given output stream.static java.io.OutputStreamencode(java.io.OutputStream os, java.lang.String encoding, java.lang.String filename)Encodes the given output stream.static java.lang.StringencodeText(java.lang.String text)Encodes an RFC 822 "text" token into mail-safe form according to RFC 2047.static java.lang.StringencodeText(java.lang.String text, java.lang.String charset, java.lang.String encoding)Encodes an RFC 822 "text" token into mail-safe form according to RFC 2047.static java.lang.StringencodeWord(java.lang.String text)Encodes an RFC 822 "word" token into mail-safe form according to RFC 2047.static java.lang.StringencodeWord(java.lang.String text, java.lang.String charset, java.lang.String encoding)Encodes an RFC 822 "word" token into mail-safe form according to RFC 2047.static java.lang.Stringfold(int used, java.lang.String s)Folds the specified string such that each line is no longer than 76 characters, whitespace permitting.static java.lang.StringgetDefaultJavaCharset()Returns the default Java charset.static java.lang.StringgetEncoding(javax.activation.DataHandler dh)Returns the Content-Transfer-Encoding that needs to be applied to the given content in order to make it mail safe.static java.lang.StringgetEncoding(javax.activation.DataSource ds)Returns the Content-Transfer-Encoding that should be applied to the input stream of this data source, to make it mail safe.static java.lang.StringjavaCharset(java.lang.String charset)Converts a MIME charset name into a Java charset name.static java.lang.StringmimeCharset(java.lang.String charset)Converts a Java charset name into a MIME charset name.static java.lang.Stringquote(java.lang.String text, java.lang.String specials)Quotes the specified word, if it contains any characters from the given "specials" list.static java.lang.Stringunfold(java.lang.String s)Unfolds a folded header.
-
-
-
Field Detail
-
ALL
public static final int ALL
- See Also:
- Constant Field Values
-
-
Method Detail
-
getEncoding
public static java.lang.String getEncoding(javax.activation.DataSource ds)
Returns the Content-Transfer-Encoding that should be applied to the input stream of this data source, to make it mail safe.- If the primary type of this datasource is "text" and if all the bytes in its input stream are US-ASCII, then the encoding is "7bit". If more than half of the bytes are non-US-ASCII, then the encoding is "base64". If less than half of the bytes are non-US-ASCII, then the encoding is "quoted-printable".
- If the primary type of this datasource is not "text", then if all the bytes of its input stream are US-ASCII, the encoding is "7bit". If there is even one non-US-ASCII character, the encoding is "base64".
- Parameters:
ds- the data source- Returns:
- "7bit", "quoted-printable" or "base64"
-
getEncoding
public static java.lang.String getEncoding(javax.activation.DataHandler dh)
Returns the Content-Transfer-Encoding that needs to be applied to the given content in order to make it mail safe. This is the same as thegetEncoding(DataSource)method except that instead of reading the data from an input stream it uses thewriteTomethod to examine the data, which can be more efficient.
-
decode
public static java.io.InputStream decode(java.io.InputStream is, java.lang.String encoding) throws MessagingExceptionDecodes the given input stream. All the encodings defined in RFC 2045 are supported here, including "base64", "quoted-printable", "7bit", "8bit", and "binary". "uuencode" is also supported.- Parameters:
is- the input streamencoding- the encoding- Returns:
- the decoded input stream
- Throws:
MessagingException
-
encode
public static java.io.OutputStream encode(java.io.OutputStream os, java.lang.String encoding) throws MessagingExceptionEncodes the given output stream. All the encodings defined in RFC 2045 are supported here, including "base64", "quoted-printable", "7bit", "8bit" and "binary". "uuencode" is also supported.- Parameters:
os- the output streamencoding- the encoding- Returns:
- an output stream that applies the specified encoding
- Throws:
MessagingException
-
encode
public static java.io.OutputStream encode(java.io.OutputStream os, java.lang.String encoding, java.lang.String filename) throws MessagingExceptionEncodes the given output stream. All the encodings defined in RFC 2045 are supported here, including "base64", "quoted-printable", "7bit", "8bit" and "binary". "uuencode" is also supported.- Parameters:
os- the output streamencoding- the encodingfilename- the name for the file being encoded (this is only used with the uuencode encoding)- Returns:
- an output stream that applies the specified encoding
- Throws:
MessagingException
-
encodeText
public static java.lang.String encodeText(java.lang.String text) throws java.io.UnsupportedEncodingExceptionEncodes an RFC 822 "text" token into mail-safe form according to RFC 2047.- Parameters:
text- the Unicode stringUnsupportedEncodingException- if the encoding fails- Throws:
java.io.UnsupportedEncodingException
-
encodeText
public static java.lang.String encodeText(java.lang.String text, java.lang.String charset, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionEncodes an RFC 822 "text" token into mail-safe form according to RFC 2047.- Parameters:
text- the Unicode stringcharset- the charset, or null to use the platform default charsetencoding- the encoding to be used ("B" or "Q")- Throws:
java.io.UnsupportedEncodingException
-
decodeText
public static java.lang.String decodeText(java.lang.String etext) throws java.io.UnsupportedEncodingExceptionDecodes headers that are defined as '*text' in RFC 822.- Parameters:
etext- the possibly encoded value- Throws:
java.io.UnsupportedEncodingException- if the charset conversion failed
-
encodeWord
public static java.lang.String encodeWord(java.lang.String text) throws java.io.UnsupportedEncodingExceptionEncodes an RFC 822 "word" token into mail-safe form according to RFC 2047.- Parameters:
text- the Unicode string- Throws:
java.io.UnsupportedEncodingException- if the encoding fails
-
encodeWord
public static java.lang.String encodeWord(java.lang.String text, java.lang.String charset, java.lang.String encoding) throws java.io.UnsupportedEncodingExceptionEncodes an RFC 822 "word" token into mail-safe form according to RFC 2047.- Parameters:
text- the Unicode stringcharset- the charset, or null to use the platform default charsetencoding- the encoding to be used ("B" or "Q")- Throws:
java.io.UnsupportedEncodingException- if the encoding fails
-
decodeWord
public static java.lang.String decodeWord(java.lang.String text) throws ParseException, java.io.UnsupportedEncodingExceptionDecodes the specified string using the RFC 2047 rules for parsing an "encoded-word".- Parameters:
eword- the possibly encoded value- Throws:
ParseException- if the string is not an encoded-wordjava.io.UnsupportedEncodingException- if the decoding failed
-
quote
public static java.lang.String quote(java.lang.String text, java.lang.String specials)Quotes the specified word, if it contains any characters from the given "specials" list.The HeaderTokenizer class defines two "specials" lists, MIME and RFC 822.
- Parameters:
word- the word to be quotedspecials- the set of special characters
-
javaCharset
public static java.lang.String javaCharset(java.lang.String charset)
Converts a MIME charset name into a Java charset name.- Parameters:
charset- the MIME charset name
-
mimeCharset
public static java.lang.String mimeCharset(java.lang.String charset)
Converts a Java charset name into a MIME charset name.- Parameters:
charset- the Java charset name
-
getDefaultJavaCharset
public static java.lang.String getDefaultJavaCharset()
Returns the default Java charset.
-
fold
public static java.lang.String fold(int used, java.lang.String s)Folds the specified string such that each line is no longer than 76 characters, whitespace permitting.- Parameters:
used- the number of characters used in the line alreadys- the string to fold- Since:
- JavaMail 1.4
-
unfold
public static java.lang.String unfold(java.lang.String s)
Unfolds a folded header.- Parameters:
s- the header to unfold- Since:
- JavaMail 1.4
-
-