1 /**
2 *
3 * Copyright 2006 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package javax.mail.util;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.ByteArrayInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25
26 import javax.activation.DataSource;
27 import javax.mail.internet.ContentType;
28 import javax.mail.internet.ParseException;
29 import javax.mail.internet.MimeUtility;
30
31
32 /**
33 * An activation DataSource object that sources the data from
34 * a byte[] array.
35 * @version $Rev: 421852 $ $Date: 2006-07-14 03:02:19 -0700 (Fri, 14 Jul 2006) $
36 */
37 public class ByteArrayDataSource implements DataSource {
38
39 private byte[] source;
40
41 private String contentType;
42
43 private String name = "";
44
45
46 /**
47 * Create a ByteArrayDataSource from an input stream.
48 *
49 * @param in The source input stream.
50 * @param type The MIME-type of the data.
51 *
52 * @exception IOException
53 */
54 public ByteArrayDataSource(InputStream in, String type) throws IOException {
55 ByteArrayOutputStream sink = new ByteArrayOutputStream();
56
57
58 byte[] buffer = new byte[8192];
59 int bytesRead;
60
61 while ((bytesRead = in.read(buffer)) > 0) {
62 sink.write(buffer, 0, bytesRead);
63 }
64
65 source = sink.toByteArray();
66 contentType = type;
67 }
68
69
70 /**
71 * Create a ByteArrayDataSource directly from a byte array.
72 *
73 * @param data The source byte array (not copied).
74 * @param type The content MIME-type.
75 */
76 public ByteArrayDataSource(byte[] data, String type) {
77 source = data;
78 contentType = type;
79 }
80
81 /**
82 * Create a ByteArrayDataSource from a string value. If the
83 * type information includes a charset parameter, that charset
84 * is used to extract the bytes. Otherwise, the default Java
85 * char set is used.
86 *
87 * @param data The source data string.
88 * @param type The MIME type information.
89 *
90 * @exception IOException
91 */
92 public ByteArrayDataSource(String data, String type) throws IOException {
93 String charset = null;
94 try {
95
96
97 ContentType content = new ContentType(type);
98 charset = content.getParameter("charset");
99 } catch (ParseException e) {
100
101 }
102 if (charset == null) {
103 charset = MimeUtility.getDefaultJavaCharset();
104 }
105 else {
106
107 charset = MimeUtility.javaCharset(charset);
108 }
109
110
111 source = data.getBytes(charset);
112 contentType = type;
113 }
114
115
116 /**
117 * Create an input stream for this data. A new input stream
118 * is created each time.
119 *
120 * @return An InputStream for reading the encapsulated data.
121 * @exception IOException
122 */
123 public InputStream getInputStream() throws IOException {
124 return new ByteArrayInputStream(source);
125 }
126
127
128 /**
129 * Open an output stream for the DataSource. This is not
130 * supported by this DataSource, so an IOException is always
131 * throws.
132 *
133 * @return Nothing...an IOException is always thrown.
134 * @exception IOException
135 */
136 public OutputStream getOutputStream() throws IOException {
137 throw new IOException("Writing to a ByteArrayDataSource is not supported");
138 }
139
140
141 /**
142 * Get the MIME content type information for this DataSource.
143 *
144 * @return The MIME content type string.
145 */
146 public String getContentType() {
147 return contentType;
148 }
149
150
151 /**
152 * Retrieve the DataSource name. If not explicitly set, this
153 * returns "".
154 *
155 * @return The currently set DataSource name.
156 */
157 public String getName() {
158 return name;
159 }
160
161
162 /**
163 * Set a new DataSource name.
164 *
165 * @param name The new name.
166 */
167 public void setName(String name) {
168 this.name = name;
169 }
170 }
171