?? mimeutility.java
字號:
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of either the GNU * General Public License Version 2 only ("GPL") or the Common Development * and Distribution License("CDDL") (collectively, the "License"). You * may not use this file except in compliance with the License. You can obtain * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. * Sun designates this particular file as subject to the "Classpath" exception * as provided by Sun in the GPL Version 2 section of the License file that * accompanied this code. If applicable, add the following below the License * Header, with the fields enclosed by brackets [] replaced by your own * identifying information: "Portions Copyrighted [year] * [name of copyright owner]" * * Contributor(s): * * If you wish your version of this file to be governed by only the CDDL or * only the GPL Version 2, indicate your decision by adding "[Contributor] * elects to include this software in this distribution under the [CDDL or GPL * Version 2] license." If you don't indicate a single choice of license, a * recipient has the option to distribute your version of this file under * either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL Version 2 code * and therefore, elected the GPL Version 2 license, then the option applies * only if the new code is made subject to such option by the copyright * holder. *//* * @(#)MimeUtility.java 1.60 07/05/15 */package javax.mail.internet;import javax.mail.MessagingException;import javax.activation.*;import java.util.*;import java.io.*;import com.sun.mail.util.*;/** * This is a utility class that provides various MIME related * functionality. <p> * * There are a set of methods to encode and decode MIME headers as * per RFC 2047. A brief description on handling such headers is * given below: <p> * * RFC 822 mail headers <strong>must</strong> contain only US-ASCII * characters. Headers that contain non US-ASCII characters must be * encoded so that they contain only US-ASCII characters. Basically, * this process involves using either BASE64 or QP to encode certain * characters. RFC 2047 describes this in detail. <p> * * In Java, Strings contain (16 bit) Unicode characters. ASCII is a * subset of Unicode (and occupies the range 0 - 127). A String * that contains only ASCII characters is already mail-safe. If the * String contains non US-ASCII characters, it must be encoded. An * additional complexity in this step is that since Unicode is not * yet a widely used charset, one might want to first charset-encode * the String into another charset and then do the transfer-encoding. * <p> * Note that to get the actual bytes of a mail-safe String (say, * for sending over SMTP), one must do * <p><blockquote><pre> * * byte[] bytes = string.getBytes("iso-8859-1"); * * </pre></blockquote><p> * * The <code>setHeader</code> and <code>addHeader</code> methods * on MimeMessage and MimeBodyPart assume that the given header values * are Unicode strings that contain only US-ASCII characters. Hence * the callers of those methods must insure that the values they pass * do not contain non US-ASCII characters. The methods in this class * help do this. <p> * * The <code>getHeader</code> family of methods on MimeMessage and * MimeBodyPart return the raw header value. These might be encoded * as per RFC 2047, and if so, must be decoded into Unicode Strings. * The methods in this class help to do this. <p> * * Several System properties control strict conformance to the MIME * spec. Note that these are not session properties but must be set * globally as System properties. <p> * * The <code>mail.mime.decodetext.strict</code> property controls * decoding of MIME encoded words. The MIME spec requires that encoded * words start at the beginning of a whitespace separated word. Some * mailers incorrectly include encoded words in the middle of a word. * If the <code>mail.mime.decodetext.strict</code> System property is * set to <code>"false"</code>, an attempt will be made to decode these * illegal encoded words. The default is true. <p> * * The <code>mail.mime.encodeeol.strict</code> property controls the * choice of Content-Transfer-Encoding for MIME parts that are not of * type "text". Often such parts will contain textual data for which * an encoding that allows normal end of line conventions is appropriate. * In rare cases, such a part will appear to contain entirely textual * data, but will require an encoding that preserves CR and LF characters * without change. If the <code>mail.mime.encodeeol.strict</code> * System property is set to <code>"true"</code>, such an encoding will * be used when necessary. The default is false. <p> * * In addition, the <code>mail.mime.charset</code> System property can * be used to specify the default MIME charset to use for encoded words * and text parts that don't otherwise specify a charset. Normally, the * default MIME charset is derived from the default Java charset, as * specified in the <code>file.encoding</code> System property. Most * applications will have no need to explicitly set the default MIME * charset. In cases where the default MIME charset to be used for * mail messages is different than the charset used for files stored on * the system, this property should be set. * * @version 1.60, 07/05/15 * @author John Mani * @author Bill Shannon */public class MimeUtility { // This class cannot be instantiated private MimeUtility() { } public static final int ALL = -1; private static boolean decodeStrict = true; private static boolean encodeEolStrict = false; private static boolean foldEncodedWords = false; private static boolean foldText = true; static { try { String s = System.getProperty("mail.mime.decodetext.strict"); // default to true decodeStrict = s == null || !s.equalsIgnoreCase("false"); s = System.getProperty("mail.mime.encodeeol.strict"); // default to false encodeEolStrict = s != null && s.equalsIgnoreCase("true"); s = System.getProperty("mail.mime.foldencodedwords"); // default to false foldEncodedWords = s != null && s.equalsIgnoreCase("true"); s = System.getProperty("mail.mime.foldtext"); // default to true foldText = s == null || !s.equalsIgnoreCase("false"); } catch (SecurityException sex) { // ignore it } } /** * Get the content-transfer-encoding that should be applied * to the input stream of this datasource, to make it mailsafe. <p> * * The algorithm used here is: <br> * <ul> * <li> * 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". * <li> * 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". * </ul> * * @param ds DataSource * @return the encoding. This is either "7bit", * "quoted-printable" or "base64" */ public static String getEncoding(DataSource ds) { ContentType cType = null; InputStream is = null; String encoding = null; try { cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); } catch (Exception ex) { return "base64"; // what else ?! } boolean isText = cType.match("text/*"); // if not text, stop processing when we see non-ASCII int i = checkAscii(is, ALL, !isText); switch (i) { case ALL_ASCII: encoding = "7bit"; // all ascii break; case MOSTLY_ASCII: encoding = "quoted-printable"; // mostly ascii break; default: encoding = "base64"; // mostly binary break; } // Close the input stream try { is.close(); } catch (IOException ioex) { } return encoding; } /** * Same as <code>getEncoding(DataSource)</code> except that instead * of reading the data from an <code>InputStream</code> it uses the * <code>writeTo</code> method to examine the data. This is more * efficient in the common case of a <code>DataHandler</code> * 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 <code>InputStream</code> the * <code>DataHandler</code> uses a thread, a pair of pipe streams, * and the <code>writeTo</code> method to produce the data. <p> * * @since JavaMail 1.2 */ public static String getEncoding(DataHandler dh) { ContentType cType = null; String encoding = null; /* * Try to pick the most efficient means of determining the * encoding. If this DataHandler was created using a DataSource, * the getEncoding(DataSource) method is typically faster. If * the DataHandler was created with an object, this method is * much faster. To distinguish the two cases, we use a heuristic. * A DataHandler created with an object will always have a null name. * A DataHandler created with a DataSource will usually have a * non-null name. * * XXX - This is actually quite a disgusting hack, but it makes * a common case run over twice as fast. */ if (dh.getName() != null) return getEncoding(dh.getDataSource()); try { cType = new ContentType(dh.getContentType()); } catch (Exception ex) { return "base64"; // what else ?! } if (cType.match("text/*")) { // Check all of the available bytes AsciiOutputStream aos = new AsciiOutputStream(false, false); try { dh.writeTo(aos); } catch (IOException ex) { // ignore it, can't happen } switch (aos.getAscii()) { case ALL_ASCII: encoding = "7bit"; // all ascii break; case MOSTLY_ASCII: encoding = "quoted-printable"; // mostly ascii break; default: encoding = "base64"; // mostly binary break; } } else { // not "text" // Check all of available bytes, break out if we find // at least one non-US-ASCII character AsciiOutputStream aos = new AsciiOutputStream(true, encodeEolStrict); try { dh.writeTo(aos); } catch (IOException ex) { } // ignore it if (aos.getAscii() == ALL_ASCII) // all ascii encoding = "7bit"; else // found atleast one non-ascii character, use b64 encoding = "base64"; } return encoding; } /** * Decode the given input stream. The Input stream returned is * the decoded input stream. All the encodings defined in RFC 2045 * are supported here. They include "base64", "quoted-printable", * "7bit", "8bit", and "binary". In addition, "uuencode" is also * supported. * * @param is input stream * @param encoding the encoding of the stream. * @return decoded input stream. */ public static InputStream decode(InputStream is, String encoding) throws MessagingException { if (encoding.equalsIgnoreCase("base64")) return new BASE64DecoderStream(is); else if (encoding.equalsIgnoreCase("quoted-printable")) return new QPDecoderStream(is); else if (encoding.equalsIgnoreCase("uuencode") || encoding.equalsIgnoreCase("x-uuencode") || encoding.equalsIgnoreCase("x-uue")) return new UUDecoderStream(is); else if (encoding.equalsIgnoreCase("binary") || encoding.equalsIgnoreCase("7bit") || encoding.equalsIgnoreCase("8bit")) return is; else throw new MessagingException("Unknown encoding: " + encoding); } /** * Wrap an encoder around the given output stream. * All the encodings defined in RFC 2045 are supported here. * They include "base64", "quoted-printable", "7bit", "8bit" and * "binary". In addition, "uuencode" is also supported. * * @param os output stream * @param encoding the encoding of the stream. * @return output stream that applies the * specified encoding. */ public static OutputStream encode(OutputStream os, String encoding) throws MessagingException { if (encoding == null) return os; else if (encoding.equalsIgnoreCase("base64")) return new BASE64EncoderStream(os); else if (encoding.equalsIgnoreCase("quoted-printable")) return new QPEncoderStream(os); else if (encoding.equalsIgnoreCase("uuencode") || encoding.equalsIgnoreCase("x-uuencode") || encoding.equalsIgnoreCase("x-uue")) return new UUEncoderStream(os); else if (encoding.equalsIgnoreCase("binary") || encoding.equalsIgnoreCase("7bit") || encoding.equalsIgnoreCase("8bit")) return os; else throw new MessagingException("Unknown encoding: " +encoding); } /** * Wrap an encoder around the given output stream. * All the encodings defined in RFC 2045 are supported here. * They include "base64", "quoted-printable", "7bit", "8bit" and * "binary". In addition, "uuencode" is also supported. * The <code>filename</code> parameter is used with the "uuencode" * encoding and is included in the encoded output. * * @param os output stream * @param encoding the encoding of the stream. * @param filename name for the file being encoded (only used * with uuencode) * @return output stream that applies the * specified encoding. * @since JavaMail 1.2 */ public static OutputStream encode(OutputStream os, String encoding, String filename) throws MessagingException { if (encoding == null) return os; else if (encoding.equalsIgnoreCase("base64")) return new BASE64EncoderStream(os); else if (encoding.equalsIgnoreCase("quoted-printable")) return new QPEncoderStream(os); else if (encoding.equalsIgnoreCase("uuencode") || encoding.equalsIgnoreCase("x-uuencode") || encoding.equalsIgnoreCase("x-uue")) return new UUEncoderStream(os, filename); else if (encoding.equalsIgnoreCase("binary") || encoding.equalsIgnoreCase("7bit") || encoding.equalsIgnoreCase("8bit")) return os; else throw new MessagingException("Unknown encoding: " +encoding); } /** * Encode a RFC 822 "text" token into mail-safe form as per * RFC 2047. <p> * * The given Unicode string is examined for non US-ASCII * characters. If the string contains only US-ASCII characters,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -