1 38 39 import java.io.*; 40 import javax.activation.*; 41 42 53 public class ByteArrayDataSource implements DataSource { 54 private byte[] data; private String type; 57 58 public ByteArrayDataSource(InputStream is, String type) { 59 this.type = type; 60 try { 61 ByteArrayOutputStream os = new ByteArrayOutputStream(); 62 int ch; 63 64 while ((ch = is.read()) != -1) 65 os.write(ch); 68 data = os.toByteArray(); 69 70 } catch (IOException ioex) { } 71 } 72 73 74 public ByteArrayDataSource(byte[] data, String type) { 75 this.data = data; 76 this.type = type; 77 } 78 79 80 public ByteArrayDataSource(String data, String type) { 81 try { 82 this.data = data.getBytes("iso-8859-1"); 86 } catch (UnsupportedEncodingException uex) { } 87 this.type = type; 88 } 89 90 94 public InputStream getInputStream() throws IOException { 95 if (data == null) 96 throw new IOException("no data"); 97 return new ByteArrayInputStream(data); 98 } 99 100 public OutputStream getOutputStream() throws IOException { 101 throw new IOException("cannot do this"); 102 } 103 104 public String getContentType() { 105 return type; 106 } 107 108 public String getName() { 109 return "dummy"; 110 } 111 } 112 | Popular Tags |