1 22 package org.objectweb.petals.jbi.messaging; 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.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.Serializable ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import javax.activation.DataHandler ; 36 import javax.mail.util.ByteArrayDataSource; 37 import javax.xml.transform.Source ; 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.stream.StreamResult ; 40 import javax.xml.transform.stream.StreamSource ; 41 42 import org.objectweb.petals.util.SourceHelper; 43 44 49 public class MessageExchangeSerializer implements Serializable { 50 51 private static final long serialVersionUID = 1L; 52 53 private MessageExchangeSerializer() { 54 super(); 55 } 56 57 protected static MessageExchangeSerializer instance = new MessageExchangeSerializer(); 58 59 public static MessageExchangeSerializer instance() { 60 return instance; 61 } 62 63 public void serializeContent(Source content, ObjectOutputStream s) 64 throws Exception { 65 synchronized (content) { 66 StreamResult sresult = new StreamResult (s); 67 Transformer transform = SourceHelper.getTransformer(); 68 transform.transform(content, sresult); 69 } 70 } 71 72 public Source deserializeContent(ObjectInputStream s) throws IOException { 73 StreamSource source = new StreamSource (); 74 75 byte[] buff = new byte[1024]; 76 ByteArrayOutputStream outputStream = new ByteArrayOutputStream (); 77 int count = 0; 78 int n = 0; 79 while (-1 != (n = s.read(buff))) { 80 outputStream.write(buff, 0, n); 81 count += n; 82 } 83 84 ByteArrayInputStream in = new ByteArrayInputStream (outputStream 85 .toByteArray()); 86 source.setInputStream(in); 87 return source; 88 } 89 90 103 public void serializeAttachments(Map <String , DataHandler > attachments, 104 ObjectOutputStream s) throws Exception { 105 s.writeInt(attachments.size()); 106 107 for (Iterator iter = attachments.keySet().iterator(); iter.hasNext();) { 108 109 String attachmentName = (String ) iter.next(); 110 DataHandler attachment = attachments.get(attachmentName); 111 112 InputStream binary = attachment.getInputStream(); 113 synchronized (binary) { 114 115 int available = binary.available(); 116 117 s.writeUTF(attachmentName); 119 s.writeUTF(attachment.getContentType()); 120 s.writeUTF(attachment.getName()); 121 s.writeInt(available); 122 123 byte[] content = new byte[available]; 125 binary.read(content); 126 s.write(content); 127 s.flush(); 128 } 129 } 130 } 131 132 145 public Map <String , DataHandler > deserializeAttachments(ObjectInputStream s) 146 throws IOException { 147 HashMap <String , DataHandler > attachments = new HashMap <String , DataHandler >(); 148 149 int attachmentsCount = s.readInt(); 150 151 for (int i = 0; i < attachmentsCount; i++) { 152 String attachmentName = s.readUTF(); 154 String contentType = s.readUTF(); 155 String name = s.readUTF(); 156 int available = s.readInt(); 157 158 byte[] content = new byte[available]; 160 s.readFully(content); 161 ByteArrayDataSource dataSource = new ByteArrayDataSource(content, 162 contentType); 163 dataSource.setName(name); 164 165 DataHandler dataHandler = new DataHandler (dataSource); 167 168 attachments.put(attachmentName, dataHandler); 169 } 170 return attachments; 171 } 172 173 } 174 | Popular Tags |