diff options
Diffstat (limited to 'java/rmi/MarshalledObject.java')
-rw-r--r-- | java/rmi/MarshalledObject.java | 103 |
1 files changed, 72 insertions, 31 deletions
diff --git a/java/rmi/MarshalledObject.java b/java/rmi/MarshalledObject.java index 9ec0ace0e..e1a30f5f0 100644 --- a/java/rmi/MarshalledObject.java +++ b/java/rmi/MarshalledObject.java @@ -1,5 +1,6 @@ /* MarshalledObject.java -- - Copyright (c) 1996, 1997, 1998, 1999, 2004 Free Software Foundation, Inc. + Copyright (c) 1996, 1997, 1998, 1999, 2004, 2006 + Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -42,38 +43,68 @@ import gnu.java.rmi.RMIMarshalledObjectInputStream; import gnu.java.rmi.RMIMarshalledObjectOutputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.Serializable; /** - * FIXME - doc missing + * A <code>MarshalledObject</code> consists of a serialized object which is + * marshalled according to the RMI specification. + * <p> + * An object passed to the constructor is serialized and tagged with the needed + * URL to retrieve its class definition for remote usage. If the object is a + * remote reference its stub is serialized instead. The instance of this + * marshalled object can be later retrieved by its <code>get()</code> method. + * </p> + * + * @author unknown */ -public final class MarshalledObject implements Serializable +public final class MarshalledObject + implements Serializable { - //The following fields are from Java API Documentation "Serialized form" + // The following fields are from Java API Documentation "Serialized form" private static final long serialVersionUID = 8988374069173025854L; + byte[] objBytes; byte[] locBytes; int hash; - - public MarshalledObject(Object obj) throws java.io.IOException + + /** + * Constructs a <code>MarshalledObject</code> from the given object. + * + * @param obj the object to marshal + * @throws IOException if an I/O error during serialization occurs. + */ + public MarshalledObject(Object obj) throws IOException { ByteArrayOutputStream objStream = new ByteArrayOutputStream(); - RMIMarshalledObjectOutputStream stream = new RMIMarshalledObjectOutputStream(objStream); + RMIMarshalledObjectOutputStream stream = + new RMIMarshalledObjectOutputStream(objStream); stream.writeObject(obj); stream.flush(); objBytes = objStream.toByteArray(); locBytes = stream.getLocBytes(); - - //The following algorithm of calculating hashCode is similar to String + + // The following algorithm of calculating hashCode is similar to String hash = 0; for (int i = 0; i < objBytes.length; i++) hash = hash * 31 + objBytes[i]; - if(locBytes != null) + + if (locBytes != null) for (int i = 0; i < locBytes.length; i++) - hash = hash * 31 + locBytes[i]; + hash = hash * 31 + locBytes[i]; } - - public boolean equals(Object obj) + + /** + * Checks if the given object is equal to this marshalled object. + * + * <p>Marshalled objects are considered equal if they contain the + * same serialized object. Codebase annotations where the class + * definition can be downloaded are ignored in the equals test.</p> + * + * @param obj the object to compare. + * @return <code>true</code> if equal, <code>false</code> otherwise. + */ + public boolean equals(Object obj) { if (! (obj instanceof MarshalledObject)) return false; @@ -81,33 +112,43 @@ public final class MarshalledObject implements Serializable // hashCode even differs, don't do the time-consuming comparisons if (obj.hashCode() != hash) return false; - - MarshalledObject aobj = (MarshalledObject)obj; + + MarshalledObject aobj = (MarshalledObject) obj; if (objBytes == null || aobj.objBytes == null) return objBytes == aobj.objBytes; if (objBytes.length != aobj.objBytes.length) return false; - for (int i = 0; i < objBytes.length; i++) + for (int i = 0; i < objBytes.length; i++) { - if (objBytes[i] != aobj.objBytes[i]) - return false; + if (objBytes[i] != aobj.objBytes[i]) + return false; } // Ignore comparison of locBytes(annotation) return true; } - -public Object get() - throws java.io.IOException, java.lang.ClassNotFoundException -{ - if(objBytes == null) - return null; - RMIMarshalledObjectInputStream stream = - new RMIMarshalledObjectInputStream(objBytes, locBytes); - return stream.readObject(); -} - - public int hashCode() { + + /** + * Constructs and returns a copy of the internal serialized object. + * + * @return The deserialized object. + * + * @throws IOException if an I/O exception occurs during deserialization. + * @throws ClassNotFoundException if the class of the deserialized object + * cannot be found. + */ + public Object get() throws IOException, ClassNotFoundException + { + if (objBytes == null) + return null; + + RMIMarshalledObjectInputStream stream = + new RMIMarshalledObjectInputStream(objBytes, locBytes); + return stream.readObject(); + } + + public int hashCode() + { return hash; } - + } |