Source for java.rmi.MarshalledObject

   1: /* MarshalledObject.java --
   2:    Copyright (c) 1996, 1997, 1998, 1999, 2004, 2006 
   3:    Free Software Foundation, Inc.
   4: 
   5: This file is part of GNU Classpath.
   6: 
   7: GNU Classpath is free software; you can redistribute it and/or modify
   8: it under the terms of the GNU General Public License as published by
   9: the Free Software Foundation; either version 2, or (at your option)
  10: any later version.
  11:  
  12: GNU Classpath is distributed in the hope that it will be useful, but
  13: WITHOUT ANY WARRANTY; without even the implied warranty of
  14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15: General Public License for more details.
  16: 
  17: You should have received a copy of the GNU General Public License
  18: along with GNU Classpath; see the file COPYING.  If not, write to the
  19: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20: 02110-1301 USA.
  21: 
  22: Linking this library statically or dynamically with other modules is
  23: making a combined work based on this library.  Thus, the terms and
  24: conditions of the GNU General Public License cover the whole
  25: combination.
  26: 
  27: As a special exception, the copyright holders of this library give you
  28: permission to link this library with independent modules to produce an
  29: executable, regardless of the license terms of these independent
  30: modules, and to copy and distribute the resulting executable under
  31: terms of your choice, provided that you also meet, for each linked
  32: independent module, the terms and conditions of the license of that
  33: module.  An independent module is a module which is not derived from
  34: or based on this library.  If you modify this library, you may extend
  35: this exception to your version of the library, but you are not
  36: obligated to do so.  If you do not wish to do so, delete this
  37: exception statement from your version. */
  38: 
  39: 
  40: package java.rmi;
  41: 
  42: import gnu.java.rmi.RMIMarshalledObjectInputStream;
  43: import gnu.java.rmi.RMIMarshalledObjectOutputStream;
  44: 
  45: import java.io.ByteArrayOutputStream;
  46: import java.io.IOException;
  47: import java.io.Serializable;
  48: 
  49: /**
  50:  * A <code>MarshalledObject</code> consists of a serialized object which is
  51:  * marshalled according to the RMI specification.
  52:  * <p>
  53:  * An object passed to the constructor is serialized and tagged with the needed
  54:  * URL to retrieve its class definition for remote usage. If the object is a 
  55:  * remote reference its stub is serialized instead. The instance of this 
  56:  * marshalled object can be later retrieved by its <code>get()</code> method.
  57:  * </p>
  58:  *
  59:  * @author unknown
  60:  */
  61: public final class MarshalledObject
  62:   implements Serializable
  63: {
  64:   // The following fields are from Java API Documentation "Serialized form"
  65:   private static final long serialVersionUID = 8988374069173025854L;
  66: 
  67:   byte[] objBytes;
  68:   byte[] locBytes;
  69:   int hash;
  70: 
  71:   /**
  72:    * Constructs a <code>MarshalledObject</code> from the given object.
  73:    * 
  74:    * @param obj the object to marshal
  75:    * @throws IOException if an I/O error during serialization occurs.
  76:    */
  77:   public MarshalledObject(Object obj) throws IOException
  78:   {
  79:     ByteArrayOutputStream objStream = new ByteArrayOutputStream();
  80:     RMIMarshalledObjectOutputStream stream = 
  81:       new RMIMarshalledObjectOutputStream(objStream);
  82:     stream.writeObject(obj);
  83:     stream.flush();
  84:     objBytes = objStream.toByteArray();
  85:     locBytes = stream.getLocBytes();
  86: 
  87:     // The following algorithm of calculating hashCode is similar to String
  88:     hash = 0;
  89:     for (int i = 0; i < objBytes.length; i++)
  90:       hash = hash * 31 + objBytes[i];
  91:     
  92:     if (locBytes != null)
  93:       for (int i = 0; i < locBytes.length; i++)
  94:         hash = hash * 31 + locBytes[i];
  95:   }
  96: 
  97:   /**
  98:    * Checks if the given object is equal to this marshalled object.
  99:    * 
 100:    * <p>Marshalled objects are considered equal if they contain the
 101:    * same serialized object. Codebase annotations where the class 
 102:    * definition can be downloaded are ignored in the equals test.</p>
 103:    *
 104:    * @param obj the object to compare.   
 105:    * @return <code>true</code> if equal, <code>false</code> otherwise.
 106:    */
 107:   public boolean equals(Object obj)
 108:   {
 109:     if (! (obj instanceof MarshalledObject))
 110:       return false;
 111: 
 112:     // hashCode even differs, don't do the time-consuming comparisons
 113:     if (obj.hashCode() != hash)
 114:       return false;
 115: 
 116:     MarshalledObject aobj = (MarshalledObject) obj;
 117:     if (objBytes == null || aobj.objBytes == null)
 118:       return objBytes == aobj.objBytes;
 119:     if (objBytes.length != aobj.objBytes.length)
 120:       return false;
 121:     for (int i = 0; i < objBytes.length; i++)
 122:       {
 123:         if (objBytes[i] != aobj.objBytes[i])
 124:           return false;
 125:       }
 126:     // Ignore comparison of locBytes(annotation)
 127:     return true;
 128:   }
 129: 
 130:   /**
 131:    * Constructs and returns a copy of the internal serialized object.
 132:    * 
 133:    * @return The deserialized object.
 134:    * 
 135:    * @throws IOException if an I/O exception occurs during deserialization.
 136:    * @throws ClassNotFoundException if the class of the deserialized object 
 137:    * cannot be found.
 138:    */
 139:   public Object get() throws IOException, ClassNotFoundException
 140:   {
 141:     if (objBytes == null)
 142:       return null;
 143:     
 144:     RMIMarshalledObjectInputStream stream = 
 145:       new RMIMarshalledObjectInputStream(objBytes, locBytes);
 146:     return stream.readObject();
 147:   }
 148: 
 149:   public int hashCode()
 150:   {
 151:     return hash;
 152:   }
 153: 
 154: }