1 14 package org.compiere.util; 15 16 import java.io.*; 17 import javax.activation.*; 18 19 29 public class ByteArrayDataSource 30 implements DataSource 31 { 32 37 public ByteArrayDataSource (InputStream is, String type) 38 { 39 m_type = type; 40 try 41 { 42 ByteArrayOutputStream os = new ByteArrayOutputStream (); 43 int ch; 44 45 while ((ch = is.read ()) != -1) 46 { 47 os.write (ch); 50 } 51 m_data = os.toByteArray (); 52 } 53 catch (IOException ioex) 54 { 55 System.err.println("ByteArrayDataSource - " + ioex); 56 } 57 } 59 64 public ByteArrayDataSource (byte[] data, String type) 65 { 66 m_data = data; 67 m_type = type; 68 } 70 77 public ByteArrayDataSource (String asciiData, String type) 78 { 79 try 80 { 81 m_data = asciiData.getBytes ("iso-8859-1"); 82 } 83 catch (UnsupportedEncodingException uex) 84 { 85 System.err.println("ByteArrayDataSource - " + uex); 86 } 87 m_type = type; 88 } 90 91 private byte[] m_data = null; 92 93 private String m_type = "text/plain"; 94 95 private String m_name = null; 96 97 102 public InputStream getInputStream () 103 throws IOException 104 { 105 if (m_data == null) 106 throw new IOException ("no data"); 107 return new ByteArrayInputStream (m_data); 109 } 111 116 public OutputStream getOutputStream () 117 throws IOException 118 { 119 throw new IOException ("cannot do this"); 120 } 122 126 public String getContentType () 127 { 128 return m_type; 129 } 131 136 public ByteArrayDataSource setName(String name) 137 { 138 m_name = name; 139 return this; 140 } 142 146 public String getName () 147 { 148 if (m_name != null) 149 return m_name; 150 return "ByteArrayDataStream " + m_type; 151 } 153 } | Popular Tags |