This is a utility class providing micellaneous MIME-related functionality.
decode
public static InputStream decode(InputStream is,
String encoding)
throws MessagingException
Decodes 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.
is
- the input streamencoding
- the encoding
decodeText
public static String decodeText(String etext)
throws UnsupportedEncodingException
Decodes headers that are defined as '*text' in RFC 822.
etext
- the possibly encoded value
decodeWord
public static String decodeWord(String text)
throws ParseException,
UnsupportedEncodingException
Decodes the specified string using the RFC 2047 rules for parsing an
"encoded-word".
encode
public static OutputStream encode(OutputStream os,
String encoding)
throws MessagingException
Encodes 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.
os
- the output streamencoding
- the encoding
- an output stream that applies the specified encoding
encode
public static OutputStream encode(OutputStream os,
String encoding,
String filename)
throws MessagingException
Encodes 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.
os
- the output streamencoding
- the encodingfilename
- the name for the file being encoded (this is only used
with the uuencode encoding)
- an output stream that applies the specified encoding
encodeText
public static String encodeText(String text)
throws UnsupportedEncodingException
Encodes an RFC 822 "text" token into mail-safe form according to
RFC 2047.
text
- the Unicode string
encodeText
public static String encodeText(String text,
String charset,
String encoding)
throws UnsupportedEncodingException
Encodes an RFC 822 "text" token into mail-safe form according to
RFC 2047.
text
- the Unicode stringcharset
- the charset, or null to use the platform default charsetencoding
- the encoding to be used ("B" or "Q")
encodeWord
public static String encodeWord(String text)
throws UnsupportedEncodingException
Encodes an RFC 822 "word" token into mail-safe form according to
RFC 2047.
text
- the Unicode string
encodeWord
public static String encodeWord(String text,
String charset,
String encoding)
throws UnsupportedEncodingException
Encodes an RFC 822 "word" token into mail-safe form according to
RFC 2047.
text
- the Unicode stringcharset
- the charset, or null to use the platform default charsetencoding
- the encoding to be used ("B" or "Q")
fold
public static String fold(int used,
String s)
Folds the specified string such that each line is no longer than 76
characters, whitespace permitting.
used
- the number of characters used in the line alreadys
- the string to fold
getDefaultJavaCharset
public static String getDefaultJavaCharset()
Returns the default Java charset.
getEncoding
public static String getEncoding(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 the getEncoding(DataSource)
method
except that instead of reading the data from an input stream it uses
the writeTo
method to examine the data, which can be more
efficient.
getEncoding
public static String getEncoding(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".
- "7bit", "quoted-printable" or "base64"
javaCharset
public static String javaCharset(String charset)
Converts a MIME charset name into a Java charset name.
charset
- the MIME charset name
mimeCharset
public static String mimeCharset(String charset)
Converts a Java charset name into a MIME charset name.
charset
- the Java charset name
quote
public static String quote(String text,
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.
specials
- the set of special characters
unfold
public static String unfold(String s)
Unfolds a folded header.