Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ByteArrayDataSource |
|
| 3.0;3 |
1 | /* | |
2 | * $Id: ByteArrayDataSource.java 4350 2006-12-20 16:34:49Z holger $ | |
3 | * -------------------------------------------------------------------------------------- | |
4 | * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com | |
5 | * | |
6 | * The software in this package is published under the terms of the MuleSource MPL | |
7 | * license, a copy of which has been included with this distribution in the | |
8 | * LICENSE.txt file. | |
9 | */ | |
10 | ||
11 | /* | |
12 | * Copyright 2001-2004 The Apache Software Foundation | |
13 | * | |
14 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
15 | * you may not use this file except in compliance with the License. | |
16 | * You may obtain a copy of the License at | |
17 | * | |
18 | * http://www.apache.org/licenses/LICENSE-2.0 | |
19 | * | |
20 | * Unless required by applicable law or agreed to in writing, software | |
21 | * distributed under the License is distributed on an "AS IS" BASIS, | |
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
23 | * See the License for the specific language governing permissions and | |
24 | * limitations under the License. | |
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 | * This class implements a typed DataSource from:<br> - an InputStream<br> - a byte | |
44 | * array<br> - a String<br> | |
45 | * | |
46 | * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a> | |
47 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> | |
48 | * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> | |
49 | * @version $Id: ByteArrayDataSource.java 4350 2006-12-20 16:34:49Z holger $ | |
50 | */ | |
51 | public class ByteArrayDataSource implements DataSource | |
52 | { | |
53 | /** Stream containg the Data */ | |
54 | 0 | private ByteArrayOutputStream baos = null; |
55 | ||
56 | /** Content-type. */ | |
57 | 0 | private String type = "application/octet-stream"; |
58 | ||
59 | /** | |
60 | * Create a datasource from a byte array. | |
61 | * | |
62 | * @param data A byte[]. | |
63 | * @param type A String. | |
64 | * @exception IOException | |
65 | */ | |
66 | public ByteArrayDataSource(byte[] data, String type) throws IOException | |
67 | 0 | { |
68 | 0 | ByteArrayInputStream Bis = null; |
69 | ||
70 | try | |
71 | { | |
72 | 0 | Bis = new ByteArrayInputStream(data); |
73 | 0 | this.byteArrayDataSource(Bis, type); |
74 | 0 | } |
75 | 0 | catch (IOException ioex) |
76 | { | |
77 | 0 | throw ioex; |
78 | } | |
79 | finally | |
80 | { | |
81 | 0 | try |
82 | { | |
83 | 0 | if (Bis != null) |
84 | { | |
85 | 0 | Bis.close(); |
86 | } | |
87 | } | |
88 | 0 | catch (IOException ignored) |
89 | { | |
90 | // ignore | |
91 | 0 | } |
92 | 0 | } |
93 | 0 | } |
94 | ||
95 | /** | |
96 | * Create a datasource from an input stream. | |
97 | * | |
98 | * @param aIs An InputStream. | |
99 | * @param type A String. | |
100 | * @exception IOException | |
101 | */ | |
102 | public ByteArrayDataSource(InputStream aIs, String type) throws IOException | |
103 | 0 | { |
104 | 0 | this.byteArrayDataSource(aIs, type); |
105 | 0 | } |
106 | ||
107 | /** | |
108 | * Create a datasource from an input stream. | |
109 | * | |
110 | * @param aIs An InputStream. | |
111 | * @param type A String. | |
112 | * @exception IOException | |
113 | */ | |
114 | private void byteArrayDataSource(InputStream aIs, String type) throws IOException | |
115 | { | |
116 | 0 | this.type = type; |
117 | ||
118 | 0 | BufferedInputStream Bis = null; |
119 | 0 | BufferedOutputStream osWriter = null; |
120 | ||
121 | try | |
122 | { | |
123 | 0 | Bis = new BufferedInputStream(aIs); |
124 | 0 | baos = new ByteArrayOutputStream(); |
125 | 0 | osWriter = new BufferedOutputStream(baos); |
126 | ||
127 | // Write the InputData to OutputStream | |
128 | 0 | IOUtils.copy(Bis, osWriter); |
129 | 0 | osWriter.flush(); |
130 | 0 | osWriter.close(); |
131 | 0 | } |
132 | 0 | catch (IOException ioex) |
133 | { | |
134 | 0 | throw ioex; |
135 | } | |
136 | finally | |
137 | { | |
138 | 0 | try |
139 | { | |
140 | 0 | if (Bis != null) |
141 | { | |
142 | 0 | Bis.close(); |
143 | } | |
144 | 0 | if (baos != null) |
145 | { | |
146 | 0 | baos.close(); |
147 | } | |
148 | 0 | if (osWriter != null) |
149 | { | |
150 | 0 | osWriter.close(); |
151 | } | |
152 | } | |
153 | 0 | catch (IOException ignored) |
154 | { | |
155 | // ignore | |
156 | 0 | } |
157 | 0 | } |
158 | 0 | } |
159 | ||
160 | /** | |
161 | * Create a datasource from a String. | |
162 | * | |
163 | * @param data A String. | |
164 | * @param type A String. | |
165 | * @exception IOException | |
166 | */ | |
167 | public ByteArrayDataSource(String data, String type) throws IOException | |
168 | 0 | { |
169 | 0 | this.type = type; |
170 | ||
171 | try | |
172 | { | |
173 | 0 | baos = new ByteArrayOutputStream(); |
174 | ||
175 | // Assumption that the string contains only ASCII | |
176 | // characters! Else just pass in a charset into this | |
177 | // constructor and use it in getBytes(). | |
178 | 0 | baos.write(data.getBytes("iso-8859-1")); |
179 | 0 | baos.flush(); |
180 | 0 | baos.close(); |
181 | 0 | } |
182 | 0 | catch (UnsupportedEncodingException uex) |
183 | { | |
184 | // Do something! | |
185 | 0 | } |
186 | 0 | catch (IOException ignored) |
187 | { | |
188 | // Ignore | |
189 | 0 | } |
190 | finally | |
191 | { | |
192 | 0 | try |
193 | { | |
194 | 0 | if (baos != null) |
195 | { | |
196 | 0 | baos.close(); |
197 | } | |
198 | } | |
199 | 0 | catch (IOException ignored) |
200 | { | |
201 | // ignore | |
202 | 0 | } |
203 | 0 | } |
204 | 0 | } |
205 | ||
206 | /** | |
207 | * Get the content type. | |
208 | * | |
209 | * @return A String. | |
210 | */ | |
211 | public String getContentType() | |
212 | { | |
213 | 0 | return (type == null ? "application/octet-stream" : type); |
214 | } | |
215 | ||
216 | /** | |
217 | * Get the input stream. | |
218 | * | |
219 | * @return An InputStream. | |
220 | * @exception IOException | |
221 | */ | |
222 | public InputStream getInputStream() throws IOException | |
223 | { | |
224 | 0 | if (baos == null) |
225 | { | |
226 | 0 | throw new IOException("no data"); |
227 | } | |
228 | 0 | return new ByteArrayInputStream(baos.toByteArray()); |
229 | } | |
230 | ||
231 | /** | |
232 | * Get the name. | |
233 | * | |
234 | * @return A String. | |
235 | */ | |
236 | public String getName() | |
237 | { | |
238 | 0 | return "ByteArrayDataSource"; |
239 | } | |
240 | ||
241 | /** | |
242 | * Get the OutputStream to write to | |
243 | * | |
244 | * @return An OutputStream | |
245 | * @exception IOException | |
246 | */ | |
247 | public OutputStream getOutputStream() throws IOException | |
248 | { | |
249 | 0 | baos = new ByteArrayOutputStream(); |
250 | 0 | return baos; |
251 | } | |
252 | } |