1 package com.espada.bugtracker.util; 2 3 import java.io.*; 4 import javax.activation.DataSource ; 5 6 public class ByteArrayDataSource 7 implements DataSource 8 { 9 10 public String getContentType() 11 { 12 if(type == null) 13 return "application/octet-stream"; 14 else 15 return type; 16 } 17 18 public InputStream getInputStream() 19 throws IOException 20 { 21 if(data == null) 22 throw new IOException("no data"); 23 else 24 return new ByteArrayInputStream(data); 25 } 26 27 public String getName() 28 { 29 return "ByteArrayDataSource"; 30 } 31 32 public OutputStream getOutputStream() 33 throws IOException 34 { 35 baos = new ByteArrayOutputStream(); 36 return baos; 37 } 38 39 public ByteArrayDataSource(byte abyte0[], String s) 40 { 41 data = abyte0; 42 type = s; 43 } 44 45 public ByteArrayDataSource(InputStream inputstream, String s) 46 { 47 type = s; 48 try 49 { 50 ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream(); 51 BufferedInputStream bufferedinputstream = new BufferedInputStream(inputstream); 52 BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(bytearrayoutputstream); 53 int i; 54 while((i = bufferedinputstream.read()) != -1) 55 bufferedoutputstream.write(i); 56 data = bytearrayoutputstream.toByteArray(); 57 } 58 catch(IOException ioexception) { } 59 } 60 61 public ByteArrayDataSource(String s, String s1) 62 { 63 type = s1; 64 try 65 { 66 data = s.getBytes("iso-8859-1"); 67 } 68 catch(UnsupportedEncodingException unsupportedencodingexception) { } 69 } 70 71 private byte data[]; 72 private String type; 73 private ByteArrayOutputStream baos; 74 } 75 | Popular Tags |