View Javadoc

1   /*
2    * Copyright 2005-2008 The Kuali Foundation
3    * 
4    * 
5    * Licensed under the Educational Community License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    * 
9    * http://www.opensource.org/licenses/ecl2.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 org.kuali.rice.kew.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 org.kuali.rice.kew.api.WorkflowRuntimeException;
30  
31  
32  /**
33   * A simple DataSource for demonstration purposes. This class implements a
34   * DataSource from: an InputStream a byte array a String.
35   */
36  public class ByteArrayDataSource implements DataSource {
37  
38  	private byte[] data; // data
39      private String type; // content-type
40  
41      /* Create a DataSource from an input stream */
42      public ByteArrayDataSource(InputStream is, String type) {
43          this.type = type;
44          try {
45              ByteArrayOutputStream baos = new ByteArrayOutputStream();
46              OutputStream os = new BufferedOutputStream(baos);
47              int byteData;
48              while ((byteData = is.read()) != -1) {
49                  os.write(byteData);
50              }
51              os.flush();
52              data = baos.toByteArray();
53          } catch (IOException ioex) {
54          	throw new WorkflowRuntimeException(ioex);
55          }
56      }
57  
58      /* Create a DataSource from a byte array */
59      public ByteArrayDataSource(byte[] data, String type) {
60          this.data = data;
61          this.type = type;
62      }
63  
64      /* Create a DataSource from a String */
65      public ByteArrayDataSource(String data, String type) {
66          try {
67              // Assumption that the string contains only ASCII
68              // characters! Otherwise just pass a charset into this
69              // constructor and use it in getBytes()
70              this.data = data.getBytes("iso-8859-1");
71          } catch (UnsupportedEncodingException uex) {}
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  }