Skip to content

Package: ByteArrayDataSource$DSByteArrayOutputStream

ByteArrayDataSource$DSByteArrayOutputStream

nameinstructionbranchcomplexitylinemethod
ByteArrayDataSource.DSByteArrayOutputStream()
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%
getBuf()
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%
getCount()
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%

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.util;
18:
19: import jakarta.activation.DataSource;
20: import jakarta.mail.internet.ContentType;
21: import jakarta.mail.internet.MimeUtility;
22: import jakarta.mail.internet.ParseException;
23:
24: import java.io.ByteArrayOutputStream;
25: import java.io.IOException;
26: import java.io.InputStream;
27: import java.io.OutputStream;
28:
29: /**
30: * A DataSource backed by a byte array. The byte array may be
31: * passed in directly, or may be initialized from an InputStream
32: * or a String.
33: *
34: * @author John Mani
35: * @author Bill Shannon
36: * @author Max Spivak
37: * @since JavaMail 1.4
38: */
39: public class ByteArrayDataSource implements DataSource {
40: private byte[] data; // data
41: private int len = -1;
42: private String type; // content-type
43: private String name = "";
44:
45: static class DSByteArrayOutputStream extends ByteArrayOutputStream {
46: public byte[] getBuf() {
47: return buf;
48: }
49:
50: public int getCount() {
51: return count;
52: }
53: }
54:
55: /**
56: * Create a ByteArrayDataSource with data from the
57: * specified InputStream and with the specified MIME type.
58: * The InputStream is read completely and the data is
59: * stored in a byte array.
60: *
61: * @param is the InputStream
62: * @param type the MIME type
63: * @throws IOException errors reading the stream
64: */
65: public ByteArrayDataSource(InputStream is, String type) throws IOException {
66: DSByteArrayOutputStream os = new DSByteArrayOutputStream();
67: byte[] buf = new byte[8192];
68: int len;
69: while ((len = is.read(buf)) > 0)
70: os.write(buf, 0, len);
71: this.data = os.getBuf();
72: this.len = os.getCount();
73:
74: /*
75: * ByteArrayOutputStream doubles the size of the buffer every time
76: * it needs to expand, which can waste a lot of memory in the worst
77: * case with large buffers. Check how much is wasted here and if
78: * it's too much, copy the data into a new buffer and allow the
79: * old buffer to be garbage collected.
80: */
81: if (this.data.length - this.len > 256 * 1024) {
82: this.data = os.toByteArray();
83: this.len = this.data.length; // should be the same
84: }
85: this.type = type;
86: }
87:
88: /**
89: * Create a ByteArrayDataSource with data from the
90: * specified byte array and with the specified MIME type.
91: *
92: * @param data the data
93: * @param type the MIME type
94: */
95: public ByteArrayDataSource(byte[] data, String type) {
96: this.data = data;
97: this.type = type;
98: }
99:
100: /**
101: * Create a ByteArrayDataSource with data from the
102: * specified String and with the specified MIME type.
103: * The MIME type should include a <code>charset</code>
104: * parameter specifying the charset to be used for the
105: * string. If the parameter is not included, the
106: * default charset is used.
107: *
108: * @param data the String
109: * @param type the MIME type
110: * @throws IOException errors reading the String
111: */
112: public ByteArrayDataSource(String data, String type) throws IOException {
113: String charset = null;
114: try {
115: ContentType ct = new ContentType(type);
116: charset = ct.getParameter("charset");
117: } catch (ParseException pex) {
118: // ignore parse error
119: }
120: charset = MimeUtility.javaCharset(charset);
121: if (charset == null)
122: charset = MimeUtility.getDefaultJavaCharset();
123: // XXX - could convert to bytes on demand rather than copying here
124: this.data = data.getBytes(charset);
125: this.type = type;
126: }
127:
128: /**
129: * Return an InputStream for the data.
130: * Note that a new stream is returned each time
131: * this method is called.
132: *
133: * @return the InputStream
134: * @throws IOException if no data has been set
135: */
136: @Override
137: public InputStream getInputStream() throws IOException {
138: if (data == null)
139: throw new IOException("no data");
140: if (len < 0)
141: len = data.length;
142: return new SharedByteArrayInputStream(data, 0, len);
143: }
144:
145: /**
146: * Return an OutputStream for the data.
147: * Writing the data is not supported; an <code>IOException</code>
148: * is always thrown.
149: *
150: * @throws IOException always
151: */
152: @Override
153: public OutputStream getOutputStream() throws IOException {
154: throw new IOException("cannot do this");
155: }
156:
157: /**
158: * Get the MIME content type of the data.
159: *
160: * @return the MIME type
161: */
162: @Override
163: public String getContentType() {
164: return type;
165: }
166:
167: /**
168: * Get the name of the data.
169: * By default, an empty string ("") is returned.
170: *
171: * @return the name of this data
172: */
173: @Override
174: public String getName() {
175: return name;
176: }
177:
178: /**
179: * Set the name of the data.
180: *
181: * @param name the name of this data
182: */
183: public void setName(String name) {
184: this.name = name;
185: }
186: }