|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||||||||||||||||||
JavaTM Platform, Enterprise Edition, v 5.0
javax.mail.internet
|
Field Summary | |
---|---|
static int |
ALL
|
Method Summary | |
---|---|
static InputStream |
decode(InputStream is,
String encoding)
Decode the given input stream. |
static String |
decodeText(String etext)
Decode "unstructured" headers, that is, headers that are defined as '*text' as per RFC 822. |
static String |
decodeWord(String eword)
The string is parsed using the rules in RFC 2047 for parsing an "encoded-word". |
static OutputStream |
encode(OutputStream os,
String encoding)
Wrap an encoder around the given output stream. |
static OutputStream |
encode(OutputStream os,
String encoding,
String filename)
Wrap an encoder around the given output stream. |
static String |
encodeText(String text)
Encode a RFC 822 "text" token into mail-safe form as per RFC 2047. |
static String |
encodeText(String text,
String charset,
String encoding)
Encode a RFC 822 "text" token into mail-safe form as per RFC 2047. |
static String |
encodeWord(String word)
Encode a RFC 822 "word" token into mail-safe form as per RFC 2047. |
static String |
encodeWord(String word,
String charset,
String encoding)
Encode a RFC 822 "word" token into mail-safe form as per RFC 2047. |
static String |
fold(int used,
String s)
Fold a string at linear whitespace so that each line is no longer than 76 characters, if possible. |
static String |
getDefaultJavaCharset()
Get the default charset corresponding to the system's current default locale. |
static String |
getEncoding(DataHandler dh)
Same as getEncoding(DataSource) except that instead
of reading the data from an InputStream it uses the
writeTo method to examine the data. |
static String |
getEncoding(DataSource ds)
Get the content-transfer-encoding that should be applied to the input stream of this datasource, to make it mailsafe. |
static String |
javaCharset(String charset)
Convert a MIME charset name into a valid Java charset name. |
static String |
mimeCharset(String charset)
Convert a java charset into its MIME charset name. |
static String |
quote(String word,
String specials)
A utility method to quote a word, if the word contains any characters from the specified 'specials' list. |
static String |
unfold(String s)
Unfold a folded header. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int ALL
Method Detail |
---|
public static String getEncoding(DataSource ds)
The algorithm used here is:
ds
- DataSource
public static String getEncoding(DataHandler dh)
getEncoding(DataSource)
except that instead
of reading the data from an InputStream
it uses the
writeTo
method to examine the data. This is more
efficient in the common case of a DataHandler
created with an object and a MIME type (for example, a
"text/plain" String) because all the I/O is done in this
thread. In the case requiring an InputStream
the
DataHandler
uses a thread, a pair of pipe streams,
and the writeTo
method to produce the data.
public static InputStream decode(InputStream is, String encoding) throws MessagingException
is
- input streamencoding
- the encoding of the stream.
MessagingException
public static OutputStream encode(OutputStream os, String encoding) throws MessagingException
os
- output streamencoding
- the encoding of the stream.
MessagingException
public static OutputStream encode(OutputStream os, String encoding, String filename) throws MessagingException
filename
parameter is used with the "uuencode"
encoding and is included in the encoded output.
os
- output streamencoding
- the encoding of the stream.filename
- name for the file being encoded (only used
with uuencode)
MessagingException
public static String encodeText(String text) throws UnsupportedEncodingException
The given Unicode string is examined for non US-ASCII characters. If the string contains only US-ASCII characters, it is returned as-is. If the string contains non US-ASCII characters, it is first character-encoded using the platform's default charset, then transfer-encoded using either the B or Q encoding. The resulting bytes are then returned as a Unicode string containing only ASCII characters.
Note that this method should be used to encode only "unstructured" RFC 822 headers.
Example of usage:
MimePart part = ... String rawvalue = "FooBar Mailer, Japanese version 1.1" try { // If we know for sure that rawvalue contains only US-ASCII // characters, we can skip the encoding part part.setHeader("X-mailer", MimeUtility.encodeText(rawvalue)); } catch (UnsupportedEncodingException e) { // encoding failure } catch (MessagingException me) { // setHeader() failure }
text
- Unicode string
UnsupportedEncodingException
- if the encoding failspublic static String encodeText(String text, String charset, String encoding) throws UnsupportedEncodingException
The given Unicode string is examined for non US-ASCII characters. If the string contains only US-ASCII characters, it is returned as-is. If the string contains non US-ASCII characters, it is first character-encoded using the specified charset, then transfer-encoded using either the B or Q encoding. The resulting bytes are then returned as a Unicode string containing only ASCII characters.
Note that this method should be used to encode only "unstructured" RFC 822 headers.
text
- the header valuecharset
- the charset. If this parameter is null, the
platform's default chatset is used.encoding
- the encoding to be used. Currently supported
values are "B" and "Q". If this parameter is null, then
the "Q" encoding is used if most of characters to be
encoded are in the ASCII charset, otherwise "B" encoding
is used.
UnsupportedEncodingException
public static String decodeText(String etext) throws UnsupportedEncodingException
The string is decoded using the algorithm specified in RFC 2047, Section 6.1.1. If the charset-conversion fails for any sequence, an UnsupportedEncodingException is thrown. If the String is not an RFC 2047 style encoded header, it is returned as-is
Example of usage:
MimePart part = ... String rawvalue = null; String value = null; try { if ((rawvalue = part.getHeader("X-mailer")[0]) != null) value = MimeUtility.decodeText(rawvalue); } catch (UnsupportedEncodingException e) { // Don't care value = rawvalue; } catch (MessagingException me) { } return value;
etext
- the possibly encoded value
UnsupportedEncodingException
- if the charset
conversion failed.public static String encodeWord(String word) throws UnsupportedEncodingException
The given Unicode string is examined for non US-ASCII characters. If the string contains only US-ASCII characters, it is returned as-is. If the string contains non US-ASCII characters, it is first character-encoded using the platform's default charset, then transfer-encoded using either the B or Q encoding. The resulting bytes are then returned as a Unicode string containing only ASCII characters.
This method is meant to be used when creating RFC 822 "phrases". The InternetAddress class, for example, uses this to encode it's 'phrase' component.
word
- Unicode string
UnsupportedEncodingException
- if the encoding failspublic static String encodeWord(String word, String charset, String encoding) throws UnsupportedEncodingException
The given Unicode string is examined for non US-ASCII characters. If the string contains only US-ASCII characters, it is returned as-is. If the string contains non US-ASCII characters, it is first character-encoded using the specified charset, then transfer-encoded using either the B or Q encoding. The resulting bytes are then returned as a Unicode string containing only ASCII characters.
word
- Unicode stringcharset
- the MIME charsetencoding
- the encoding to be used. Currently supported
values are "B" and "Q". If this parameter is null, then
the "Q" encoding is used if most of characters to be
encoded are in the ASCII charset, otherwise "B" encoding
is used.
UnsupportedEncodingException
- if the encoding failspublic static String decodeWord(String eword) throws ParseException, UnsupportedEncodingException
eword
- the possibly encoded value
ParseException
- if the string is not an
encoded-word as per RFC 2047.
UnsupportedEncodingException
- if the charset
conversion failed.public static String quote(String word, String specials)
The HeaderTokenizer
class defines two special
sets of delimiters - MIME and RFC 822.
This method is typically used during the generation of RFC 822 and MIME header fields.
word
- word to be quotedspecials
- the set of special characters
HeaderTokenizer.MIME
,
HeaderTokenizer.RFC822
public static String fold(int used, String s)
used
indicates how many characters have been used in
the current line; it is usually the length of the header name. Note that line breaks in the string aren't escaped; they probably should be.
used
- characters used in line so fars
- the string to fold
public static String unfold(String s)
s
- the string to unfold
public static String javaCharset(String charset)
charset
- the MIME charset name
public static String mimeCharset(String charset)
Note that a future version of JDK (post 1.2) might provide this functionality, in which case, we may deprecate this method then.
charset
- the JDK charset
public static String getDefaultJavaCharset()
mail.mime.charset
is set, a system charset corresponding to this MIME charset will be
returned.