1 17 package org.openbravo.erpCommon.businessUtility; 18 19 import java.io.*; 20 import javax.activation.*; 21 22 public class ByteArrayDataSource implements DataSource { 23 private byte[] g_data = null; 24 private String g_type = "text/plain"; 25 private String g_name = null; 26 27 public ByteArrayDataSource (InputStream is, String type) { 28 g_type = type; 29 try { 30 ByteArrayOutputStream os = new ByteArrayOutputStream (); 31 int ch; 32 33 while ((ch = is.read ()) != -1) { 34 os.write (ch); 35 } 36 g_data = os.toByteArray (); 37 } catch (IOException ioex) { 38 System.err.println("ByteArrayDataSource - " + ioex); 39 } 40 } 41 42 public ByteArrayDataSource (byte[] data, String type) { 43 g_data = data; 44 g_type = type; 45 } 46 47 public ByteArrayDataSource (String asciiData, String type) { 48 try { 49 g_data = asciiData.getBytes ("UTF-8"); 50 } catch (UnsupportedEncodingException uex) { 51 System.err.println("ByteArrayDataSource - " + uex); 52 } 53 g_type = type; 54 } 55 56 public InputStream getInputStream () throws IOException { 57 if (g_data == null) throw new IOException ("no data"); 58 return new ByteArrayInputStream (g_data); 59 } 60 61 public OutputStream getOutputStream () throws IOException { 62 throw new IOException ("cannot do this"); 63 } 64 65 public String getContentType () { 66 return g_type; 67 } 68 69 public ByteArrayDataSource setName(String name) { 70 g_name = name; 71 return this; 72 } 73 74 public String getName () { 75 if (g_name != null) return g_name; 76 return "ByteArrayDataStream " + g_type; 77 } 78 } 79 | Popular Tags |