1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.mail;
17
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.io.UnsupportedEncodingException;
26
27 import javax.activation.DataSource;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class ByteArrayDataSource implements DataSource
43 {
44
45 public static final int BUFFER_SIZE = 512;
46
47
48 private ByteArrayOutputStream baos;
49
50
51 private String type = "application/octet-stream";
52
53
54
55
56
57
58
59
60
61 public ByteArrayDataSource(byte[] data, String aType) throws IOException
62 {
63 ByteArrayInputStream bis = null;
64
65 try
66 {
67 bis = new ByteArrayInputStream(data);
68 this.byteArrayDataSource(bis, aType);
69 }
70 catch (IOException ioex)
71 {
72 throw ioex;
73 }
74 finally
75 {
76 if (bis != null)
77 {
78 bis.close();
79 }
80 }
81 }
82
83
84
85
86
87
88
89
90
91 public ByteArrayDataSource(InputStream aIs, String aType) throws IOException
92 {
93 this.byteArrayDataSource(aIs, aType);
94 }
95
96
97
98
99
100
101
102
103
104 public ByteArrayDataSource(String data, String aType) throws IOException
105 {
106 this.type = aType;
107
108 try
109 {
110 baos = new ByteArrayOutputStream();
111
112
113
114
115 baos.write(data.getBytes("iso-8859-1"));
116 baos.flush();
117 baos.close();
118 }
119 catch (UnsupportedEncodingException uex)
120 {
121 throw new IOException("The Character Encoding is not supported.");
122 }
123 finally
124 {
125 if (baos != null)
126 {
127 baos.close();
128 }
129 }
130 }
131
132
133
134
135
136
137
138
139 private void byteArrayDataSource(InputStream aIs, String aType)
140 throws IOException
141 {
142 this.type = aType;
143
144 BufferedInputStream bis = null;
145 BufferedOutputStream osWriter = null;
146
147 try
148 {
149 int length = 0;
150 byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
151
152 bis = new BufferedInputStream(aIs);
153 baos = new ByteArrayOutputStream();
154 osWriter = new BufferedOutputStream(baos);
155
156
157 while ((length = bis.read(buffer)) != -1)
158 {
159 osWriter.write(buffer, 0, length);
160 }
161 osWriter.flush();
162 osWriter.close();
163
164 }
165 catch (IOException ioex)
166 {
167 throw ioex;
168 }
169 finally
170 {
171 if (bis != null)
172 {
173 bis.close();
174 }
175 if (baos != null)
176 {
177 baos.close();
178 }
179 if (osWriter != null)
180 {
181 osWriter.close();
182 }
183 }
184 }
185
186
187
188
189
190
191
192
193
194 public String getContentType()
195 {
196 return type == null ? "application/octet-stream" : type;
197 }
198
199
200
201
202
203
204
205
206 public InputStream getInputStream() throws IOException
207 {
208 if (baos == null)
209 {
210 throw new IOException("no data");
211 }
212 return new ByteArrayInputStream(baos.toByteArray());
213 }
214
215
216
217
218
219
220
221 public String getName()
222 {
223 return "ByteArrayDataSource";
224 }
225
226
227
228
229
230
231
232 public OutputStream getOutputStream()
233 {
234 baos = new ByteArrayOutputStream();
235 return baos;
236 }
237 }