1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.transport.mailets.listservcommands;
21
22
23 import javax.activation.DataSource;
24
25 import java.io.BufferedInputStream;
26 import java.io.BufferedOutputStream;
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.OutputStream;
32 import java.io.UnsupportedEncodingException;
33
34 /***
35 * MailDataSource implements a typed DataSource from :
36 * an InputStream, a byte array, and a string
37 *
38 * This is used from {@link BaseCommand#generateMail}
39 *
40 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 10:23:58 +0000 (Mon, 08 Jan 2007) $
41 * @since 2.2.0
42 */
43 public class MailDataSource implements DataSource {
44
45 protected static final int DEFAULT_BUF_SIZE = 0x2000;
46
47 protected static final String DEFAULT_ENCODING = "iso-8859-1";
48 protected static final String DEFAULT_NAME = "HtmlMailDataSource";
49
50 protected byte[] data;
51 protected String contentType;
52
53 /***
54 * Create a datasource from an input stream
55 */
56 public MailDataSource(InputStream inputStream, String contentType) throws IOException {
57 this.contentType = contentType;
58
59 ByteArrayOutputStream baos = new ByteArrayOutputStream();
60 copyStream(inputStream, baos);
61 data = baos.toByteArray();
62 }
63
64 /***
65 * Create a datasource from a byte array
66 */
67 public MailDataSource(byte[] data, String contentType) {
68 this.contentType = contentType;
69 this.data = data;
70 }
71
72 /***
73 * Create a datasource from a String
74 */
75 public MailDataSource(String data, String contentType) throws UnsupportedEncodingException {
76 this.contentType = contentType;
77 this.data = data.getBytes(DEFAULT_ENCODING);
78 }
79
80 /***
81 * returns the inputStream
82 */
83 public InputStream getInputStream() throws IOException {
84 if (data == null)
85 throw new IOException("no data");
86 return new ByteArrayInputStream(data);
87 }
88
89 /***
90 * Not implemented
91 */
92 public OutputStream getOutputStream() throws IOException {
93 throw new IOException("getOutputStream() isn't implemented");
94 }
95
96 /***
97 * returns the contentType for this data source
98 */
99 public String getContentType() {
100 return contentType;
101 }
102
103 /***
104 * returns a static moniker
105 */
106 public String getName() {
107 return DEFAULT_NAME;
108 }
109
110 protected static int copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
111 inputStream = new BufferedInputStream(inputStream);
112 outputStream = new BufferedOutputStream(outputStream);
113
114 byte[] bbuf = new byte[DEFAULT_BUF_SIZE];
115 int len;
116 int totalBytes = 0;
117 while ((len = inputStream.read(bbuf)) != -1) {
118 outputStream.write(bbuf, 0, len);
119 totalBytes += len;
120 }
121 outputStream.flush();
122 return totalBytes;
123 }
124 }
125