View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.mail;
18  
19  import java.io.BufferedInputStream;
20  import java.io.BufferedOutputStream;
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.io.UnsupportedEncodingException;
27  
28  import javax.activation.DataSource;
29  
30  /**
31   * This class implements a typed DataSource from:<br>
32   *
33   * - an InputStream<br>
34   * - a byte array<br>
35   * - a String<br>
36   *
37   * <p>
38   * From version 1.3.1, it is possible to set a name for this DataSource,
39   * and it is recommended to do so.
40   *
41   * @since 1.0
42   * @deprecated since 1.4, use {@link javax.mail.util.ByteArrayDataSource} instead
43   */
44  @Deprecated
45  public class ByteArrayDataSource implements DataSource
46  {
47      /** Define the buffer size. */
48      public static final int BUFFER_SIZE = 512;
49  
50      /** Stream containing the Data. */
51      private ByteArrayOutputStream baos;
52  
53      /** The Content-type. */
54      private final String type; // = "application/octet-stream";
55  
56      /**
57       * The name associated with this data source.
58       * By default, the name is an empty string, similar to javax.mail.util.ByteArrayDataSource.
59       * @since 1.3.1
60       */
61      private String name = "";
62  
63      /**
64       * Create a datasource from a byte array.
65       *
66       * @param data A byte[].
67       * @param aType A String.
68       * @throws IOException IOException
69       * @since 1.0
70       */
71      public ByteArrayDataSource(final byte[] data, final String aType) throws IOException
72      {
73          this.type = aType;
74          ByteArrayInputStream bis = null;
75  
76          try
77          {
78              bis = new ByteArrayInputStream(data);
79              this.byteArrayDataSource(bis);
80          }
81          finally
82          {
83              if (bis != null)
84              {
85                  bis.close();
86              }
87          }
88      }
89  
90      /**
91       * Create a datasource from an input stream.
92       *
93       * @param aIs An InputStream.
94       * @param aType A String.
95       * @throws IOException IOException
96       * @since 1.0
97       */
98      public ByteArrayDataSource(final InputStream aIs, final String aType) throws IOException
99      {
100         this.type = aType;
101         this.byteArrayDataSource(aIs);
102     }
103 
104     /**
105      * Create a datasource from a String.
106      * N.B. assumes the data string can be converted using the charset iso-8859-1.
107      *
108      * @param data A String.
109      * @param aType A String.
110      * @throws IOException IOException
111      * @since 1.0
112      */
113     public ByteArrayDataSource(final String data, final String aType) throws IOException
114     {
115         this.type = aType;
116 
117         try
118         {
119             baos = new ByteArrayOutputStream();
120 
121             // Assumption that the string contains only ASCII characters!
122             // Else just pass in a charset into this constructor and use it in getBytes().
123             baos.write(data.getBytes("iso-8859-1"));
124             baos.flush();
125             baos.close();
126         }
127         catch (final UnsupportedEncodingException uex)
128         {
129             throw new IOException("The Character Encoding is not supported.");
130         }
131         finally
132         {
133             if (baos != null)
134             {
135                 baos.close();
136             }
137         }
138     }
139 
140     /**
141       * Create a datasource from an input stream.
142       *
143       * @param aIs An InputStream.
144       * @throws IOException IOException
145       */
146     private void byteArrayDataSource(final InputStream aIs)
147         throws IOException
148     {
149         BufferedInputStream bis = null;
150         BufferedOutputStream osWriter = null;
151 
152         try
153         {
154             int length = 0;
155             final byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
156 
157             bis = new BufferedInputStream(aIs);
158             baos = new ByteArrayOutputStream();
159             osWriter = new BufferedOutputStream(baos);
160 
161             // Write the InputData to OutputStream
162             while ((length = bis.read(buffer)) != -1)
163             {
164                 osWriter.write(buffer, 0, length);
165             }
166             osWriter.flush();
167             osWriter.close();
168 
169         }
170         finally
171         {
172             if (bis != null)
173             {
174                 bis.close();
175             }
176             if (baos != null)
177             {
178                 baos.close();
179             }
180             if (osWriter != null)
181             {
182                 osWriter.close();
183             }
184         }
185     }
186 
187     /**
188      * Get the content type.
189      *
190      * @return A String.
191      * @since 1.0
192      */
193     @Override
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     @Override
207     public InputStream getInputStream() throws IOException
208     {
209         if (baos == null)
210         {
211             throw new IOException("no data");
212         }
213         return new ByteArrayInputStream(baos.toByteArray());
214     }
215 
216     /**
217      * Sets the name for this DataSource.
218      *
219      * @param name The name.
220      * @since 1.3.1
221      */
222     public void setName(final String name)
223     {
224         this.name = name;
225     }
226 
227     /**
228      * Get the name.
229      *
230      * @return A String.
231      * @since 1.0
232      */
233     @Override
234     public String getName()
235     {
236         return name;
237     }
238 
239     /**
240      * Get the OutputStream to write to.
241      *
242      * @return  An OutputStream
243      * @since 1.0
244      */
245     @Override
246     public OutputStream getOutputStream()
247     {
248         baos = new ByteArrayOutputStream();
249         return baos;
250     }
251 }