1 22 package org.jboss.invocation.pooled.interfaces; 23 24 import java.io.ObjectInputStream ; 25 import java.io.ObjectOutputStream ; 26 27 import java.io.ByteArrayInputStream ; 28 import java.io.ByteArrayOutputStream ; 29 import java.io.IOException ; 30 import java.io.ObjectInput ; 31 import java.io.ObjectOutput ; 32 import java.util.Arrays ; 33 34 42 public class PooledMarshalledValue 43 implements java.io.Externalizable 44 { 45 46 private static final long serialVersionUID = -1265347772036749711L; 47 48 53 private byte[] serializedForm; 54 55 58 private int hashCode; 59 60 63 public PooledMarshalledValue() 64 { 65 super(); 66 } 67 68 public PooledMarshalledValue(Object obj) throws IOException 69 { 70 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 71 ObjectOutputStream mvos = new OptimizedObjectOutputStream(baos); 72 mvos.writeObject(obj); 73 mvos.flush(); 74 serializedForm = baos.toByteArray(); 75 mvos.close(); 76 int hash = 0; 78 for (int i = 0; i < serializedForm.length; i++) 79 { 80 hash = 31 * hash + serializedForm[i]; 81 } 82 83 hashCode = hash; 84 } 85 86 public Object get() throws IOException , ClassNotFoundException 87 { 88 if (serializedForm == null) 89 return null; 90 91 ByteArrayInputStream bais = new ByteArrayInputStream (serializedForm); 92 ObjectInputStream mvis = new OptimizedObjectInputStream(bais); 93 Object retValue = mvis.readObject(); 94 mvis.close(); 95 return retValue; 96 } 97 98 public byte[] toByteArray() 99 { 100 return serializedForm; 101 } 102 103 public int size() 104 { 105 int size = serializedForm != null ? serializedForm.length : 0; 106 return size; 107 } 108 109 114 public int hashCode() 115 { 116 return hashCode; 117 } 118 119 public boolean equals(Object obj) 120 { 121 if( this == obj ) 122 return true; 123 124 boolean equals = false; 125 if( obj instanceof PooledMarshalledValue ) 126 { 127 PooledMarshalledValue mv = (PooledMarshalledValue) obj; 128 if( serializedForm == mv.serializedForm ) 129 { 130 equals = true; 131 } 132 else 133 { 134 equals = Arrays.equals(serializedForm, mv.serializedForm); 135 } 136 } 137 return equals; 138 } 139 140 153 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 154 { 155 int length = in.readInt(); 156 serializedForm = null; 157 if( length > 0 ) 158 { 159 serializedForm = new byte[length]; 160 in.readFully(serializedForm); 161 } 162 hashCode = in.readInt(); 163 } 164 165 181 public void writeExternal(ObjectOutput out) throws IOException 182 { 183 int length = serializedForm != null ? serializedForm.length : 0; 184 out.writeInt(length); 185 if( length > 0 ) 186 { 187 out.write(serializedForm); 188 } 189 out.writeInt(hashCode); 190 } 191 } 192 | Popular Tags |