quoted-printable
encoding.
*/
public static final String ENC_QUOTED_PRINTABLE = "quoted-printable";
/**
* The binary
encoding.
*/
public static final String ENC_BINARY = "binary";
/**
* The base64
encoding.
*/
public static final String ENC_BASE64 = "base64";
/**
* The 8bit
encoding.
*/
public static final String ENC_8BIT = "8bit";
/**
* The 7bit
encoding.
*/
public static final String ENC_7BIT = "7bit";
/** MIME-Version
header name (lowercase) */
public static final String MIME_HEADER_MIME_VERSION = "mime-version";
/** Content-ID
header name (lowercase) */
public static final String MIME_HEADER_CONTENT_ID = "content-id";
/** Content-Description
header name (lowercase) */
public static final String MIME_HEADER_CONTENT_DESCRIPTION = "content-description";
/**
* Content-Disposition
header name (lowercase).
* See RFC2183.
*/
public static final String MIME_HEADER_CONTENT_DISPOSITION = "content-disposition";
/**
* Content-Disposition
filename parameter (lowercase).
* See RFC2183.
*/
public static final String PARAM_FILENAME = "filename";
/**
* Content-Disposition
modification-date parameter (lowercase).
* See RFC2183.
*/
public static final String PARAM_MODIFICATION_DATE = "modification-date";
/**
* Content-Disposition
creation-date parameter (lowercase).
* See RFC2183.
*/
public static final String PARAM_CREATION_DATE = "creation-date";
/**
* Content-Disposition
read-date parameter (lowercase).
* See RFC2183.
*/
public static final String PARAM_READ_DATE = "read-date";
/**
* Content-Disposition
size parameter (lowercase).
* See RFC2183.
*/
public static final String PARAM_SIZE = "size";
/**
* Content-Langauge
header (lower case).
* See RFC4646.
*/
public static final String MIME_HEADER_LANGAUGE = "content-language";
/**
* Content-Location
header (lower case).
* See RFC2557.
*/
public static final String MIME_HEADER_LOCATION = "content-location";
/**
* Content-MD5
header (lower case).
* See RFC1864.
*/
public static final String MIME_HEADER_MD5 = "content-md5";
// used to create unique ids
private static final Random random = new Random();
// used to create unique ids
private static int counter = 0;
private MimeUtil() {
// this is an utility class to be used statically.
// this constructor protect from instantiation.
}
/**
* Returns, whether the given two MIME types are identical.
*/
public static boolean isSameMimeType(String pType1, String pType2) {
return pType1 != null && pType2 != null && pType1.equalsIgnoreCase(pType2);
}
/**
* Returns true, if the given MIME type is that of a message.
*/
public static boolean isMessage(String pMimeType) {
return pMimeType != null && pMimeType.equalsIgnoreCase("message/rfc822");
}
/**
* Return true, if the given MIME type indicates a multipart entity.
*/
public static boolean isMultipart(String pMimeType) {
return pMimeType != null && pMimeType.toLowerCase().startsWith("multipart/");
}
/**
* Returns, whether the given transfer-encoding is "base64".
*/
public static boolean isBase64Encoding(String pTransferEncoding) {
return ENC_BASE64.equalsIgnoreCase(pTransferEncoding);
}
/**
* Returns, whether the given transfer-encoding is "quoted-printable".
*/
public static boolean isQuotedPrintableEncoded(String pTransferEncoding) {
return ENC_QUOTED_PRINTABLE.equalsIgnoreCase(pTransferEncoding);
}
/**
* Parses a complex field value into a map of key/value pairs. You may * use this, for example, to parse a definition like *
* text/plain; charset=UTF-8; boundary=foobar ** The above example would return a map with the keys "", "charset", * and "boundary", and the values "text/plain", "UTF-8", and "foobar". *
* Header value will be unfolded and excess white space trimmed. *
* @param pValue The field value to parse. * @return The result map; use the key "" to retrieve the first value. */ @SuppressWarnings("fallthrough") public static Mapnull
it will be used as suffix for the message ID
* (following an at sign).
*
* The resulting string is enclosed in angle brackets (< and >);
*
* @param hostName host name to be included in the message ID or
* null
if no host name should be included.
* @return a new unique message identifier.
*/
public static String createUniqueMessageId(String hostName) {
StringBuilder sb = new StringBuilder("null
to use the default
* time zone.
* @return the formatted time string.
*/
public static String formatDate(Date date, TimeZone zone) {
DateFormat df = RFC822_DATE_FORMAT.get();
if (zone == null) {
df.setTimeZone(TimeZone.getDefault());
} else {
df.setTimeZone(zone);
}
return df.format(date);
}
/**
* Splits the specified string into a multiple-line representation with
* lines no longer than 76 characters (because the line might contain
* encoded words; see RFC
* 2047 section 2). If the string contains non-whitespace sequences
* longer than 76 characters a line break is inserted at the whitespace
* character following the sequence resulting in a line longer than 76
* characters.
*
* @param s
* string to split.
* @param usedCharacters
* number of characters already used up. Usually the number of
* characters for header field name plus colon and one space.
* @return a multiple-line representation of the given string.
*/
public static String fold(String s, int usedCharacters) {
final int maxCharacters = 76;
final int length = s.length();
if (usedCharacters + length <= maxCharacters)
return s;
StringBuilder sb = new StringBuilder();
int lastLineBreak = -usedCharacters;
int wspIdx = indexOfWsp(s, 0);
while (true) {
if (wspIdx == length) {
sb.append(s.substring(Math.max(0, lastLineBreak)));
return sb.toString();
}
int nextWspIdx = indexOfWsp(s, wspIdx + 1);
if (nextWspIdx - lastLineBreak > maxCharacters) {
sb.append(s.substring(Math.max(0, lastLineBreak), wspIdx));
sb.append("\r\n");
lastLineBreak = wspIdx;
}
wspIdx = nextWspIdx;
}
}
/**
* Unfold a multiple-line representation into a single line.
*
* @param s
* string to unfold.
* @return unfolded string.
*/
public static String unfold(String s) {
final int length = s.length();
for (int idx = 0; idx < length; idx++) {
char c = s.charAt(idx);
if (c == '\r' || c == '\n') {
return unfold0(s, idx);
}
}
return s;
}
private static String unfold0(String s, int crlfIdx) {
final int length = s.length();
StringBuilder sb = new StringBuilder(length);
if (crlfIdx > 0) {
sb.append(s.substring(0, crlfIdx));
}
for (int idx = crlfIdx + 1; idx < length; idx++) {
char c = s.charAt(idx);
if (c != '\r' && c != '\n') {
sb.append(c);
}
}
return sb.toString();
}
private static int indexOfWsp(String s, int fromIndex) {
final int len = s.length();
for (int index = fromIndex; index < len; index++) {
char c = s.charAt(index);
if (c == ' ' || c == '\t')
return index;
}
return len;
}
private static synchronized int nextCounterValue() {
return counter++;
}
private static final ThreadLocal