📄 mimebodypart.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. *//* * @(#)MimeBodyPart.java 1.67 07/05/04 */package javax.mail.internet;import javax.mail.*;import javax.activation.*;import java.io.*;import java.util.*;import com.sun.mail.util.*;/** * This class represents a MIME body part. It implements the * <code>BodyPart</code> abstract class and the <code>MimePart</code> * interface. MimeBodyParts are contained in <code>MimeMultipart</code> * objects. <p> * * MimeBodyPart uses the <code>InternetHeaders</code> class to parse * and store the headers of that body part. <p> * * <hr><strong>A note on RFC 822 and MIME headers</strong><p> * * RFC 822 header fields <strong>must</strong> contain only * US-ASCII characters. MIME allows non ASCII characters to be present * in certain portions of certain headers, by encoding those characters. * RFC 2047 specifies the rules for doing this. The MimeUtility * class provided in this package can be used to to achieve this. * Callers of the <code>setHeader</code>, <code>addHeader</code>, and * <code>addHeaderLine</code> methods are responsible for enforcing * the MIME requirements for the specified headers. In addition, these * header fields must be folded (wrapped) before being sent if they * exceed the line length limitation for the transport (1000 bytes for * SMTP). Received headers may have been folded. The application is * responsible for folding and unfolding headers as appropriate. <p> * * @author John Mani * @author Bill Shannon * @author Kanwar Oberoi * @see javax.mail.Part * @see javax.mail.internet.MimePart * @see javax.mail.internet.MimeUtility */public class MimeBodyPart extends BodyPart implements MimePart { // Paranoia: // allow this last minute change to be disabled if it causes problems private static boolean setDefaultTextCharset = true; private static boolean setContentTypeFileName = true; private static boolean encodeFileName = false; private static boolean decodeFileName = false; // Paranoia: // allow this last minute change to be disabled if it causes problems static boolean cacheMultipart = true; // accessed by MimeMessage static { try { String s = System.getProperty("mail.mime.setdefaulttextcharset"); // default to true setDefaultTextCharset = s == null || !s.equalsIgnoreCase("false"); s = System.getProperty("mail.mime.setcontenttypefilename"); // default to true setContentTypeFileName = s == null || !s.equalsIgnoreCase("false"); s = System.getProperty("mail.mime.encodefilename"); // default to false encodeFileName = s != null && !s.equalsIgnoreCase("false"); s = System.getProperty("mail.mime.decodefilename"); // default to false decodeFileName = s != null && !s.equalsIgnoreCase("false"); s = System.getProperty("mail.mime.cachemultipart"); // default to true cacheMultipart = s == null || !s.equalsIgnoreCase("false"); } catch (SecurityException sex) { // ignore it } } /** * The DataHandler object representing this Part's content. */ protected DataHandler dh; /** * Byte array that holds the bytes of the content of this Part. */ protected byte[] content; /** * If the data for this body part was supplied by an * InputStream that implements the SharedInputStream interface, * <code>contentStream</code> is another such stream representing * the content of this body part. In this case, <code>content</code> * will be null. * * @since JavaMail 1.2 */ protected InputStream contentStream; /** * The InternetHeaders object that stores all the headers * of this body part. */ protected InternetHeaders headers; /** * If our content is a Multipart of Message object, we save it * the first time it's created by parsing a stream so that changes * to the contained objects will not be lost. */ private Object cachedContent; /** * An empty MimeBodyPart object is created. * This body part maybe filled in by a client constructing a multipart * message. */ public MimeBodyPart() { super(); headers = new InternetHeaders(); } /** * Constructs a MimeBodyPart by reading and parsing the data from * the specified input stream. The parser consumes data till the end * of the given input stream. The input stream must start at the * beginning of a valid MIME body part and must terminate at the end * of that body part. <p> * * Note that the "boundary" string that delimits body parts must * <strong>not</strong> be included in the input stream. The intention * is that the MimeMultipart parser will extract each body part's bytes * from a multipart stream and feed them into this constructor, without * the delimiter strings. * * @param is the body part Input Stream */ public MimeBodyPart(InputStream is) throws MessagingException { if (!(is instanceof ByteArrayInputStream) && !(is instanceof BufferedInputStream) && !(is instanceof SharedInputStream)) is = new BufferedInputStream(is); headers = new InternetHeaders(is); if (is instanceof SharedInputStream) { SharedInputStream sis = (SharedInputStream)is; contentStream = sis.newStream(sis.getPosition(), -1); } else { try { content = ASCIIUtility.getBytes(is); } catch (IOException ioex) { throw new MessagingException("Error reading input stream", ioex); } } } /** * Constructs a MimeBodyPart using the given header and * content bytes. <p> * * Used by providers. * * @param headers The header of this part * @param content bytes representing the body of this part. */ public MimeBodyPart(InternetHeaders headers, byte[] content) throws MessagingException { super(); this.headers = headers; this.content = content; } /** * Return the size of the content of this body part in bytes. * Return -1 if the size cannot be determined. <p> * * Note that this number may not be an exact measure of the * content size and may or may not account for any transfer * encoding of the content. <p> * * This implementation returns the size of the <code>content</code> * array (if not null), or, if <code>contentStream</code> is not * null, and the <code>available</code> method returns a positive * number, it returns that number as the size. Otherwise, it returns * -1. * * @return size in bytes, or -1 if not known */ public int getSize() throws MessagingException { if (content != null) return content.length; if (contentStream != null) { try { int size = contentStream.available(); // only believe the size if it's greate than zero, since zero // is the default returned by the InputStream class itself if (size > 0) return size; } catch (IOException ex) { // ignore it } } return -1; } /** * Return the number of lines for the content of this Part. * Return -1 if this number cannot be determined. <p> * * Note that this number may not be an exact measure of the * content length and may or may not account for any transfer * encoding of the content. <p> * * This implementation returns -1. * * @return number of lines, or -1 if not known */ public int getLineCount() throws MessagingException { return -1; } /** * Returns the value of the RFC 822 "Content-Type" header field. * This represents the content type of the content of this * body part. This value must not be null. If this field is * unavailable, "text/plain" should be returned. <p> * * This implementation uses <code>getHeader(name)</code> * to obtain the requisite header field. * * @return Content-Type of this body part */ public String getContentType() throws MessagingException { String s = getHeader("Content-Type", null); if (s == null) s = "text/plain"; return s; } /** * Is this Part of the specified MIME type? This method * compares <strong>only the <code>primaryType</code> and * <code>subType</code></strong>. * The parameters of the content types are ignored. <p> * * For example, this method will return <code>true</code> when * comparing a Part of content type <strong>"text/plain"</strong> * with <strong>"text/plain; charset=foobar"</strong>. <p> * * If the <code>subType</code> of <code>mimeType</code> is the * special character '*', then the subtype is ignored during the * comparison. */ public boolean isMimeType(String mimeType) throws MessagingException { return isMimeType(this, mimeType); } /** * Returns the value of the "Content-Disposition" header field. * This represents the disposition of this part. The disposition * describes how the part should be presented to the user. <p> * * If the Content-Disposition field is unavailable, * null is returned. <p> * * This implementation uses <code>getHeader(name)</code> * to obtain the requisite header field. * * @see #headers */ public String getDisposition() throws MessagingException { return getDisposition(this); } /** * Set the "Content-Disposition" header field of this body part. * If the disposition is null, any existing "Content-Disposition" * header field is removed. * * @exception IllegalWriteException if the underlying * implementation does not support modification * @exception IllegalStateException if this body part is * obtained from a READ_ONLY folder. */ public void setDisposition(String disposition) throws MessagingException { setDisposition(this, disposition); } /** * Returns the content transfer encoding from the * "Content-Transfer-Encoding" header * field. Returns <code>null</code> if the header is unavailable * or its value is absent. <p> * * This implementation uses <code>getHeader(name)</code> * to obtain the requisite header field. * * @see #headers */ public String getEncoding() throws MessagingException { return getEncoding(this); } /** * Returns the value of the "Content-ID" header field. Returns * <code>null</code> if the field is unavailable or its value is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -