KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dfs > ByteArrayDataSource


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

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

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

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

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