1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.mail;
18
19 import java.io.BufferedInputStream;
20 import java.io.BufferedOutputStream;
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.UnsupportedEncodingException;
27
28 import javax.activation.DataSource;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class ByteArrayDataSource implements DataSource
44 {
45
46 public static final int BUFFER_SIZE = 512;
47
48
49 private ByteArrayOutputStream baos;
50
51
52 private String type = "application/octet-stream";
53
54
55
56
57
58
59
60
61
62 public ByteArrayDataSource(byte[] data, String aType) throws IOException
63 {
64 ByteArrayInputStream bis = null;
65
66 try
67 {
68 bis = new ByteArrayInputStream(data);
69 this.byteArrayDataSource(bis, aType);
70 }
71 catch (IOException ioex)
72 {
73 throw ioex;
74 }
75 finally
76 {
77 if (bis != null)
78 {
79 bis.close();
80 }
81 }
82 }
83
84
85
86
87
88
89
90
91
92 public ByteArrayDataSource(InputStream aIs, String aType) throws IOException
93 {
94 this.byteArrayDataSource(aIs, aType);
95 }
96
97
98
99
100
101
102
103
104
105 public ByteArrayDataSource(String data, String aType) throws IOException
106 {
107 this.type = aType;
108
109 try
110 {
111 baos = new ByteArrayOutputStream();
112
113
114
115
116 baos.write(data.getBytes("iso-8859-1"));
117 baos.flush();
118 baos.close();
119 }
120 catch (UnsupportedEncodingException uex)
121 {
122 throw new IOException("The Character Encoding is not supported.");
123 }
124 finally
125 {
126 if (baos != null)
127 {
128 baos.close();
129 }
130 }
131 }
132
133
134
135
136
137
138
139
140 private void byteArrayDataSource(InputStream aIs, String aType)
141 throws IOException
142 {
143 this.type = aType;
144
145 BufferedInputStream bis = null;
146 BufferedOutputStream osWriter = null;
147
148 try
149 {
150 int length = 0;
151 byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
152
153 bis = new BufferedInputStream(aIs);
154 baos = new ByteArrayOutputStream();
155 osWriter = new BufferedOutputStream(baos);
156
157
158 while ((length = bis.read(buffer)) != -1)
159 {
160 osWriter.write(buffer, 0, length);
161 }
162 osWriter.flush();
163 osWriter.close();
164
165 }
166 catch (IOException ioex)
167 {
168 throw ioex;
169 }
170 finally
171 {
172 if (bis != null)
173 {
174 bis.close();
175 }
176 if (baos != null)
177 {
178 baos.close();
179 }
180 if (osWriter != null)
181 {
182 osWriter.close();
183 }
184 }
185 }
186
187
188
189
190
191
192
193
194
195 public String getContentType()
196 {
197 return type == null ? "application/octet-stream" : type;
198 }
199
200
201
202
203
204
205
206
207 public InputStream getInputStream() throws IOException
208 {
209 if (baos == null)
210 {
211 throw new IOException("no data");
212 }
213 return new ByteArrayInputStream(baos.toByteArray());
214 }
215
216
217
218
219
220
221
222 public String getName()
223 {
224 return "ByteArrayDataSource";
225 }
226
227
228
229
230
231
232
233 public OutputStream getOutputStream()
234 {
235 baos = new ByteArrayOutputStream();
236 return baos;
237 }
238 }