View Javadoc

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