View Javadoc

1   package org.apache.turbine.util.mail;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
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  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   * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
37   * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
38   * @version $Id: ByteArrayDataSource.java 264148 2005-08-29 14:21:04Z henning $
39   * @deprecated Use org.apache.commons.mail.ByteArrayDataSource instead.
40   */
41  public class ByteArrayDataSource
42          implements DataSource
43  {
44      /*** Data. */
45      private byte[] data;
46  
47      /*** Content-type. */
48      private String type;
49  
50      private ByteArrayOutputStream baos;
51  
52      /***
53       * Create a datasource from a byte array.
54       *
55       * @param data A byte[].
56       * @param type A String.
57       */
58      public ByteArrayDataSource(byte[] data,
59                                 String type)
60      {
61          this.data = data;
62          this.type = type;
63      }
64  
65      /***
66       * Create a datasource from an input stream.
67       *
68       * @param is An InputStream.
69       * @param type A String.
70       */
71      public ByteArrayDataSource(InputStream is,
72                                 String type)
73      {
74          this.type = type;
75          try
76          {
77              int ch;
78  
79              ByteArrayOutputStream os = new ByteArrayOutputStream();
80              BufferedInputStream isReader = new BufferedInputStream(is);
81              BufferedOutputStream osWriter = new BufferedOutputStream(os);
82  
83              while ((ch = isReader.read()) != -1)
84              {
85                  osWriter.write(ch);
86              }
87              data = os.toByteArray();
88          }
89          catch (IOException ioex)
90          {
91              // Do something!
92          }
93      }
94  
95      /***
96       * Create a datasource from a String.
97       *
98       * @param data A String.
99       * @param type A String.
100      */
101     public ByteArrayDataSource(String data,
102                                String type)
103     {
104         this.type = type;
105         try
106         {
107             // Assumption that the string contains only ASCII
108             // characters!  Else just pass in a charset into this
109             // constructor and use it in getBytes().
110             this.data = data.getBytes("iso-8859-1");
111         }
112         catch (UnsupportedEncodingException uex)
113         {
114             // Do something!
115         }
116     }
117 
118     /***
119      * Get the content type.
120      *
121      * @return A String.
122      */
123     public String getContentType()
124     {
125         if (type == null)
126             return "application/octet-stream";
127         else
128             return type;
129     }
130 
131     /***
132      * Get the input stream.
133      *
134      * @return An InputStream.
135      * @exception IOException.
136      */
137     public InputStream getInputStream()
138             throws IOException
139     {
140         if (data == null)
141             throw new IOException("no data");
142         return new ByteArrayInputStream(data);
143     }
144 
145     /***
146      * Get the name.
147      *
148      * @return A String.
149      */
150     public String getName()
151     {
152         return "ByteArrayDataSource";
153     }
154 
155     /***
156      * Get the output stream.
157      *
158      * @return An OutputStream.
159      * @exception IOException.
160      */
161     public OutputStream getOutputStream()
162             throws IOException
163     {
164         baos = new ByteArrayOutputStream();
165         return baos;
166     }
167 }