Skip to content

Package: MimePartDataSource

MimePartDataSource

nameinstructionbranchcomplexitylinemethod
MimePartDataSource(MimePart)
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%
getContentType()
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%
getInputStream()
M: 60 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 14 C: 0
0%
M: 1 C: 0
0%
getMessageContext()
M: 13 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getName()
M: 12 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getOutputStream()
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2023 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 jakarta.activation.DataSource;
20: import jakarta.mail.FolderClosedException;
21: import jakarta.mail.MessageAware;
22: import jakarta.mail.MessageContext;
23: import jakarta.mail.MessagingException;
24:
25: import java.io.IOException;
26: import java.io.InputStream;
27: import java.io.OutputStream;
28: import java.net.UnknownServiceException;
29:
30:
31: /**
32: * A utility class that implements a DataSource out of
33: * a MimePart. This class is primarily meant for service providers.
34: *
35: * @author John Mani
36: * @see jakarta.mail.internet.MimePart
37: * @see jakarta.activation.DataSource
38: */
39:
40: public class MimePartDataSource implements DataSource, MessageAware {
41: /**
42: * The MimePart that provides the data for this DataSource.
43: *
44: * @since JavaMail 1.4
45: */
46: protected MimePart part;
47:
48: private MessageContext context;
49:
50: /**
51: * Constructor, that constructs a DataSource from a MimePart.
52: *
53: * @param part the MimePart
54: */
55: public MimePartDataSource(MimePart part) {
56: this.part = part;
57: }
58:
59: /**
60: * Returns an input stream from this MimePart. <p>
61: *
62: * This method applies the appropriate transfer-decoding, based
63: * on the Content-Transfer-Encoding attribute of this MimePart.
64: * Thus the returned input stream is a decoded stream of bytes.<p>
65: *
66: * This implementation obtains the raw content from the Part
67: * using the <code>getContentStream()</code> method and decodes
68: * it using the <code>MimeUtility.decode()</code> method.
69: *
70: * @return decoded input stream
71: * @see jakarta.mail.internet.MimeMessage#getContentStream
72: * @see jakarta.mail.internet.MimeBodyPart#getContentStream
73: * @see jakarta.mail.internet.MimeUtility#decode
74: */
75: @Override
76: public InputStream getInputStream() throws IOException {
77: InputStream is;
78:
79: try {
80:• if (part instanceof MimeBodyPart)
81: is = ((MimeBodyPart) part).getContentStream();
82:• else if (part instanceof MimeMessage)
83: is = ((MimeMessage) part).getContentStream();
84: else
85: throw new MessagingException("Unknown part");
86:
87: String encoding =
88: MimeBodyPart.restrictEncoding(part, part.getEncoding());
89:• if (encoding != null)
90: return MimeUtility.decode(is, encoding);
91: else
92: return is;
93: } catch (FolderClosedException fex) {
94: throw new IOException(new FolderClosedException(fex.getFolder(), fex.getMessage(), fex));
95: } catch (MessagingException mex) {
96: // Cause is removed because in upper stack the cause is checked. See test POP3FolderClosedExceptionTest
97: throw new IOException(mex.getMessage());
98: }
99: }
100:
101: /**
102: * DataSource method to return an output stream. <p>
103: *
104: * This implementation throws the UnknownServiceException.
105: */
106: @Override
107: public OutputStream getOutputStream() throws IOException {
108: throw new UnknownServiceException("Writing not supported");
109: }
110:
111: /**
112: * Returns the content-type of this DataSource. <p>
113: *
114: * This implementation just invokes the <code>getContentType</code>
115: * method on the MimePart.
116: */
117: @Override
118: public String getContentType() {
119: try {
120: return part.getContentType();
121: } catch (MessagingException mex) {
122: // would like to be able to reflect the exception to the
123: // application, but since we can't do that we return a
124: // generic "unknown" value here and hope for another
125: // exception later.
126: return "application/octet-stream";
127: }
128: }
129:
130: /**
131: * DataSource method to return a name. <p>
132: *
133: * This implementation just returns an empty string.
134: */
135: @Override
136: public String getName() {
137: try {
138:• if (part instanceof MimeBodyPart)
139: return part.getFileName();
140: } catch (MessagingException mex) {
141: // ignore it
142: }
143: return "";
144: }
145:
146: /**
147: * Return the <code>MessageContext</code> for the current part.
148: *
149: * @since JavaMail 1.1
150: */
151: @Override
152: public synchronized MessageContext getMessageContext() {
153:• if (context == null)
154: context = new MessageContext(part);
155: return context;
156: }
157: }