001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets.listservcommands;
019:
020: import javax.activation.DataSource;
021:
022: import java.io.BufferedInputStream;
023: import java.io.BufferedOutputStream;
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.IOException;
027: import java.io.InputStream;
028: import java.io.OutputStream;
029: import java.io.UnsupportedEncodingException;
030:
031: /**
032: * MailDataSource implements a typed DataSource from :
033: * an InputStream, a byte array, and a string
034: *
035: * This is used from {@link BaseCommand#generateMail}
036: *
037: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
038: * @since 2.2.0
039: */
040: public class MailDataSource implements DataSource {
041:
042: protected static final int DEFAULT_BUF_SIZE = 0x2000;
043:
044: protected static final String DEFAULT_ENCODING = "iso-8859-1";
045: protected static final String DEFAULT_NAME = "HtmlMailDataSource";
046:
047: protected byte[] data; // data
048: protected String contentType; // content-type
049:
050: /**
051: * Create a datasource from an input stream
052: */
053: public MailDataSource(InputStream inputStream, String contentType)
054: throws IOException {
055: this .contentType = contentType;
056:
057: ByteArrayOutputStream baos = new ByteArrayOutputStream();
058: copyStream(inputStream, baos);
059: data = baos.toByteArray();
060: }
061:
062: /**
063: * Create a datasource from a byte array
064: */
065: public MailDataSource(byte[] data, String contentType) {
066: this .contentType = contentType;
067: this .data = data;
068: }
069:
070: /**
071: * Create a datasource from a String
072: */
073: public MailDataSource(String data, String contentType)
074: throws UnsupportedEncodingException {
075: this .contentType = contentType;
076: this .data = data.getBytes(DEFAULT_ENCODING);
077: }
078:
079: /**
080: * returns the inputStream
081: */
082: public InputStream getInputStream() throws IOException {
083: if (data == null)
084: throw new IOException("no data");
085: return new ByteArrayInputStream(data);
086: }
087:
088: /**
089: * Not implemented
090: */
091: public OutputStream getOutputStream() throws IOException {
092: throw new IOException("getOutputStream() isn't implemented");
093: }
094:
095: /**
096: * returns the contentType for this data source
097: */
098: public String getContentType() {
099: return contentType;
100: }
101:
102: /**
103: * returns a static moniker
104: */
105: public String getName() {
106: return DEFAULT_NAME;
107: }
108:
109: protected static int copyStream(InputStream inputStream,
110: 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: }
|