1 10 11 26 27 package org.mule.providers.email.transformers; 28 29 import org.apache.commons.io.IOUtils; 30 import org.apache.commons.io.output.ByteArrayOutputStream; 31 32 import javax.activation.DataSource ; 33 34 import java.io.BufferedInputStream ; 35 import java.io.BufferedOutputStream ; 36 import java.io.ByteArrayInputStream ; 37 import java.io.IOException ; 38 import java.io.InputStream ; 39 import java.io.OutputStream ; 40 import java.io.UnsupportedEncodingException ; 41 42 51 public class ByteArrayDataSource implements DataSource 52 { 53 54 private ByteArrayOutputStream baos = null; 55 56 57 private String type = "application/octet-stream"; 58 59 66 public ByteArrayDataSource(byte[] data, String type) throws IOException 67 { 68 ByteArrayInputStream Bis = null; 69 70 try 71 { 72 Bis = new ByteArrayInputStream (data); 73 this.byteArrayDataSource(Bis, type); 74 } 75 catch (IOException ioex) 76 { 77 throw ioex; 78 } 79 finally 80 { 81 try 82 { 83 if (Bis != null) 84 { 85 Bis.close(); 86 } 87 } 88 catch (IOException ignored) 89 { 90 } 92 } 93 } 94 95 102 public ByteArrayDataSource(InputStream aIs, String type) throws IOException 103 { 104 this.byteArrayDataSource(aIs, type); 105 } 106 107 114 private void byteArrayDataSource(InputStream aIs, String type) throws IOException 115 { 116 this.type = type; 117 118 BufferedInputStream Bis = null; 119 BufferedOutputStream osWriter = null; 120 121 try 122 { 123 Bis = new BufferedInputStream (aIs); 124 baos = new ByteArrayOutputStream(); 125 osWriter = new BufferedOutputStream (baos); 126 127 IOUtils.copy(Bis, osWriter); 129 osWriter.flush(); 130 osWriter.close(); 131 } 132 catch (IOException ioex) 133 { 134 throw ioex; 135 } 136 finally 137 { 138 try 139 { 140 if (Bis != null) 141 { 142 Bis.close(); 143 } 144 if (baos != null) 145 { 146 baos.close(); 147 } 148 if (osWriter != null) 149 { 150 osWriter.close(); 151 } 152 } 153 catch (IOException ignored) 154 { 155 } 157 } 158 } 159 160 167 public ByteArrayDataSource(String data, String type) throws IOException 168 { 169 this.type = type; 170 171 try 172 { 173 baos = new ByteArrayOutputStream(); 174 175 baos.write(data.getBytes("iso-8859-1")); 179 baos.flush(); 180 baos.close(); 181 } 182 catch (UnsupportedEncodingException uex) 183 { 184 } 186 catch (IOException ignored) 187 { 188 } 190 finally 191 { 192 try 193 { 194 if (baos != null) 195 { 196 baos.close(); 197 } 198 } 199 catch (IOException ignored) 200 { 201 } 203 } 204 } 205 206 211 public String getContentType() 212 { 213 return (type == null ? "application/octet-stream" : type); 214 } 215 216 222 public InputStream getInputStream() throws IOException 223 { 224 if (baos == null) 225 { 226 throw new IOException ("no data"); 227 } 228 return new ByteArrayInputStream (baos.toByteArray()); 229 } 230 231 236 public String getName() 237 { 238 return "ByteArrayDataSource"; 239 } 240 241 247 public OutputStream getOutputStream() throws IOException 248 { 249 baos = new ByteArrayOutputStream(); 250 return baos; 251 } 252 } 253 | Popular Tags |