jdk/src/share/classes/java/rmi/MarshalledObject.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 14342 8435a30053c1
child 23010 6dadb192ad81
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11117
diff changeset
     2
 * Copyright (c) 1997, 2011, 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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectStreamConstants;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.rmi.server.MarshalInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.rmi.server.MarshalOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * A <code>MarshalledObject</code> contains a byte stream with the serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * representation of an object given to its constructor.  The <code>get</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * method returns a new copy of the original object, as deserialized from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the contained byte stream.  The contained object is serialized and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * deserialized with the same serialization semantics used for marshaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * and unmarshaling parameters and return values of RMI calls:  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * serialized form is created:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <li> classes are annotated with a codebase URL from where the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *      can be loaded (if available), and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li> any remote object in the <code>MarshalledObject</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *      represented by a serialized instance of its stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>When copy of the object is retrieved (via the <code>get</code> method),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * if the class is not available locally, it will be loaded from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * appropriate location (specified the URL annotated with the class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * when the class was serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p><code>MarshalledObject</code> facilitates passing objects in RMI calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * that are not automatically deserialized immediately by the remote peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @param <T> the type of the object contained in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <code>MarshalledObject</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @author  Ann Wollrath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * @author  Peter Jones
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public final class MarshalledObject<T> implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @serial Bytes of serialized representation.  If <code>objBytes</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>null</code> then the object marshalled was a <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private byte[] objBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @serial Bytes of location annotations, which are ignored by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <code>equals</code>.  If <code>locBytes</code> is null, there were no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * non-<code>null</code> annotations during marshalling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private byte[] locBytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @serial Stored hash code of contained object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @see #hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Indicate compatibility with 1.2 version of class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final long serialVersionUID = 8988374069173025854L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Creates a new <code>MarshalledObject</code> that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * serialized representation of the current state of the supplied object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The object is serialized with the semantics used for marshaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * parameters for RMI calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param obj the object to be serialized (must be serializable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @exception IOException if an <code>IOException</code> occurs; an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <code>IOException</code> may occur if <code>obj</code> is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * serializable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public MarshalledObject(T obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            hash = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        ByteArrayOutputStream lout = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        MarshalledObjectOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            new MarshalledObjectOutputStream(bout, lout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        out.writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        objBytes = bout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // locBytes is null if no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        locBytes = (out.hadAnnotations() ? lout.toByteArray() : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         * Calculate hash from the marshalled representation of object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         * so the hashcode will be comparable when sent between VMs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int h = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (int i = 0; i < objBytes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            h = 31 * h + objBytes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        hash = h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns a new copy of the contained marshalledobject.  The internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * representation is deserialized with the semantics used for
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   138
     * unmarshaling parameters for RMI calls.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @return a copy of the contained object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @exception IOException if an <code>IOException</code> occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * deserializing the object from its internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @exception ClassNotFoundException if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>ClassNotFoundException</code> occurs while deserializing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * object from its internal representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public T get() throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (objBytes == null)   // must have been a null object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // locBytes is null if no annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        ByteArrayInputStream lin =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            (locBytes == null ? null : new ByteArrayInputStream(locBytes));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        MarshalledObjectInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            new MarshalledObjectInputStream(bin, lin);
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
   159
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        T obj = (T) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Return a hash code for this <code>MarshalledObject</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return a hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Compares this <code>MarshalledObject</code> to another object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns true if and only if the argument refers to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <code>MarshalledObject</code> that contains exactly the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * serialized representation of an object as this one does. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * comparison ignores any class codebase annotation, meaning that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * two objects are equivalent if they have the same serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * representation <i>except</i> for the codebase of each class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * in the serialized representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @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
   185
     * @return <code>true</code> if the argument contains an equivalent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * serialized object; <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (obj == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (obj != null && obj instanceof MarshalledObject) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
   194
            MarshalledObject<?> other = (MarshalledObject<?>) obj;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // if either is a ref to null, both must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (objBytes == null || other.objBytes == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                return objBytes == other.objBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // quick, easy test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (objBytes.length != other.objBytes.length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            //!! There is talk about adding an array comparision method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            //!! at 1.2 -- if so, this should be rewritten.  -arnold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int i = 0; i < objBytes.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (objBytes[i] != other.objBytes[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * This class is used to marshal objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <code>MarshalledObject</code>.  It places the location annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * to one side so that two <code>MarshalledObject</code>s can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * compared for equality if they differ only in location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * annotations.  Objects written using this stream should be read back
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * from a <code>MarshalledObjectInputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @see java.rmi.MarshalledObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @see MarshalledObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private static class MarshalledObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        extends MarshalOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        /** The stream on which location objects are written. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        private ObjectOutputStream locOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        /** <code>true</code> if non-<code>null</code> annotations are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
         *  written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        private boolean hadAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * Creates a new <code>MarshalledObjectOutputStream</code> whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * non-location bytes will be written to <code>objOut</code> and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * location annotations (if any) will be written to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * <code>locOut</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            super(objOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            this.locOut = new ObjectOutputStream(locOut);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            hadAnnotations = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
         * Returns <code>true</code> if any non-<code>null</code> location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
         * annotations have been written to this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        boolean hadAnnotations() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return hadAnnotations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * Overrides MarshalOutputStream.writeLocation implementation to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         * annotations to the location stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        protected void writeLocation(String loc) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            hadAnnotations |= (loc != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            locOut.writeObject(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            super.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            locOut.flush();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * The counterpart to <code>MarshalledObjectOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @see MarshalledObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private static class MarshalledObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        extends MarshalInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * The stream from which annotations will be read.  If this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * <code>null</code>, then all annotations were <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        private ObjectInputStream locIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * Creates a new <code>MarshalledObjectInputStream</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * reads its objects from <code>objIn</code> and annotations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         * from <code>locIn</code>.  If <code>locIn</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * <code>null</code>, then all annotations will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         * <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        MarshalledObjectInputStream(InputStream objIn, InputStream locIn)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            super(objIn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * Overrides MarshalInputStream.readLocation to return locations from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * the stream we were given, or <code>null</code> if we were given a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * <code>null</code> location stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        protected Object readLocation()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return (locIn == null ? null : locIn.readObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
}