1 package org.apache.turbine.util.mail; 2 3 18 19 import java.io.BufferedInputStream ; 20 import java.io.BufferedOutputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.ByteArrayOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.io.OutputStream ; 26 import java.io.UnsupportedEncodingException ; 27 import javax.activation.DataSource ; 28 29 41 public class ByteArrayDataSource 42 implements DataSource 43 { 44 45 private byte[] data; 46 47 48 private String type; 49 50 private ByteArrayOutputStream baos; 51 52 58 public ByteArrayDataSource(byte[] data, 59 String type) 60 { 61 this.data = data; 62 this.type = type; 63 } 64 65 71 public ByteArrayDataSource(InputStream is, 72 String type) 73 { 74 this.type = type; 75 try 76 { 77 int ch; 78 79 ByteArrayOutputStream os = new ByteArrayOutputStream (); 80 BufferedInputStream isReader = new BufferedInputStream (is); 81 BufferedOutputStream osWriter = new BufferedOutputStream (os); 82 83 while ((ch = isReader.read()) != -1) 84 { 85 osWriter.write(ch); 86 } 87 data = os.toByteArray(); 88 } 89 catch (IOException ioex) 90 { 91 } 93 } 94 95 101 public ByteArrayDataSource(String data, 102 String type) 103 { 104 this.type = type; 105 try 106 { 107 this.data = data.getBytes("iso-8859-1"); 111 } 112 catch (UnsupportedEncodingException uex) 113 { 114 } 116 } 117 118 123 public String getContentType() 124 { 125 if (type == null) 126 return "application/octet-stream"; 127 else 128 return type; 129 } 130 131 137 public InputStream getInputStream() 138 throws IOException 139 { 140 if (data == null) 141 throw new IOException ("no data"); 142 return new ByteArrayInputStream (data); 143 } 144 145 150 public String getName() 151 { 152 return "ByteArrayDataSource"; 153 } 154 155 161 public OutputStream getOutputStream() 162 throws IOException 163 { 164 baos = new ByteArrayOutputStream (); 165 return baos; 166 } 167 } 168 | Popular Tags |