KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > mail > ByteArrayDataSource


1 package org.apache.turbine.util.mail;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import java.io.BufferedOutputStream JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.UnsupportedEncodingException JavaDoc;
27 import javax.activation.DataSource JavaDoc;
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,v 1.4.2.2 2004/05/20 03:20:18 seade Exp $
39  * @deprecated Use org.apache.commons.mail.ByteArrayDataSource instead.
40  */

41 public class ByteArrayDataSource
42         implements DataSource JavaDoc
43 {
44     /** Data. */
45     private byte[] data;
46
47     /** Content-type. */
48     private String JavaDoc type;
49
50     private ByteArrayOutputStream JavaDoc 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 JavaDoc 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 JavaDoc is,
72                                String JavaDoc type)
73     {
74         this.type = type;
75         try
76         {
77             int ch;
78
79             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
80             BufferedInputStream JavaDoc isReader = new BufferedInputStream JavaDoc(is);
81             BufferedOutputStream JavaDoc osWriter = new BufferedOutputStream JavaDoc(os);
82
83             while ((ch = isReader.read()) != -1)
84             {
85                 osWriter.write(ch);
86             }
87             data = os.toByteArray();
88         }
89         catch (IOException JavaDoc 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 JavaDoc data,
102                                String JavaDoc 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 JavaDoc uex)
113         {
114             // Do something!
115
}
116     }
117
118     /**
119      * Get the content type.
120      *
121      * @return A String.
122      */

123     public String JavaDoc 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 JavaDoc getInputStream()
138             throws IOException JavaDoc
139     {
140         if (data == null)
141             throw new IOException JavaDoc("no data");
142         return new ByteArrayInputStream JavaDoc(data);
143     }
144
145     /**
146      * Get the name.
147      *
148      * @return A String.
149      */

150     public String JavaDoc 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 JavaDoc getOutputStream()
162             throws IOException JavaDoc
163     {
164         baos = new ByteArrayOutputStream JavaDoc();
165         return baos;
166     }
167 }
168
Popular Tags