1 7 8 package java.rmi; 9 10 import java.io.*; 11 import sun.rmi.server.MarshalInputStream; 12 import sun.rmi.server.MarshalOutputStream; 13 14 43 public final class MarshalledObject implements Serializable { 44 49 private byte[] objBytes = null; 50 51 56 private byte[] locBytes = null; 57 58 63 private int hash; 64 65 66 private static final long serialVersionUID = 8988374069173025854L; 67 68 80 public MarshalledObject(Object obj) 81 throws java.io.IOException 82 { 83 if (obj == null) { 84 hash = 13; 85 return; 86 } 87 88 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 89 ByteArrayOutputStream lout = new ByteArrayOutputStream(); 90 MarshalledObjectOutputStream out = 91 new MarshalledObjectOutputStream(bout, lout); 92 out.writeObject(obj); 93 out.flush(); 94 objBytes = bout.toByteArray(); 95 locBytes = (out.hadAnnotations() ? lout.toByteArray() : null); 97 98 102 int h = 0; 103 for (int i = 0; i < objBytes.length; i++) { 104 h = 31 * h + objBytes[i]; 105 } 106 hash = h; 107 } 108 109 123 public Object get() 124 throws java.io.IOException , java.lang.ClassNotFoundException 125 { 126 if (objBytes == null) return null; 128 129 ByteArrayInputStream bin = new ByteArrayInputStream(objBytes); 130 ByteArrayInputStream lin = 132 (locBytes == null ? null : new ByteArrayInputStream(locBytes)); 133 MarshalledObjectInputStream in = 134 new MarshalledObjectInputStream(bin, lin); 135 Object obj = in.readObject(); 136 in.close(); 137 return obj; 138 } 139 140 145 public int hashCode() { 146 return hash; 147 } 148 149 164 public boolean equals(Object obj) { 165 if (obj == this) 166 return true; 167 168 if (obj != null && obj instanceof MarshalledObject ) { 169 MarshalledObject other = (MarshalledObject ) obj; 170 171 if (objBytes == null || other.objBytes == null) 173 return objBytes == other.objBytes; 174 175 if (objBytes.length != other.objBytes.length) 177 return false; 178 179 for (int i = 0; i < objBytes.length; ++i) { 182 if (objBytes[i] != other.objBytes[i]) 183 return false; 184 } 185 return true; 186 } else { 187 return false; 188 } 189 } 190 191 202 private static class MarshalledObjectOutputStream 203 extends MarshalOutputStream 204 { 205 206 private ObjectOutputStream locOut; 207 208 211 private boolean hadAnnotations; 212 213 219 MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut) 220 throws IOException 221 { 222 super(objOut); 223 this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2); 224 this.locOut = new ObjectOutputStream(locOut); 225 hadAnnotations = false; 226 } 227 228 232 boolean hadAnnotations() { 233 return hadAnnotations; 234 } 235 236 240 protected void writeLocation(String loc) throws IOException { 241 hadAnnotations |= (loc != null); 242 locOut.writeObject(loc); 243 } 244 245 246 public void flush() throws IOException { 247 super.flush(); 248 locOut.flush(); 249 } 250 } 251 252 257 private static class MarshalledObjectInputStream 258 extends MarshalInputStream 259 { 260 264 private ObjectInputStream locIn; 265 266 273 MarshalledObjectInputStream(InputStream objIn, InputStream locIn) 274 throws IOException 275 { 276 super(objIn); 277 this.locIn = (locIn == null ? null : new ObjectInputStream(locIn)); 278 } 279 280 285 protected Object readLocation() 286 throws IOException, ClassNotFoundException 287 { 288 return (locIn == null ? null : locIn.readObject()); 289 } 290 } 291 292 } 293 | Popular Tags |