1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
34
35
36 public class ByteArrayDataSource implements DataSource {
37
38 private byte[] data;
39 private String type;
40
41
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
59 public ByteArrayDataSource(byte[] data, String type) {
60 this.data = data;
61 this.type = type;
62 }
63
64
65 public ByteArrayDataSource(String data, String type) {
66 try {
67
68
69
70 this.data = data.getBytes("iso-8859-1");
71 } catch (UnsupportedEncodingException uex) {}
72 this.type = type;
73 }
74
75
76
77
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 }