KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > mail > workflow > ByteArrayDataSource


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: ByteArrayDataSource.java,v 1.1 2006/03/31 22:48:17 wfro Exp $
5  * Description: openCRX Mail
6  * Revision: $Revision: 1.1 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/31 22:48:17 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.mail.workflow;
57
58 import java.io.ByteArrayInputStream JavaDoc;
59 import java.io.ByteArrayOutputStream JavaDoc;
60 import java.io.IOException JavaDoc;
61 import java.io.InputStream JavaDoc;
62 import java.io.OutputStream JavaDoc;
63 import java.io.UnsupportedEncodingException JavaDoc;
64
65 import javax.activation.DataSource JavaDoc;
66
67 public class ByteArrayDataSource implements DataSource JavaDoc {
68     private byte[] data; // data
69
private String JavaDoc type; // content-type
70

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

108     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
109     if (data == null)
110         throw new IOException JavaDoc("no data");
111     return new ByteArrayInputStream JavaDoc(data);
112     }
113
114     public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
115     throw new IOException JavaDoc("cannot do this");
116     }
117
118     public String JavaDoc getContentType() {
119         return type;
120     }
121
122     public String JavaDoc getName() {
123         return "dummy";
124     }
125 }
126
Popular Tags