KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ByteArrayDataSource


1 /*
2  * @(#)ByteArrayDataSource.java 1.4 01/05/23
3  *
4  * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.io.*;
40 import javax.activation.*;
41
42 /**
43  * A simple DataSource for demonstration purposes.
44  * This class implements a DataSource from:
45  * an InputStream
46  * a byte array
47  * a String
48  *
49  * @author John Mani
50  * @author Bill Shannon
51  * @author Max Spivak
52  */

53 public class ByteArrayDataSource implements DataSource {
54     private byte[] data; // data
55
private String JavaDoc type; // content-type
56

57     /* Create a DataSource from an input stream */
58     public ByteArrayDataSource(InputStream is, String JavaDoc type) {
59         this.type = type;
60         try {
61             ByteArrayOutputStream os = new ByteArrayOutputStream();
62         int ch;
63
64         while ((ch = is.read()) != -1)
65                 // XXX - must be made more efficient by
66
// doing buffered reads, rather than one byte reads
67
os.write(ch);
68         data = os.toByteArray();
69
70         } catch (IOException ioex) { }
71     }
72
73     /* Create a DataSource from a byte array */
74     public ByteArrayDataSource(byte[] data, String JavaDoc type) {
75         this.data = data;
76     this.type = type;
77     }
78
79     /* Create a DataSource from a String */
80     public ByteArrayDataSource(String JavaDoc data, String JavaDoc type) {
81     try {
82         // Assumption that the string contains only ASCII
83
// characters! Otherwise just pass a charset into this
84
// constructor and use it in getBytes()
85
this.data = data.getBytes("iso-8859-1");
86     } catch (UnsupportedEncodingException uex) { }
87     this.type = type;
88     }
89
90     /**
91      * Return an InputStream for the data.
92      * Note - a new stream must be returned each time.
93      */

94     public InputStream getInputStream() throws IOException {
95     if (data == null)
96         throw new IOException("no data");
97     return new ByteArrayInputStream(data);
98     }
99
100     public OutputStream getOutputStream() throws IOException {
101     throw new IOException("cannot do this");
102     }
103
104     public String JavaDoc getContentType() {
105         return type;
106     }
107
108     public String JavaDoc getName() {
109         return "dummy";
110     }
111 }
112
Popular Tags