View Javadoc

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      private ByteArrayOutputStream baos = null;
55  
56      /** Content-type. */
57      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      {
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                  // 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 aIs, String type) throws IOException
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 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             // Write the InputData to OutputStream
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                 // 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 data, String type) throws IOException
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 uex)
183         {
184             // Do something!
185         }
186         catch (IOException ignored)
187         {
188             // Ignore
189         }
190         finally
191         {
192             try
193             {
194                 if (baos != null)
195                 {
196                     baos.close();
197                 }
198             }
199             catch (IOException ignored)
200             {
201                 // ignore
202             }
203         }
204     }
205 
206     /**
207      * Get the content type.
208      * 
209      * @return A String.
210      */
211     public String 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 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      * Get the name.
233      * 
234      * @return A String.
235      */
236     public String 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
248     {
249         baos = new ByteArrayOutputStream();
250         return baos;
251     }
252 }