KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > email > ByteArrayDataSource


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.modules.email;
14
15
16 /*
17  * @(#)ByteArrayDataSource.java 1.2 00/05/24
18  *
19  * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
20  *
21  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
22  * modify and redistribute this software in source and binary code form,
23  * provided that i) this copyright notice and license appear on all copies of
24  * the software; and ii) Licensee does not utilize the software in a manner
25  * which is disparaging to Sun.
26  *
27  * This software is provided "AS IS," without a warranty of any kind. ALL
28  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
29  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
30  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
31  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
32  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
33  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
34  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
35  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
36  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGES.
38  *
39  * This software is not designed or intended for use in on-line control of
40  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
41  * the design, construction, operation or maintenance of any nuclear
42  * facility. Licensee represents and warrants that it will not use or
43  * redistribute the Software for such purposes.
44  */

45 import java.io.ByteArrayInputStream JavaDoc;
46 import java.io.ByteArrayOutputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.InputStream JavaDoc;
49 import java.io.OutputStream JavaDoc;
50 import java.io.UnsupportedEncodingException JavaDoc;
51
52 import javax.activation.DataSource JavaDoc;
53
54
55 /**
56  * A simple DataSource for demonstration purposes. This class implements a DataSource from: an
57  * InputStream a byte array a String
58  *
59  * @author John Mani
60  * @author Bill Shannon
61  * @author Max Spivak
62  */

63 public class ByteArrayDataSource implements DataSource JavaDoc
64 {
65     private String JavaDoc type; // content-type
66
private byte[] data; // data
67

68     /* Create a DataSource from an input stream */
69     public ByteArrayDataSource(InputStream JavaDoc is, String JavaDoc type)
70     {
71         this.type = type;
72
73         try
74         {
75             ByteArrayOutputStream JavaDoc os = new ByteArrayOutputStream JavaDoc();
76             int ch;
77
78             while ((ch = is.read()) != -1)
79
80                 // XXX - must be made more efficient by
81
// doing buffered reads, rather than one byte reads
82
os.write(ch);
83
84             data = os.toByteArray();
85         }
86         catch (IOException JavaDoc ioex)
87         {
88         }
89     }
90
91     /* Create a DataSource from a byte array */
92     public ByteArrayDataSource(byte[] data, String JavaDoc type)
93     {
94         this.data = data;
95         this.type = type;
96     }
97
98     /* Create a DataSource from a String */
99     public ByteArrayDataSource(String JavaDoc data, String JavaDoc type)
100     {
101         try
102         {
103             // Assumption that the string contains only ASCII
104
// characters! Otherwise just pass a charset into this
105
// constructor and use it in getBytes()
106
this.data = data.getBytes("iso-8859-1");
107         }
108         catch (UnsupportedEncodingException JavaDoc uex)
109         {
110         }
111
112         this.type = type;
113     }
114
115     /**
116      * DOCUMENT ME!
117      *
118      * @return
119      */

120     public String JavaDoc getContentType()
121     {
122         return type;
123     }
124
125     /**
126      * Return an InputStream for the data. Note - a new stream must be returned each time.
127      *
128      * @return
129      *
130      * @throws IOException DOCUMENT ME!
131      */

132     public InputStream JavaDoc getInputStream() throws IOException JavaDoc
133     {
134         if (data == null)
135         {
136             throw new IOException JavaDoc("no data");
137         }
138
139         return new ByteArrayInputStream JavaDoc(data);
140     }
141
142     /**
143      * DOCUMENT ME!
144      *
145      * @return
146      */

147     public String JavaDoc getName()
148     {
149         return "dummy";
150     }
151
152     /**
153      * DOCUMENT ME!
154      *
155      * @return
156      *
157      * @throws IOException
158      */

159     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
160     {
161         throw new IOException JavaDoc("cannot do this");
162     }
163 }
164
Popular Tags