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