View Javadoc

1   /*
2    * Copyright 2001-2005 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This class implements a typed DataSource from:<br>
31   *
32   * - an InputStream<br>
33   * - a byte array<br>
34   * - a String<br>
35   *
36   * @since 1.0
37   * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
38   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
39   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
40   * @version $Id: ByteArrayDataSource.java 225600 2005-07-27 20:16:23Z rdonkin $
41   */
42  public class ByteArrayDataSource implements DataSource
43  {
44      /** define the buffer size */
45      public static final int BUFFER_SIZE = 512;
46  
47      /** Stream containg the Data */
48      private ByteArrayOutputStream baos;
49  
50      /** Content-type. */
51      private String type = "application/octet-stream";
52  
53      /**
54       * Create a datasource from a byte array.
55       *
56       * @param data A byte[].
57       * @param aType A String.
58       * @throws IOException IOException
59       * @since 1.0
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       * Create a datasource from an input stream.
85       *
86       * @param aIs An InputStream.
87       * @param aType A String.
88       * @throws IOException IOException
89       * @since 1.0
90       */
91      public ByteArrayDataSource(InputStream aIs, String aType) throws IOException
92      {
93          this.byteArrayDataSource(aIs, aType);
94      }
95  
96      /**
97       * Create a datasource from a String.
98       *
99       * @param data A String.
100      * @param aType A String.
101      * @throws IOException IOException
102      * @since 1.0
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             // Assumption that the string contains only ASCII
113             // characters!  Else just pass in a charset into this
114             // constructor and use it in getBytes().
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       * Create a datasource from an input stream.
134       *
135       * @param aIs An InputStream.
136       * @param aType A String.
137       * @throws IOException IOException
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             //Write the InputData to OutputStream
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      * Get the content type.
190      *
191      * @return A String.
192      * @since 1.0
193      */
194     public String getContentType()
195     {
196         return type == null ? "application/octet-stream" : type;
197     }
198 
199     /**
200      * Get the input stream.
201      *
202      * @return An InputStream.
203      * @throws IOException IOException
204      * @since 1.0
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      * Get the name.
217      *
218      * @return A String.
219      * @since 1.0
220      */
221     public String getName()
222     {
223         return "ByteArrayDataSource";
224     }
225 
226     /**
227      * Get the OutputStream to write to
228      *
229      * @return  An OutputStream
230      * @since 1.0
231      */
232     public OutputStream getOutputStream()
233     {
234         baos = new ByteArrayOutputStream();
235         return baos;
236     }
237 }