1 16 package org.apache.axis2.attachments; 17 18 import javax.activation.DataSource ; 19 import java.awt.*; 20 import java.io.*; 21 22 public class ImageDataSource implements DataSource { 23 24 public static final String CONTENT_TYPE = "image/jpeg"; 25 26 private final String name; 27 28 private final String contentType; 29 30 private byte[] data; 31 32 private ByteArrayOutputStream os; 33 34 public ImageDataSource(String name, Image data) { 35 this(name, CONTENT_TYPE, data); 36 } 38 public ImageDataSource(String name, String contentType, Image data) { 39 this.name = name; 40 this.contentType = contentType == null ? CONTENT_TYPE : contentType; 41 os = new ByteArrayOutputStream(); 42 try { 43 if (data != null) { 44 new JDK13IO().saveImage(this.contentType, data, os); 45 } 46 } catch (Exception e) { 47 } 49 } 50 51 public String getName() { 52 return name; 53 } 55 public String getContentType() { 56 return contentType; 57 } 59 public InputStream getInputStream() throws IOException { 60 if (os.size() != 0) { 61 data = os.toByteArray(); 62 os.reset(); 63 } 64 return new ByteArrayInputStream(data == null ? new byte[0] : data); 65 } 67 public OutputStream getOutputStream() throws IOException { 68 if (os.size() != 0) { 69 data = os.toByteArray(); 70 os.reset(); 71 } 72 return os; 73 } } | Popular Tags |