1 8 package org.jboss.axis.attachments; 9 10 import org.jboss.axis.components.image.ImageIOFactory; 11 import org.jboss.axis.utils.Messages; 12 import org.jboss.logging.Logger; 13 14 import javax.activation.DataSource ; 15 import java.awt.*; 16 import java.io.ByteArrayInputStream ; 17 import java.io.ByteArrayOutputStream ; 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 22 public class ImageDataSource implements DataSource 23 { 24 private static Logger log = Logger.getLogger(ImageDataSource.class.getName()); 25 26 public static final String CONTENT_TYPE = "image/jpeg"; 27 28 private final String name; 29 private final String contentType; 30 private byte[] data; 31 private ByteArrayInputStream is; 32 private ByteArrayOutputStream os; 33 34 public ImageDataSource(String name, Image data) 35 { 36 this(name, CONTENT_TYPE, data); 37 } 39 public ImageDataSource(String name, String contentType, Image data) 40 { 41 this.name = name; 42 this.contentType = contentType == null ? CONTENT_TYPE : contentType; 43 os = new ByteArrayOutputStream (); 44 try 45 { 46 if (data != null) 47 { 48 ImageIOFactory.getImageIO().saveImage(this.contentType, data, os); 49 } 50 } 51 catch (Exception e) 52 { 53 log.error(Messages.getMessage("exception00"), e); 54 } 55 } 57 public String getName() 58 { 59 return name; 60 } 62 public String getContentType() 63 { 64 return contentType; 65 } 67 public InputStream getInputStream() throws IOException 68 { 69 if (os.size() != 0) 70 { 71 data = os.toByteArray(); 72 os.reset(); 73 } 74 return new ByteArrayInputStream (data == null ? new byte[0] : data); 75 } 77 public OutputStream getOutputStream() throws IOException 78 { 79 if (os.size() != 0) 80 { 81 data = os.toByteArray(); 82 os.reset(); 83 } 84 return os; 85 } } | Popular Tags |