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
44 @Deprecated
45 public class ByteArrayDataSource implements DataSource
46 {
47
48 public static final int BUFFER_SIZE = 512;
49
50
51 private ByteArrayOutputStream baos;
52
53
54 private final String type;
55
56
57
58
59
60
61 private String name = "";
62
63
64
65
66
67
68
69
70
71 public ByteArrayDataSource(final byte[] data, final String aType) throws IOException
72 {
73 this.type = aType;
74 ByteArrayInputStream bis = null;
75
76 try
77 {
78 bis = new ByteArrayInputStream(data);
79 this.byteArrayDataSource(bis);
80 }
81 finally
82 {
83 if (bis != null)
84 {
85 bis.close();
86 }
87 }
88 }
89
90
91
92
93
94
95
96
97
98 public ByteArrayDataSource(final InputStream aIs, final String aType) throws IOException
99 {
100 this.type = aType;
101 this.byteArrayDataSource(aIs);
102 }
103
104
105
106
107
108
109
110
111
112
113 public ByteArrayDataSource(final String data, final String aType) throws IOException
114 {
115 this.type = aType;
116
117 try
118 {
119 baos = new ByteArrayOutputStream();
120
121
122
123 baos.write(data.getBytes("iso-8859-1"));
124 baos.flush();
125 baos.close();
126 }
127 catch (final UnsupportedEncodingException uex)
128 {
129 throw new IOException("The Character Encoding is not supported.");
130 }
131 finally
132 {
133 if (baos != null)
134 {
135 baos.close();
136 }
137 }
138 }
139
140
141
142
143
144
145
146 private void byteArrayDataSource(final InputStream aIs)
147 throws IOException
148 {
149 BufferedInputStream bis = null;
150 BufferedOutputStream osWriter = null;
151
152 try
153 {
154 int length = 0;
155 final byte[] buffer = new byte[ByteArrayDataSource.BUFFER_SIZE];
156
157 bis = new BufferedInputStream(aIs);
158 baos = new ByteArrayOutputStream();
159 osWriter = new BufferedOutputStream(baos);
160
161
162 while ((length = bis.read(buffer)) != -1)
163 {
164 osWriter.write(buffer, 0, length);
165 }
166 osWriter.flush();
167 osWriter.close();
168
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 @Override
194 public String getContentType()
195 {
196 return type == null ? "application/octet-stream" : type;
197 }
198
199
200
201
202
203
204
205
206 @Override
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 void setName(final String name)
223 {
224 this.name = name;
225 }
226
227
228
229
230
231
232
233 @Override
234 public String getName()
235 {
236 return name;
237 }
238
239
240
241
242
243
244
245 @Override
246 public OutputStream getOutputStream()
247 {
248 baos = new ByteArrayOutputStream();
249 return baos;
250 }
251 }