01: /*
02: * Copyright 2005-2006 The Kuali Foundation.
03: *
04: *
05: * Licensed under the Educational Community License, Version 1.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.opensource.org/licenses/ecl1.php
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package edu.iu.uis.eden.util;
18:
19: import java.io.BufferedOutputStream;
20: import java.io.ByteArrayInputStream;
21: import java.io.ByteArrayOutputStream;
22: import java.io.IOException;
23: import java.io.InputStream;
24: import java.io.OutputStream;
25: import java.io.UnsupportedEncodingException;
26:
27: import javax.activation.DataSource;
28:
29: import edu.iu.uis.eden.exception.WorkflowRuntimeException;
30:
31: /**
32: * A simple DataSource for demonstration purposes. This class implements a
33: * DataSource from: an InputStream a byte array a String.
34: */
35: public class ByteArrayDataSource implements DataSource {
36:
37: private byte[] data; // data
38: private String type; // content-type
39:
40: /* Create a DataSource from an input stream */
41: public ByteArrayDataSource(InputStream is, String type) {
42: this .type = type;
43: try {
44: ByteArrayOutputStream baos = new ByteArrayOutputStream();
45: OutputStream os = new BufferedOutputStream(baos);
46: int byteData;
47: while ((byteData = is.read()) != -1) {
48: os.write(byteData);
49: }
50: os.flush();
51: data = baos.toByteArray();
52: } catch (IOException ioex) {
53: throw new WorkflowRuntimeException(ioex);
54: }
55: }
56:
57: /* Create a DataSource from a byte array */
58: public ByteArrayDataSource(byte[] data, String type) {
59: this .data = data;
60: this .type = type;
61: }
62:
63: /* Create a DataSource from a String */
64: public ByteArrayDataSource(String data, String type) {
65: try {
66: // Assumption that the string contains only ASCII
67: // characters! Otherwise just pass a charset into this
68: // constructor and use it in getBytes()
69: this .data = data.getBytes("iso-8859-1");
70: } catch (UnsupportedEncodingException uex) {
71: }
72: this .type = type;
73: }
74:
75: /**
76: * Return an InputStream for the data. Note - a new stream must be returned
77: * each time.
78: */
79: public InputStream getInputStream() throws IOException {
80: if (data == null)
81: throw new IOException("no data");
82: return new ByteArrayInputStream(data);
83: }
84:
85: public OutputStream getOutputStream() throws IOException {
86: throw new IOException("cannot do this");
87: }
88:
89: public String getContentType() {
90: return type;
91: }
92:
93: public String getName() {
94: return "dummy";
95: }
96: }
|