KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > email > transformers > ByteArrayDataSource


1 /*
2  * $Id: ByteArrayDataSource.java 3798 2006-11-04 04:07:14Z aperepel $
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 org.apache.commons.io.IOUtils;
30 import org.apache.commons.io.output.ByteArrayOutputStream;
31
32 import javax.activation.DataSource JavaDoc;
33
34 import java.io.BufferedInputStream JavaDoc;
35 import java.io.BufferedOutputStream JavaDoc;
36 import java.io.ByteArrayInputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.OutputStream JavaDoc;
40 import java.io.UnsupportedEncodingException JavaDoc;
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 3798 2006-11-04 04:07:14Z aperepel $
50  */

51 public class ByteArrayDataSource implements DataSource JavaDoc
52 {
53     /** Stream containg the Data */
54     private ByteArrayOutputStream baos = null;
55
56     /** Content-type. */
57     private String JavaDoc 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 JavaDoc type) throws IOException JavaDoc
67     {
68         ByteArrayInputStream JavaDoc Bis = null;
69
70         try
71         {
72             Bis = new ByteArrayInputStream JavaDoc(data);
73             this.byteArrayDataSource(Bis, type);
74         }
75         catch (IOException JavaDoc 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 JavaDoc ignored)
89             {
90                 // ignore
91
}
92         }
93     }
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 JavaDoc aIs, String JavaDoc type) throws IOException JavaDoc
103     {
104         this.byteArrayDataSource(aIs, type);
105     }
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 JavaDoc aIs, String JavaDoc type) throws IOException JavaDoc
115     {
116         this.type = type;
117
118         BufferedInputStream JavaDoc Bis = null;
119         BufferedOutputStream JavaDoc osWriter = null;
120
121         try
122         {
123             Bis = new BufferedInputStream JavaDoc(aIs);
124             baos = new ByteArrayOutputStream();
125             osWriter = new BufferedOutputStream JavaDoc(baos);
126
127             // Write the InputData to OutputStream
128
IOUtils.copy(Bis, osWriter);
129             osWriter.flush();
130             osWriter.close();
131         }
132         catch (IOException JavaDoc 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 JavaDoc ignored)
154             {
155                 // ignore
156
}
157         }
158     }
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 JavaDoc data, String JavaDoc type) throws IOException JavaDoc
168     {
169         this.type = type;
170
171         try
172         {
173             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
baos.write(data.getBytes("iso-8859-1"));
179             baos.flush();
180             baos.close();
181         }
182         catch (UnsupportedEncodingException JavaDoc uex)
183         {
184             // Do something!
185
}
186         catch (IOException JavaDoc ignored)
187         {
188             // Ignore
189
}
190         finally
191         {
192             try
193             {
194                 if (baos != null)
195                 {
196                     baos.close();
197                 }
198             }
199             catch (IOException JavaDoc ignored)
200             {
201                 // ignore
202
}
203         }
204     }
205
206     /**
207      * Get the content type.
208      *
209      * @return A String.
210      */

211     public String JavaDoc getContentType()
212     {
213         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 JavaDoc getInputStream() throws IOException JavaDoc
223     {
224         if (baos == null)
225         {
226             throw new IOException JavaDoc("no data");
227         }
228         return new ByteArrayInputStream JavaDoc(baos.toByteArray());
229     }
230
231     /**
232      * Get the name.
233      *
234      * @return A String.
235      */

236     public String JavaDoc getName()
237     {
238         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 JavaDoc
248     {
249         baos = new ByteArrayOutputStream();
250         return baos;
251     }
252 }
253
Popular Tags