Skip to content

Package: PreencodedMimeBodyPart

PreencodedMimeBodyPart

nameinstructionbranchcomplexitylinemethod
PreencodedMimeBodyPart(String)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getEncoding()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
updateHeaders()
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
writeTo(OutputStream)
M: 36 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0, which is available at
6: * http://www.eclipse.org/legal/epl-2.0.
7: *
8: * This Source Code may also be made available under the following Secondary
9: * Licenses when the conditions for such availability set forth in the
10: * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11: * version 2 with the GNU Classpath Exception, which is available at
12: * https://www.gnu.org/software/classpath/license.html.
13: *
14: * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15: */
16:
17: package jakarta.mail.internet;
18:
19: import java.io.IOException;
20: import java.io.OutputStream;
21: import java.util.Enumeration;
22:
23: import jakarta.mail.MessagingException;
24: import jakarta.mail.util.LineOutputStream;
25:
26: /**
27: * A MimeBodyPart that handles data that has already been encoded.
28: * This class is useful when constructing a message and attaching
29: * data that has already been encoded (for example, using base64
30: * encoding). The data may have been encoded by the application,
31: * or may have been stored in a file or database in encoded form.
32: * The encoding is supplied when this object is created. The data
33: * is attached to this object in the usual fashion, by using the
34: * <code>setText</code>, <code>setContent</code>, or
35: * <code>setDataHandler</code> methods.
36: *
37: * @since        JavaMail 1.4
38: */
39:
40: public class PreencodedMimeBodyPart extends MimeBodyPart {
41: private String encoding;
42:
43: /**
44: * Create a PreencodedMimeBodyPart that assumes the data is
45: * encoded using the specified encoding. The encoding must
46: * be a MIME supported Content-Transfer-Encoding.
47: *
48: * @param        encoding        the Content-Transfer-Encoding
49: */
50: public PreencodedMimeBodyPart(String encoding) {
51:         this.encoding = encoding;
52: }
53:
54: /**
55: * Returns the content transfer encoding specified when
56: * this object was created.
57: */
58: @Override
59: public String getEncoding() throws MessagingException {
60:         return encoding;
61: }
62:
63: /**
64: * Output the body part as an RFC 822 format stream.
65: *
66: * @exception IOException        if an error occurs writing to the
67: *                                stream or if an error is generated
68: *                                by the jakarta.activation layer.
69: * @exception MessagingException for other failures
70: * @see jakarta.activation.DataHandler#writeTo
71: */
72: @Override
73: public void writeTo(OutputStream os)
74:                         throws IOException, MessagingException {
75:
76:         // see if we already have a LOS
77:         LineOutputStream los = null;
78:•        if (os instanceof LineOutputStream) {
79:          los = (LineOutputStream) os;
80:         } else {
81:          los = streamProvider.outputLineStream(os, false);
82:         }
83:
84:         // First, write out the header
85:         Enumeration<String> hdrLines = getAllHeaderLines();
86:•        while (hdrLines.hasMoreElements())
87:          los.writeln(hdrLines.nextElement());
88:
89:         // The CRLF separator between header and content
90:         los.writeln();
91:
92:         // Finally, the content, already encoded.
93:         getDataHandler().writeTo(os);
94:         os.flush();
95: }
96:
97: /**
98: * Force the <code>Content-Transfer-Encoding</code> header to use
99: * the encoding that was specified when this object was created.
100: */
101: @Override
102: protected void updateHeaders() throws MessagingException {
103:         super.updateHeaders();
104:         MimeBodyPart.setEncoding(this, encoding);
105: }
106: }