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