1 7 package org.jboss.remoting.marshal.http; 8 9 import java.io.BufferedReader ; 10 import java.io.ByteArrayInputStream ; 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.io.InputStreamReader ; 15 import java.io.StreamCorruptedException ; 16 import java.util.Map ; 17 import org.jboss.logging.Logger; 18 import org.jboss.remoting.marshal.UnMarshaller; 19 import org.jboss.remoting.marshal.serializable.SerializableUnMarshaller; 20 21 24 public class HTTPUnMarshaller extends SerializableUnMarshaller 25 { 26 static final long serialVersionUID = 1085086661310576768L; 27 28 public final static String DATATYPE = "http"; 29 30 protected final Logger log = Logger.getLogger(getClass()); 31 32 41 public Object read(InputStream inputStream, Map metadata) throws IOException , ClassNotFoundException 42 { 43 int contentLength = -1; 44 Object ret = null; 45 int bufferSize = 1024; 46 byte[] byteBuffer = new byte[bufferSize]; 47 ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream (); 48 49 if(metadata != null) 51 { 52 Object value = metadata.get("Content-Length"); 53 if(value == null) 54 { 55 value = metadata.get("content-length"); 56 } 57 if(value != null) 58 { 59 if(value instanceof String ) 60 { 61 try 62 { 63 contentLength = Integer.parseInt((String ) value); 64 } 65 catch(NumberFormatException e) 66 { 67 log.warn("Error converting Content-Length value (" + value + ") from metadata into int value."); 68 } 69 } 70 else 71 { 72 } 74 } 75 } 76 77 int pointer = 0; 78 int amtRead = inputStream.read(byteBuffer); 79 while(amtRead > 0) 80 { 81 byteOutputStream.write(byteBuffer, pointer, amtRead); 82 if(amtRead < bufferSize && byteOutputStream.size() >= contentLength) 83 { 84 break; 86 } 87 amtRead = inputStream.read(byteBuffer); 88 } 89 90 byteOutputStream.flush(); 91 92 byte[] totalByteArray = byteOutputStream.toByteArray(); 93 94 if(totalByteArray.length == 0) 95 { 96 return null; 98 } 99 100 try 101 { 102 return super.read(new ByteArrayInputStream (totalByteArray), metadata); 103 } 104 catch(StreamCorruptedException sce) 105 { 106 107 try 108 { 109 110 BufferedReader reader = new BufferedReader (new InputStreamReader (new ByteArrayInputStream (totalByteArray))); 111 StringBuffer buffer = new StringBuffer (); 112 String str = null; 113 while((str = reader.readLine()) != null) 114 { 115 buffer.append(str); 116 } 117 reader.close(); 118 119 ret = buffer.toString(); 120 121 } 122 catch(Exception e) 123 { 124 log.error("Can not unmarshall inputstream. Tried to unmarshall as both an object and string type.", e); 125 throw new IOException ("Can not unmarshall inputstream."); 126 } 127 } 128 return ret; 129 130 } 131 132 public UnMarshaller cloneUnMarshaller() throws CloneNotSupportedException 133 { 134 HTTPUnMarshaller unmarshaller = new HTTPUnMarshaller(); 135 unmarshaller.setClassLoader(this.customClassLoader); 136 return unmarshaller; 137 } 138 139 } | Popular Tags |