1 22 package org.jboss.xb.binding.sunday.xop; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.io.OutputStream ; 30 import javax.activation.DataSource ; 31 import org.jboss.xb.binding.JBossXBRuntimeException; 32 33 37 public class SimpleDataSource 38 implements DataSource 39 { 40 public final byte[] bytes; 41 public final String contentType; 42 43 public SimpleDataSource(Object o, String contentType) 44 { 45 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 46 ObjectOutputStream oos = null; 47 try 48 { 49 oos = new ObjectOutputStream (baos); 50 oos.writeObject(o); 51 } 52 catch(IOException e) 53 { 54 throw new JBossXBRuntimeException("XOP failed to serialize object " + o + ": " + e.getMessage()); 55 } 56 finally 57 { 58 if(oos != null) 59 { 60 try 61 { 62 oos.close(); 63 } 64 catch(IOException e) 65 { 66 } 67 } 68 } 69 bytes = baos.toByteArray(); 70 71 this.contentType = contentType; 72 } 73 74 public String getContentType() 75 { 76 return contentType; 77 } 78 79 public InputStream getInputStream() throws IOException 80 { 81 return new ByteArrayInputStream (bytes); 82 } 83 84 public String getName() 85 { 86 throw new UnsupportedOperationException ("getName is not implemented."); 87 } 88 89 public OutputStream getOutputStream() throws IOException 90 { 91 throw new UnsupportedOperationException ("getOutputStream is not implemented."); 92 } 93 } 94 | Popular Tags |