001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * @(#)PreencodedMimeBodyPart.java 1.3 07/05/04
039: */
040:
041: package javax.mail.internet;
042:
043: import java.io.*;
044: import java.util.Enumeration;
045: import javax.mail.*;
046:
047: import com.sun.mail.util.LineOutputStream;
048:
049: /**
050: * A MimeBodyPart that handles data that has already been encoded.
051: * This class is useful when constructing a message and attaching
052: * data that has already been encoded (for example, using base64
053: * encoding). The data may have been encoded by the application,
054: * or may have been stored in a file or database in encoded form.
055: * The encoding is supplied when this object is created. The data
056: * is attached to this object in the usual fashion, by using the
057: * <code>setText</code>, <code>setContent</code>, or
058: * <code>setDataHandler</code> methods.
059: *
060: * @since JavaMail 1.4
061: */
062:
063: public class PreencodedMimeBodyPart extends MimeBodyPart {
064: private String encoding;
065:
066: /**
067: * Create a PreencodedMimeBodyPart that assumes the data is
068: * encoded using the specified encoding. The encoding must
069: * be a MIME supported Content-Transfer-Encoding.
070: */
071: public PreencodedMimeBodyPart(String encoding) {
072: this .encoding = encoding;
073: }
074:
075: /**
076: * Returns the content transfer encoding specified when
077: * this object was created.
078: */
079: public String getEncoding() throws MessagingException {
080: return encoding;
081: }
082:
083: /**
084: * Output the body part as an RFC 822 format stream.
085: *
086: * @exception MessagingException
087: * @exception IOException if an error occurs writing to the
088: * stream or if an error is generated
089: * by the javax.activation layer.
090: * @see javax.activation.DataHandler#writeTo
091: */
092: public void writeTo(OutputStream os) throws IOException,
093: MessagingException {
094:
095: // see if we already have a LOS
096: LineOutputStream los = null;
097: if (os instanceof LineOutputStream) {
098: los = (LineOutputStream) os;
099: } else {
100: los = new LineOutputStream(os);
101: }
102:
103: // First, write out the header
104: Enumeration hdrLines = getAllHeaderLines();
105: while (hdrLines.hasMoreElements())
106: los.writeln((String) hdrLines.nextElement());
107:
108: // The CRLF separator between header and content
109: los.writeln();
110:
111: // Finally, the content, already encoded.
112: getDataHandler().writeTo(os);
113: os.flush();
114: }
115:
116: /**
117: * Force the <code>Content-Transfer-Encoding</code> header to use
118: * the encoding that was specified when this object was created.
119: */
120: protected void updateHeaders() throws MessagingException {
121: super.updateHeaders();
122: MimeBodyPart.setEncoding(this, encoding);
123: }
124: }
|