src/java.rmi/share/classes/java/rmi/MarshalledObject.java
author rriggs
Wed, 02 Oct 2019 13:57:03 -0400
changeset 58446 5c83830390ba
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231663: Incorrect GPL header in some RMI/SQL package-info.java files Reviewed-by: bpb, iris, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.rmi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.InputStream;
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    32
import java.io.ObjectInputFilter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectStreamConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.Serializable;
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    38
import java.security.AccessController;
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    39
import java.security.PrivilegedAction;
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.rmi.server.MarshalInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.rmi.server.MarshalOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * A <code>MarshalledObject</code> contains a byte stream with the serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * representation of an object given to its constructor.  The <code>get</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * method returns a new copy of the original object, as deserialized from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the contained byte stream.  The contained object is serialized and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * deserialized with the same serialization semantics used for marshaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and unmarshaling parameters and return values of RMI calls:  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * serialized form is created:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <li> classes are annotated with a codebase URL from where the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *      can be loaded (if available), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <li> any remote object in the <code>MarshalledObject</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *      represented by a serialized instance of its stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <p>When copy of the object is retrieved (via the <code>get</code> method),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * if the class is not available locally, it will be loaded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * appropriate location (specified the URL annotated with the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * when the class was serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p><code>MarshalledObject</code> facilitates passing objects in RMI calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * that are not automatically deserialized immediately by the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @param <T> the type of the object contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <code>MarshalledObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author  Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * @author  Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
public final class MarshalledObject<T> implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @serial Bytes of serialized representation.  If <code>objBytes</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <code>null</code> then the object marshalled was a <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private byte[] objBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @serial Bytes of location annotations, which are ignored by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <code>equals</code>.  If <code>locBytes</code> is null, there were no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * non-<code>null</code> annotations during marshalling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private byte[] locBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @serial Stored hash code of contained object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @see #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    private int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    97
    /** Filter used when creating the instance from a stream; may be null. */
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    98
    private transient ObjectInputFilter objectInputFilter = null;
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
    99
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** Indicate compatibility with 1.2 version of class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private static final long serialVersionUID = 8988374069173025854L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a new <code>MarshalledObject</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * serialized representation of the current state of the supplied object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * The object is serialized with the semantics used for marshaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * parameters for RMI calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param obj the object to be serialized (must be serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @exception IOException if an <code>IOException</code> occurs; an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <code>IOException</code> may occur if <code>obj</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public MarshalledObject(T obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            hash = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ByteArrayOutputStream lout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        MarshalledObjectOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            new MarshalledObjectOutputStream(bout, lout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        out.writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        objBytes = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        // locBytes is null if no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        locBytes = (out.hadAnnotations() ? lout.toByteArray() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
         * Calculate hash from the marshalled representation of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
         * so the hashcode will be comparable when sent between VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int i = 0; i < objBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            h = 31 * h + objBytes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   143
     * Reads in the state of the object and saves the stream's
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   144
     * serialization filter to be used when the object is deserialized.
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   145
     *
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   146
     * @param stream the stream
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   147
     * @throws IOException if an I/O error occurs
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   148
     * @throws ClassNotFoundException if a class cannot be found
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   149
     */
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   150
    private void readObject(ObjectInputStream stream)
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   151
        throws IOException, ClassNotFoundException {
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   152
        stream.defaultReadObject();     // read in all fields
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   153
        objectInputFilter = stream.getObjectInputFilter();
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   154
    }
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   155
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   156
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns a new copy of the contained marshalledobject.  The internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * representation is deserialized with the semantics used for
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   159
     * unmarshaling parameters for RMI calls.
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   160
     * If the MarshalledObject was read from an ObjectInputStream,
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   161
     * the filter from that stream is used to deserialize the object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @return a copy of the contained object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @exception IOException if an <code>IOException</code> occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * deserializing the object from its internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @exception ClassNotFoundException if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <code>ClassNotFoundException</code> occurs while deserializing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * object from its internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public T get() throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (objBytes == null)   // must have been a null object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        // locBytes is null if no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ByteArrayInputStream lin =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            (locBytes == null ? null : new ByteArrayInputStream(locBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        MarshalledObjectInputStream in =
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   181
            new MarshalledObjectInputStream(bin, lin, objectInputFilter);
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
   182
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        T obj = (T) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Return a hash code for this <code>MarshalledObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return a hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Compares this <code>MarshalledObject</code> to another object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Returns true if and only if the argument refers to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * <code>MarshalledObject</code> that contains exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * serialized representation of an object as this one does. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * comparison ignores any class codebase annotation, meaning that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * two objects are equivalent if they have the same serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * representation <i>except</i> for the codebase of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * in the serialized representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param obj the object to compare with this <code>MarshalledObject</code>
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   208
     * @return <code>true</code> if the argument contains an equivalent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * serialized object; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (obj != null && obj instanceof MarshalledObject) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
   217
            MarshalledObject<?> other = (MarshalledObject<?>) obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // if either is a ref to null, both must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (objBytes == null || other.objBytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                return objBytes == other.objBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            // quick, easy test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if (objBytes.length != other.objBytes.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            //!! There is talk about adding an array comparision method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            //!! at 1.2 -- if so, this should be rewritten.  -arnold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            for (int i = 0; i < objBytes.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                if (objBytes[i] != other.objBytes[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * This class is used to marshal objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <code>MarshalledObject</code>.  It places the location annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * to one side so that two <code>MarshalledObject</code>s can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * compared for equality if they differ only in location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * annotations.  Objects written using this stream should be read back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * from a <code>MarshalledObjectInputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @see java.rmi.MarshalledObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see MarshalledObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    private static class MarshalledObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        extends MarshalOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /** The stream on which location objects are written. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        private ObjectOutputStream locOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        /** <code>true</code> if non-<code>null</code> annotations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         *  written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        private boolean hadAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * Creates a new <code>MarshalledObjectOutputStream</code> whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * non-location bytes will be written to <code>objOut</code> and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         * location annotations (if any) will be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
         * <code>locOut</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            super(objOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            this.locOut = new ObjectOutputStream(locOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            hadAnnotations = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * Returns <code>true</code> if any non-<code>null</code> location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * annotations have been written to this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        boolean hadAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            return hadAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * Overrides MarshalOutputStream.writeLocation implementation to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * annotations to the location stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        protected void writeLocation(String loc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            hadAnnotations |= (loc != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            locOut.writeObject(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            super.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            locOut.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * The counterpart to <code>MarshalledObjectOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @see MarshalledObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private static class MarshalledObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        extends MarshalInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * The stream from which annotations will be read.  If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * <code>null</code>, then all annotations were <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        private ObjectInputStream locIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * Creates a new <code>MarshalledObjectInputStream</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * reads its objects from <code>objIn</code> and annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * from <code>locIn</code>.  If <code>locIn</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * <code>null</code>, then all annotations will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         */
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   321
        MarshalledObjectInputStream(InputStream objIn, InputStream locIn,
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   322
                    ObjectInputFilter filter)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            super(objIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));
43211
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   327
            if (filter != null) {
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   328
                AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   329
                    MarshalledObjectInputStream.this.setObjectInputFilter(filter);
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   330
                    if (MarshalledObjectInputStream.this.locIn != null) {
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   331
                        MarshalledObjectInputStream.this.locIn.setObjectInputFilter(filter);
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   332
                    }
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   333
                    return null;
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   334
                });
f264afd5082c 8156802: Better constraint checking
rriggs
parents: 25859
diff changeset
   335
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * Overrides MarshalInputStream.readLocation to return locations from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * the stream we were given, or <code>null</code> if we were given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * <code>null</code> location stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        protected Object readLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return (locIn == null ? null : locIn.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
}