View Javadoc

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  
21  
22  package org.apache.james.transport.mailets.listservcommands;
23  
24  
25  import javax.activation.DataSource;
26  
27  import java.io.BufferedInputStream;
28  import java.io.BufferedOutputStream;
29  import java.io.ByteArrayInputStream;
30  import java.io.ByteArrayOutputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.io.OutputStream;
34  import java.io.UnsupportedEncodingException;
35  
36  /**
37   * MailDataSource implements a typed DataSource from :
38   *  an InputStream, a byte array, and a string
39   *
40   * This is used from {@link BaseCommand#generateMail}
41   *
42   * @version CVS $Revision: 426007 $ $Date: 2006-07-27 09:52:06 +0100 (Thu, 27 Jul 2006) $
43   * @since 2.2.0
44   */
45  public class MailDataSource implements DataSource {
46  
47      protected static final int DEFAULT_BUF_SIZE = 0x2000;
48  
49      protected static final String DEFAULT_ENCODING = "iso-8859-1";
50      protected static final String DEFAULT_NAME = "HtmlMailDataSource";
51  
52      protected byte[] data; // data
53      protected String contentType; // content-type
54  
55      /**
56       * Create a datasource from an input stream
57       */
58      public MailDataSource(InputStream inputStream, String contentType) throws IOException {
59          this.contentType = contentType;
60  
61          ByteArrayOutputStream baos = new ByteArrayOutputStream();
62          copyStream(inputStream, baos);
63          data = baos.toByteArray();
64      }
65  
66      /**
67       * Create a datasource from a byte array
68       */
69      public MailDataSource(byte[] data, String contentType) {
70          this.contentType = contentType;
71          this.data = data;
72      }
73  
74      /**
75       * Create a datasource from a String
76       */
77      public MailDataSource(String data, String contentType) throws UnsupportedEncodingException {
78          this.contentType = contentType;
79          this.data = data.getBytes(DEFAULT_ENCODING);
80      }
81  
82      /**
83       * returns the inputStream
84       */
85      public InputStream getInputStream() throws IOException {
86          if (data == null)
87              throw new IOException("no data");
88          return new ByteArrayInputStream(data);
89      }
90  
91      /**
92       * Not implemented
93       */
94      public OutputStream getOutputStream() throws IOException {
95          throw new IOException("getOutputStream() isn't implemented");
96      }
97  
98      /**
99       * returns the contentType for this data source
100      */
101     public String getContentType() {
102         return contentType;
103     }
104 
105     /**
106      * returns a static moniker
107      */
108     public String getName() {
109         return DEFAULT_NAME;
110     }
111 
112     protected static int copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
113         inputStream = new BufferedInputStream(inputStream);
114         outputStream = new BufferedOutputStream(outputStream);
115 
116         byte[] bbuf = new byte[DEFAULT_BUF_SIZE];
117         int len;
118         int totalBytes = 0;
119         while ((len = inputStream.read(bbuf)) != -1) {
120             outputStream.write(bbuf, 0, len);
121             totalBytes += len;
122         }
123         outputStream.flush();
124         return totalBytes;
125     }
126 }
127