Path: blob/master/src/java.rmi/share/classes/java/rmi/MarshalledObject.java
41152 views
/*1* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.rmi;2627import java.io.ByteArrayInputStream;28import java.io.ByteArrayOutputStream;29import java.io.IOException;30import java.io.InputStream;31import java.io.ObjectInputFilter;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.io.ObjectStreamConstants;35import java.io.OutputStream;36import java.io.Serializable;37import java.security.AccessController;38import java.security.PrivilegedAction;3940import sun.rmi.server.MarshalInputStream;41import sun.rmi.server.MarshalOutputStream;4243/**44* A <code>MarshalledObject</code> contains a byte stream with the serialized45* representation of an object given to its constructor. The <code>get</code>46* method returns a new copy of the original object, as deserialized from47* the contained byte stream. The contained object is serialized and48* deserialized with the same serialization semantics used for marshaling49* and unmarshaling parameters and return values of RMI calls: When the50* serialized form is created:51*52* <ul>53* <li> classes are annotated with a codebase URL from where the class54* can be loaded (if available), and55* <li> any remote object in the <code>MarshalledObject</code> is56* represented by a serialized instance of its stub.57* </ul>58*59* <p>When copy of the object is retrieved (via the <code>get</code> method),60* if the class is not available locally, it will be loaded from the61* appropriate location (specified the URL annotated with the class descriptor62* when the class was serialized.63*64* <p><code>MarshalledObject</code> facilitates passing objects in RMI calls65* that are not automatically deserialized immediately by the remote peer.66*67* @param <T> the type of the object contained in this68* <code>MarshalledObject</code>69*70* @author Ann Wollrath71* @author Peter Jones72* @since 1.273*/74public final class MarshalledObject<T> implements Serializable {75/**76* @serial Bytes of serialized representation. If <code>objBytes</code> is77* <code>null</code> then the object marshalled was a <code>null</code>78* reference.79*/80private byte[] objBytes = null;8182/**83* @serial Bytes of location annotations, which are ignored by84* <code>equals</code>. If <code>locBytes</code> is null, there were no85* non-<code>null</code> annotations during marshalling.86*/87private byte[] locBytes = null;8889/**90* @serial Stored hash code of contained object.91*92* @see #hashCode93*/94private int hash;9596/** Filter used when creating the instance from a stream; may be null. */97private transient ObjectInputFilter objectInputFilter = null;9899/** Indicate compatibility with 1.2 version of class. */100private static final long serialVersionUID = 8988374069173025854L;101102/**103* Creates a new <code>MarshalledObject</code> that contains the104* serialized representation of the current state of the supplied object.105* The object is serialized with the semantics used for marshaling106* parameters for RMI calls.107*108* @param obj the object to be serialized (must be serializable)109* @throws IOException if an <code>IOException</code> occurs; an110* <code>IOException</code> may occur if <code>obj</code> is not111* serializable.112* @since 1.2113*/114public MarshalledObject(T obj) throws IOException {115if (obj == null) {116hash = 13;117return;118}119120ByteArrayOutputStream bout = new ByteArrayOutputStream();121ByteArrayOutputStream lout = new ByteArrayOutputStream();122MarshalledObjectOutputStream out =123new MarshalledObjectOutputStream(bout, lout);124out.writeObject(obj);125out.flush();126objBytes = bout.toByteArray();127// locBytes is null if no annotations128locBytes = (out.hadAnnotations() ? lout.toByteArray() : null);129130/*131* Calculate hash from the marshalled representation of object132* so the hashcode will be comparable when sent between VMs.133*/134int h = 0;135for (int i = 0; i < objBytes.length; i++) {136h = 31 * h + objBytes[i];137}138hash = h;139}140141/**142* Reads in the state of the object and saves the stream's143* serialization filter to be used when the object is deserialized.144*145* @param stream the stream146* @throws IOException if an I/O error occurs147* @throws ClassNotFoundException if a class cannot be found148*/149private void readObject(ObjectInputStream stream)150throws IOException, ClassNotFoundException {151stream.defaultReadObject(); // read in all fields152objectInputFilter = stream.getObjectInputFilter();153}154155/**156* Returns a new copy of the contained marshalledobject. The internal157* representation is deserialized with the semantics used for158* unmarshaling parameters for RMI calls.159* If the MarshalledObject was read from an ObjectInputStream,160* the filter from that stream is used to deserialize the object.161*162* @return a copy of the contained object163* @throws IOException if an <code>IOException</code> occurs while164* deserializing the object from its internal representation.165* @throws ClassNotFoundException if a166* <code>ClassNotFoundException</code> occurs while deserializing167* the object from its internal representation.168* could not be found169* @since 1.2170*/171public T get() throws IOException, ClassNotFoundException {172if (objBytes == null) // must have been a null object173return null;174175ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);176// locBytes is null if no annotations177ByteArrayInputStream lin =178(locBytes == null ? null : new ByteArrayInputStream(locBytes));179MarshalledObjectInputStream in =180new MarshalledObjectInputStream(bin, lin, objectInputFilter);181@SuppressWarnings("unchecked")182T obj = (T) in.readObject();183in.close();184return obj;185}186187/**188* Return a hash code for this <code>MarshalledObject</code>.189*190* @return a hash code191*/192public int hashCode() {193return hash;194}195196/**197* Compares this <code>MarshalledObject</code> to another object.198* Returns true if and only if the argument refers to a199* <code>MarshalledObject</code> that contains exactly the same200* serialized representation of an object as this one does. The201* comparison ignores any class codebase annotation, meaning that202* two objects are equivalent if they have the same serialized203* representation <i>except</i> for the codebase of each class204* in the serialized representation.205*206* @param obj the object to compare with this <code>MarshalledObject</code>207* @return <code>true</code> if the argument contains an equivalent208* serialized object; <code>false</code> otherwise209* @since 1.2210*/211public boolean equals(Object obj) {212if (obj == this)213return true;214215if (obj != null && obj instanceof MarshalledObject) {216MarshalledObject<?> other = (MarshalledObject<?>) obj;217218// if either is a ref to null, both must be219if (objBytes == null || other.objBytes == null)220return objBytes == other.objBytes;221222// quick, easy test223if (objBytes.length != other.objBytes.length)224return false;225226//!! There is talk about adding an array comparision method227//!! at 1.2 -- if so, this should be rewritten. -arnold228for (int i = 0; i < objBytes.length; ++i) {229if (objBytes[i] != other.objBytes[i])230return false;231}232return true;233} else {234return false;235}236}237238/**239* This class is used to marshal objects for240* <code>MarshalledObject</code>. It places the location annotations241* to one side so that two <code>MarshalledObject</code>s can be242* compared for equality if they differ only in location243* annotations. Objects written using this stream should be read back244* from a <code>MarshalledObjectInputStream</code>.245*246* @see java.rmi.MarshalledObject247* @see MarshalledObjectInputStream248*/249private static class MarshalledObjectOutputStream250extends MarshalOutputStream251{252/** The stream on which location objects are written. */253private ObjectOutputStream locOut;254255/** <code>true</code> if non-<code>null</code> annotations are256* written.257*/258private boolean hadAnnotations;259260/**261* Creates a new <code>MarshalledObjectOutputStream</code> whose262* non-location bytes will be written to <code>objOut</code> and whose263* location annotations (if any) will be written to264* <code>locOut</code>.265*/266MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)267throws IOException268{269super(objOut);270this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);271this.locOut = new ObjectOutputStream(locOut);272hadAnnotations = false;273}274275/**276* Returns <code>true</code> if any non-<code>null</code> location277* annotations have been written to this stream.278*/279boolean hadAnnotations() {280return hadAnnotations;281}282283/**284* Overrides MarshalOutputStream.writeLocation implementation to write285* annotations to the location stream.286*/287protected void writeLocation(String loc) throws IOException {288hadAnnotations |= (loc != null);289locOut.writeObject(loc);290}291292293public void flush() throws IOException {294super.flush();295locOut.flush();296}297}298299/**300* The counterpart to <code>MarshalledObjectOutputStream</code>.301*302* @see MarshalledObjectOutputStream303*/304private static class MarshalledObjectInputStream305extends MarshalInputStream306{307/**308* The stream from which annotations will be read. If this is309* <code>null</code>, then all annotations were <code>null</code>.310*/311private ObjectInputStream locIn;312313/**314* Creates a new <code>MarshalledObjectInputStream</code> that315* reads its objects from <code>objIn</code> and annotations316* from <code>locIn</code>. If <code>locIn</code> is317* <code>null</code>, then all annotations will be318* <code>null</code>.319*/320@SuppressWarnings("removal")321MarshalledObjectInputStream(InputStream objIn, InputStream locIn,322ObjectInputFilter filter)323throws IOException324{325super(objIn);326this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));327if (filter != null) {328AccessController.doPrivileged((PrivilegedAction<Void>) () -> {329MarshalledObjectInputStream.this.setObjectInputFilter(filter);330if (MarshalledObjectInputStream.this.locIn != null) {331MarshalledObjectInputStream.this.locIn.setObjectInputFilter(filter);332}333return null;334});335}336}337338/**339* Overrides MarshalInputStream.readLocation to return locations from340* the stream we were given, or <code>null</code> if we were given a341* <code>null</code> location stream.342*/343protected Object readLocation()344throws IOException, ClassNotFoundException345{346return (locIn == null ? null : locIn.readObject());347}348}349350}351352353