1 17 18 package org.apache.james.transport.mailets.listservcommands; 19 20 21 import javax.activation.DataSource ; 22 import java.io.*; 23 24 33 public class MailDataSource implements DataSource { 34 35 protected static final int DEFAULT_BUF_SIZE = 0x2000; 36 37 protected static final String DEFAULT_ENCODING = "iso-8859-1"; 38 protected static final String DEFAULT_NAME = "HtmlMailDataSource"; 39 40 protected byte[] data; protected String contentType; 43 46 public MailDataSource(InputStream inputStream, String contentType) throws IOException { 47 this.contentType = contentType; 48 49 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 50 copyStream(inputStream, baos); 51 data = baos.toByteArray(); 52 } 53 54 57 public MailDataSource(byte[] data, String contentType) { 58 this.contentType = contentType; 59 this.data = data; 60 } 61 62 65 public MailDataSource(String data, String contentType) throws UnsupportedEncodingException { 66 this.contentType = contentType; 67 this.data = data.getBytes(DEFAULT_ENCODING); 68 } 69 70 73 public InputStream getInputStream() throws IOException { 74 if (data == null) 75 throw new IOException("no data"); 76 return new ByteArrayInputStream(data); 77 } 78 79 82 public OutputStream getOutputStream() throws IOException { 83 throw new IOException("getOutputStream() isn't implemented"); 84 } 85 86 89 public String getContentType() { 90 return contentType; 91 } 92 93 96 public String 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 |