1 16 package org.apache.axis.attachments; 17 18 import javax.activation.DataSource ; 19 import javax.mail.internet.MimeMultipart ; 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 26 public class MimeMultipartDataSource implements DataSource { 27 public static final String CONTENT_TYPE = "multipart/mixed"; 28 29 private final String name; 30 private final String contentType; 31 private byte[] data; 32 private ByteArrayOutputStream os; 33 34 public MimeMultipartDataSource(String name, MimeMultipart data) { 35 this.name = name; 36 this.contentType = data == null ? CONTENT_TYPE : data.getContentType(); 37 os = new ByteArrayOutputStream (); 38 try { 39 if (data != null) { 40 data.writeTo(os); 41 } 42 } 43 catch (Exception e) { 44 } 46 } 48 public String getName() { 49 return name; 50 } 52 public String getContentType() { 53 return contentType; 54 } 56 public InputStream getInputStream() throws IOException { 57 if (os.size() != 0) { 58 data = os.toByteArray(); 59 os.reset(); 60 } 61 return new ByteArrayInputStream (data == null ? new byte[0] : data); 62 } 64 public OutputStream getOutputStream() throws IOException { 65 if (os.size() != 0) { 66 data = os.toByteArray(); 67 os.reset(); 68 } 69 return os; 70 } } | Popular Tags |