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