KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > mail > internet > PreencodedMimeBodyPart


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)PreencodedMimeBodyPart.java 1.2 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package javax.mail.internet;
29
30 import java.io.*;
31 import java.util.Enumeration JavaDoc;
32 import javax.mail.*;
33
34 import com.sun.mail.util.LineOutputStream;
35
36 /**
37  * A MimeBodyPart that handles data that has already been encoded.
38  * This class is useful when constructing a message and attaching
39  * data that has already been encoded (for example, using base64
40  * encoding). The data may have been encoded by the application,
41  * or may have been stored in a file or database in encoded form.
42  * The encoding is supplied when this object is created. The data
43  * is attached to this object in the usual fashion, by using the
44  * <code>setText</code>, <code>setContent</code>, or
45  * <code>setDataHandler</code> methods.
46  *
47  * @since JavaMail 1.4
48  */

49
50 public class PreencodedMimeBodyPart extends MimeBodyPart JavaDoc {
51     private String JavaDoc encoding;
52
53     /**
54      * Create a PreencodedMimeBodyPart that assumes the data is
55      * encoded using the specified encoding. The encoding must
56      * be a MIME supported Content-Transfer-Encoding.
57      */

58     public PreencodedMimeBodyPart(String JavaDoc encoding) {
59     this.encoding = encoding;
60     }
61
62     /**
63      * Returns the content transfer encoding specified when
64      * this object was created.
65      */

66     public String JavaDoc getEncoding() throws MessagingException {
67     return encoding;
68     }
69
70     /**
71      * Output the body part as an RFC 822 format stream.
72      *
73      * @exception MessagingException
74      * @exception IOException if an error occurs writing to the
75      * stream or if an error is generated
76      * by the javax.activation layer.
77      * @see javax.activation.DataHandler#writeTo
78      */

79     public void writeTo(OutputStream os)
80             throws IOException, MessagingException {
81
82     // see if we already have a LOS
83
LineOutputStream los = null;
84     if (os instanceof LineOutputStream) {
85         los = (LineOutputStream) os;
86     } else {
87         los = new LineOutputStream(os);
88     }
89
90     // First, write out the header
91
Enumeration JavaDoc hdrLines = getAllHeaderLines();
92     while (hdrLines.hasMoreElements())
93         los.writeln((String JavaDoc)hdrLines.nextElement());
94
95     // The CRLF separator between header and content
96
los.writeln();
97
98     // Finally, the content, already encoded.
99
getDataHandler().writeTo(os);
100     os.flush();
101     }
102
103     /**
104      * Force the <code>Content-Transfer-Encoding</code> header to use
105      * the encoding that was specified when this object was created.
106      */

107     protected void updateHeaders() throws MessagingException {
108     super.updateHeaders();
109     MimeBodyPart.setEncoding(this, encoding);
110     }
111 }
112
Popular Tags