jdk/src/java.rmi/share/classes/java/rmi/MarshalledObject.java
changeset 43211 f264afd5082c
parent 25859 3317bb8137f4
equal deleted inserted replaced
43210:570fbef3a53b 43211:f264afd5082c
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    27 
    27 
    28 import java.io.ByteArrayInputStream;
    28 import java.io.ByteArrayInputStream;
    29 import java.io.ByteArrayOutputStream;
    29 import java.io.ByteArrayOutputStream;
    30 import java.io.IOException;
    30 import java.io.IOException;
    31 import java.io.InputStream;
    31 import java.io.InputStream;
       
    32 import java.io.ObjectInputFilter;
    32 import java.io.ObjectInputStream;
    33 import java.io.ObjectInputStream;
    33 import java.io.ObjectOutputStream;
    34 import java.io.ObjectOutputStream;
    34 import java.io.ObjectStreamConstants;
    35 import java.io.ObjectStreamConstants;
    35 import java.io.OutputStream;
    36 import java.io.OutputStream;
    36 import java.io.Serializable;
    37 import java.io.Serializable;
       
    38 import java.security.AccessController;
       
    39 import java.security.PrivilegedAction;
       
    40 
    37 import sun.rmi.server.MarshalInputStream;
    41 import sun.rmi.server.MarshalInputStream;
    38 import sun.rmi.server.MarshalOutputStream;
    42 import sun.rmi.server.MarshalOutputStream;
    39 
    43 
    40 /**
    44 /**
    41  * A <code>MarshalledObject</code> contains a byte stream with the serialized
    45  * A <code>MarshalledObject</code> contains a byte stream with the serialized
    87      * @serial Stored hash code of contained object.
    91      * @serial Stored hash code of contained object.
    88      *
    92      *
    89      * @see #hashCode
    93      * @see #hashCode
    90      */
    94      */
    91     private int hash;
    95     private int hash;
       
    96 
       
    97     /** Filter used when creating the instance from a stream; may be null. */
       
    98     private transient ObjectInputFilter objectInputFilter = null;
    92 
    99 
    93     /** Indicate compatibility with 1.2 version of class. */
   100     /** Indicate compatibility with 1.2 version of class. */
    94     private static final long serialVersionUID = 8988374069173025854L;
   101     private static final long serialVersionUID = 8988374069173025854L;
    95 
   102 
    96     /**
   103     /**
   131         }
   138         }
   132         hash = h;
   139         hash = h;
   133     }
   140     }
   134 
   141 
   135     /**
   142     /**
       
   143      * Reads in the state of the object and saves the stream's
       
   144      * serialization filter to be used when the object is deserialized.
       
   145      *
       
   146      * @param stream the stream
       
   147      * @throws IOException if an I/O error occurs
       
   148      * @throws ClassNotFoundException if a class cannot be found
       
   149      */
       
   150     private void readObject(ObjectInputStream stream)
       
   151         throws IOException, ClassNotFoundException {
       
   152         stream.defaultReadObject();     // read in all fields
       
   153         objectInputFilter = stream.getObjectInputFilter();
       
   154     }
       
   155 
       
   156     /**
   136      * Returns a new copy of the contained marshalledobject.  The internal
   157      * Returns a new copy of the contained marshalledobject.  The internal
   137      * representation is deserialized with the semantics used for
   158      * representation is deserialized with the semantics used for
   138      * unmarshaling parameters for RMI calls.
   159      * unmarshaling parameters for RMI calls.
       
   160      * If the MarshalledObject was read from an ObjectInputStream,
       
   161      * the filter from that stream is used to deserialize the object.
   139      *
   162      *
   140      * @return a copy of the contained object
   163      * @return a copy of the contained object
   141      * @exception IOException if an <code>IOException</code> occurs while
   164      * @exception IOException if an <code>IOException</code> occurs while
   142      * deserializing the object from its internal representation.
   165      * deserializing the object from its internal representation.
   143      * @exception ClassNotFoundException if a
   166      * @exception ClassNotFoundException if a
   153         ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);
   176         ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);
   154         // locBytes is null if no annotations
   177         // locBytes is null if no annotations
   155         ByteArrayInputStream lin =
   178         ByteArrayInputStream lin =
   156             (locBytes == null ? null : new ByteArrayInputStream(locBytes));
   179             (locBytes == null ? null : new ByteArrayInputStream(locBytes));
   157         MarshalledObjectInputStream in =
   180         MarshalledObjectInputStream in =
   158             new MarshalledObjectInputStream(bin, lin);
   181             new MarshalledObjectInputStream(bin, lin, objectInputFilter);
   159         @SuppressWarnings("unchecked")
   182         @SuppressWarnings("unchecked")
   160         T obj = (T) in.readObject();
   183         T obj = (T) in.readObject();
   161         in.close();
   184         in.close();
   162         return obj;
   185         return obj;
   163     }
   186     }
   293          * reads its objects from <code>objIn</code> and annotations
   316          * reads its objects from <code>objIn</code> and annotations
   294          * from <code>locIn</code>.  If <code>locIn</code> is
   317          * from <code>locIn</code>.  If <code>locIn</code> is
   295          * <code>null</code>, then all annotations will be
   318          * <code>null</code>, then all annotations will be
   296          * <code>null</code>.
   319          * <code>null</code>.
   297          */
   320          */
   298         MarshalledObjectInputStream(InputStream objIn, InputStream locIn)
   321         MarshalledObjectInputStream(InputStream objIn, InputStream locIn,
       
   322                     ObjectInputFilter filter)
   299             throws IOException
   323             throws IOException
   300         {
   324         {
   301             super(objIn);
   325             super(objIn);
   302             this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));
   326             this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));
       
   327             if (filter != null) {
       
   328                 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
       
   329                     MarshalledObjectInputStream.this.setObjectInputFilter(filter);
       
   330                     if (MarshalledObjectInputStream.this.locIn != null) {
       
   331                         MarshalledObjectInputStream.this.locIn.setObjectInputFilter(filter);
       
   332                     }
       
   333                     return null;
       
   334                 });
       
   335             }
   303         }
   336         }
   304 
   337 
   305         /**
   338         /**
   306          * Overrides MarshalInputStream.readLocation to return locations from
   339          * Overrides MarshalInputStream.readLocation to return locations from
   307          * the stream we were given, or <code>null</code> if we were given a
   340          * the stream we were given, or <code>null</code> if we were given a