1 21 22 27 28 package javax.mail.util; 29 30 import java.io.*; 31 import javax.activation.*; 32 import javax.mail.internet.*; 33 34 44 public class ByteArrayDataSource implements DataSource { 45 private byte[] data; private String type; private String name = ""; 48 49 59 public ByteArrayDataSource(InputStream is, String type) throws IOException { 60 ByteArrayOutputStream os = new ByteArrayOutputStream(); 61 byte[] buf = new byte[8192]; 62 int len; 63 while ((len = is.read(buf)) > 0) 64 os.write(buf, 0, len); 65 this.data = os.toByteArray(); 66 this.type = type; 67 } 68 69 76 public ByteArrayDataSource(byte[] data, String type) { 77 this.data = data; 78 this.type = type; 79 } 80 81 93 public ByteArrayDataSource(String data, String type) throws IOException { 94 String charset = null; 95 try { 96 ContentType ct = new ContentType(type); 97 charset = ct.getParameter("charset"); 98 } catch (ParseException pex) { } 99 if (charset == null) 100 charset = MimeUtility.getDefaultJavaCharset(); 101 this.data = data.getBytes(charset); 103 this.type = type; 104 } 105 106 114 public InputStream getInputStream() throws IOException { 115 if (data == null) 116 throw new IOException("no data"); 117 return new ByteArrayInputStream(data); 118 } 119 120 127 public OutputStream getOutputStream() throws IOException { 128 throw new IOException("cannot do this"); 129 } 130 131 136 public String getContentType() { 137 return type; 138 } 139 140 146 public String getName() { 147 return name; 148 } 149 150 155 public void setName(String name) { 156 this.name = name; 157 } 158 } 159 | Popular Tags |