KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > listservcommands > MailDataSource


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
22 import java.io.*;
23
24 /**
25  * MailDataSource implements a typed DataSource from :
26  * an InputStream, a byte array, and a string
27  *
28  * This is used from {@link BaseCommand#generateMail}
29  *
30  * @version CVS $Revision: 1.1.2.4 $ $Date: 2004/03/15 03:54:20 $
31  * @since 2.2.0
32  */

33 public class MailDataSource implements DataSource JavaDoc {
34
35     protected static final int DEFAULT_BUF_SIZE = 0x2000;
36
37     protected static final String JavaDoc DEFAULT_ENCODING = "iso-8859-1";
38     protected static final String JavaDoc DEFAULT_NAME = "HtmlMailDataSource";
39
40     protected byte[] data; // data
41
protected String JavaDoc contentType; // content-type
42

43     /**
44      * Create a datasource from an input stream
45      */

46     public MailDataSource(InputStream inputStream, String JavaDoc contentType) throws IOException {
47         this.contentType = contentType;
48
49         ByteArrayOutputStream baos = new ByteArrayOutputStream();
50         copyStream(inputStream, baos);
51         data = baos.toByteArray();
52     }
53
54     /**
55      * Create a datasource from a byte array
56      */

57     public MailDataSource(byte[] data, String JavaDoc contentType) {
58         this.contentType = contentType;
59         this.data = data;
60     }
61
62     /**
63      * Create a datasource from a String
64      */

65     public MailDataSource(String JavaDoc data, String JavaDoc contentType) throws UnsupportedEncodingException {
66         this.contentType = contentType;
67         this.data = data.getBytes(DEFAULT_ENCODING);
68     }
69
70     /**
71      * returns the inputStream
72      */

73     public InputStream getInputStream() throws IOException {
74         if (data == null)
75             throw new IOException("no data");
76         return new ByteArrayInputStream(data);
77     }
78
79     /**
80      * Not implemented
81      */

82     public OutputStream getOutputStream() throws IOException {
83         throw new IOException("getOutputStream() isn't implemented");
84     }
85
86     /**
87      * returns the contentType for this data source
88      */

89     public String JavaDoc getContentType() {
90         return contentType;
91     }
92
93     /**
94      * returns a static moniker
95      */

96     public String JavaDoc getName() {
97         return DEFAULT_NAME;
98     }
99
100     protected static int copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
101         inputStream = new BufferedInputStream(inputStream);
102         outputStream = new BufferedOutputStream(outputStream);
103
104         byte[] bbuf = new byte[DEFAULT_BUF_SIZE];
105         int len;
106         int totalBytes = 0;
107         while ((len = inputStream.read(bbuf)) != -1) {
108             outputStream.write(bbuf, 0, len);
109             totalBytes += len;
110         }
111         outputStream.flush();
112         return totalBytes;
113     }
114 }
115
116
Popular Tags