jdk/src/java.base/share/classes/java/io/ObjectInputStream.java
author rriggs
Fri, 03 Feb 2017 14:10:33 -0500
changeset 44756 b887cb49e622
parent 44261 124fd1218a88
child 45138 ddcafe0d0ea3
permissions -rw-r--r--
8172299: Improve class processing Reviewed-by: coffeys, chegar, ahgross, skoivu, rhalade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
44756
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
     2
 * Copyright (c) 1996, 2017, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ObjectStreamClass.WeakClassKey;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
    29
import java.lang.System.Logger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessControlContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Arrays;
36659
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
    40
import java.util.Map;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
    41
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.util.concurrent.ConcurrentMap;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
    44
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import static java.io.ObjectStreamClass.processQueue;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
    46
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
    47
import jdk.internal.misc.ObjectStreamClassValidator;
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
    48
import jdk.internal.misc.SharedSecrets;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 33403
diff changeset
    49
import jdk.internal.misc.Unsafe;
16873
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
    50
import sun.reflect.misc.ReflectUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * An ObjectInputStream deserializes primitive data and objects previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * written using an ObjectOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <p>ObjectOutputStream and ObjectInputStream can provide an application with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * persistent storage for graphs of objects when used with a FileOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * and FileInputStream respectively.  ObjectInputStream is used to recover
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * those objects previously serialized. Other uses include passing objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * between hosts using a socket stream or for marshaling and unmarshaling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * arguments and parameters in a remote communication system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>ObjectInputStream ensures that the types of all objects in the graph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * created from the stream match the classes present in the Java Virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Machine.  Classes are loaded as required using the standard mechanisms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p>Only objects that support the java.io.Serializable or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * java.io.Externalizable interface can be read from streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p>The method <code>readObject</code> is used to read an object from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * stream.  Java's safe casting should be used to get the desired type.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Java, strings and arrays are objects and are treated as objects during
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * serialization. When read they need to be cast to the expected type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>Primitive data types can be read from the stream using the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * method on DataInput.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p>The default deserialization mechanism for objects restores the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * of each field to the value and type it had when it was written.  Fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * declared as transient or static are ignored by the deserialization process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * References to other objects cause those objects to be read from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * as necessary.  Graphs of objects are restored correctly using a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * sharing mechanism.  New objects are always allocated when deserializing,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * which prevents existing objects from being overwritten.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * <p>Reading an object is analogous to running the constructors of a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * object.  Memory is allocated for the object and initialized to zero (NULL).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * No-arg constructors are invoked for the non-serializable classes and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the fields of the serializable classes are restored from the stream starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * with the serializable class closest to java.lang.object and finishing with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * the object's most specific class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <p>For example to read from a stream as written by the example in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * ObjectOutputStream:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *      FileInputStream fis = new FileInputStream("t.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *      ObjectInputStream ois = new ObjectInputStream(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      int i = ois.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      String today = (String) ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      Date date = (Date) ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *      ois.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <p>Classes control how they are serialized by implementing either the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * java.io.Serializable or java.io.Externalizable interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * <p>Implementing the Serializable interface allows object serialization to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * save and restore the entire state of the object and it allows classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * evolve between the time the stream is written and the time it is read.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * automatically traverses references between objects, saving and restoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * entire graphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * <p>Serializable classes that require special handling during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * serialization and deserialization process should implement the following
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 20839
diff changeset
   118
 * methods:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * private void writeObject(java.io.ObjectOutputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * private void readObject(java.io.ObjectInputStream stream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *     throws IOException, ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * private void readObjectNoData()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 *     throws ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * <p>The readObject method is responsible for reading and restoring the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * of the object for its particular class using data written to the stream by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * the corresponding writeObject method.  The method does not need to concern
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * itself with the state belonging to its superclasses or subclasses.  State is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * restored by reading data from the ObjectInputStream for the individual
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * fields and making assignments to the appropriate fields of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Reading primitive data types is supported by DataInput.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * <p>Any attempt to read object data which exceeds the boundaries of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * custom data written by the corresponding writeObject method will cause an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * OptionalDataException to be thrown with an eof field value of true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * Non-object reads which exceed the end of the allotted data will reflect the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * end of data in the same way that they would indicate the end of the stream:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * bytewise reads will return -1 as the byte read or number of bytes read, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * primitive reads will throw EOFExceptions.  If there is no corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * writeObject method, then the end of default serialized data marks the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * the allotted data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p>Primitive and object read calls issued from within a readExternal method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * behave in the same manner--if the stream is already positioned at the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * data written by the corresponding writeExternal method, object reads will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * throw OptionalDataExceptions with eof set to true, bytewise reads will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * return -1, and primitive reads will throw EOFExceptions.  Note that this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * behavior does not hold for streams written with the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * <code>ObjectStreamConstants.PROTOCOL_VERSION_1</code> protocol, in which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * end of data written by writeExternal methods is not demarcated, and hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * cannot be detected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * <p>The readObjectNoData method is responsible for initializing the state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * the object for its particular class in the event that the serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * stream does not list the given class as a superclass of the object being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * deserialized.  This may occur in cases where the receiving party uses a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * different version of the deserialized instance's class than the sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 * party, and the receiver's version extends classes that are not extended by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * the sender's version.  This may also occur if the serialization stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * been tampered; hence, readObjectNoData is useful for initializing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * deserialized objects properly despite a "hostile" or incomplete source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * <p>Serialization does not read or assign values to the fields of any object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * that does not implement the java.io.Serializable interface.  Subclasses of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * Objects that are not serializable can be serializable. In this case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 * non-serializable class must have a no-arg constructor to allow its fields to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 * be initialized.  In this case it is the responsibility of the subclass to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * save and restore the state of the non-serializable class. It is frequently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * the case that the fields of that class are accessible (public, package, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 * protected) or that there are get and set methods that can be used to restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * the state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   178
 * <p>The contents of the stream can be filtered during deserialization.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   179
 * If a {@linkplain #setObjectInputFilter(ObjectInputFilter) filter is set}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   180
 * on an ObjectInputStream, the {@link ObjectInputFilter} can check that
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   181
 * the classes, array lengths, number of references in the stream, depth, and
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   182
 * number of bytes consumed from the input stream are allowed and
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   183
 * if not, can terminate deserialization.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   184
 * A {@linkplain ObjectInputFilter.Config#setSerialFilter(ObjectInputFilter) process-wide filter}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   185
 * can be configured that is applied to each {@code ObjectInputStream} unless replaced
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   186
 * using {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter}.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   187
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * <p>Any exception that occurs while deserializing an object will be caught by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 * the ObjectInputStream and abort the reading process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * <p>Implementing the Externalizable interface allows the object to assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * complete control over the contents and format of the object's serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * form.  The methods of the Externalizable interface, writeExternal and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * readExternal, are called to save and restore the objects state.  When
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * implemented by a class they can write and read their own state using all of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * the methods of ObjectOutput and ObjectInput.  It is the responsibility of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * the objects to handle any versioning that occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * <p>Enum constants are deserialized differently than ordinary serializable or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * externalizable objects.  The serialized form of an enum constant consists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * solely of its name; field values of the constant are not transmitted.  To
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * deserialize an enum constant, ObjectInputStream reads the constant name from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * the stream; the deserialized constant is then obtained by calling the static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * method <code>Enum.valueOf(Class, String)</code> with the enum constant's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * base type and the received constant name as arguments.  Like other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * serializable or externalizable objects, enum constants can function as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * targets of back references appearing subsequently in the serialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * stream.  The process by which enum constants are deserialized cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * customized: any class-specific readObject, readObjectNoData, and readResolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * methods defined by enum types are ignored during deserialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * Similarly, any serialPersistentFields or serialVersionUID field declarations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * are also ignored--all enum types have a fixed serialVersionUID of 0L.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * @author      Mike Warres
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * @author      Roger Riggs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * @see java.io.DataInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * @see java.io.ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * @see java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * @see <a href="../../../platform/serialization/spec/input.html"> Object Serialization Specification, Section 3, Object Input Classes</a>
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 22936
diff changeset
   220
 * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
public class ObjectInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    extends InputStream implements ObjectInput, ObjectStreamConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /** handle value representing null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private static final int NULL_HANDLE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /** marker for unshared objects in internal handle table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    private static final Object unsharedMarker = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
36659
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   231
    /**
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   232
     * immutable table mapping primitive type names to corresponding
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   233
     * class objects
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   234
     */
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   235
    private static final Map<String, Class<?>> primClasses =
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   236
        Map.of("boolean", boolean.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   237
               "byte", byte.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   238
               "char", char.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   239
               "short", short.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   240
               "int", int.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   241
               "long", long.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   242
               "float", float.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   243
               "double", double.class,
4c3494cead61 8151957: ObjectInputStream - Use new convenience method for immutable Map creation during static initialization
bpb
parents: 36511
diff changeset
   244
               "void", void.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private static class Caches {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /** cache of subclass security audit results */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7535
diff changeset
   249
            new ConcurrentHashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        /** queue for WeakReferences to audited subclasses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        static final ReferenceQueue<Class<?>> subclassAuditsQueue =
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 7535
diff changeset
   253
            new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   256
    /*
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   257
     * Separate class to defer initialization of logging until needed.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   258
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   259
    private static class Logging {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   260
        /*
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   261
         * Logger for ObjectInputFilter results.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   262
         * Setup the filter logger if it is set to DEBUG or TRACE.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   263
         * (Assuming it will not change).
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   264
         */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   265
        static final System.Logger filterLogger;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   266
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   267
        static {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   268
            Logger filterLog = System.getLogger("java.io.serialization");
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   269
            filterLogger = (filterLog.isLoggable(Logger.Level.DEBUG)
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   270
                    || filterLog.isLoggable(Logger.Level.TRACE)) ? filterLog : null;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   271
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   272
    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   273
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /** filter stream for handling block data conversion */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private final BlockDataInputStream bin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /** validation callback list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private final ValidationList vlist;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /** recursion depth */
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   279
    private long depth;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   280
    /** Total number of references to any type of object, class, enum, proxy, etc. */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   281
    private long totalObjectRefs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /** whether stream is closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /** wire handle -> obj/exception map */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    private final HandleTable handles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /** scratch field for passing handle values up/down call stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private int passHandle = NULL_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /** flag set when at end of field value block with no TC_ENDBLOCKDATA */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    private boolean defaultDataEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /** if true, invoke readObjectOverride() instead of readObject() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private final boolean enableOverride;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /** if true, invoke resolveObject() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private boolean enableResolve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Context during upcalls to class-defined readObject methods; holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * object currently being deserialized and descriptor for current class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Null when not during readObject upcall.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
   302
    private SerialCallbackContext curContext;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   305
     * Filter of class descriptors and classes read from the stream;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   306
     * may be null.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   307
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   308
    private ObjectInputFilter serialFilter;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   309
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   310
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Creates an ObjectInputStream that reads from the specified InputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * A serialization stream header is read from the stream and verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * This constructor will block until the corresponding ObjectOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * has written and flushed the header.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   316
     * <p>The serialization filter is initialized to the value of
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   317
     * {@linkplain ObjectInputFilter.Config#getSerialFilter() the process-wide filter}.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   318
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * <p>If a security manager is installed, this constructor will check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * the "enableSubclassImplementation" SerializablePermission when invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * directly or indirectly by the constructor of a subclass which overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * the ObjectInputStream.readFields or ObjectInputStream.readUnshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param   in input stream to read from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @throws  StreamCorruptedException if the stream header is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * @throws  IOException if an I/O error occurs while reading stream header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @throws  SecurityException if untrusted subclass illegally overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *          security-sensitive methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @throws  NullPointerException if <code>in</code> is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @see     ObjectInputStream#ObjectInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @see     ObjectInputStream#readFields()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @see     ObjectOutputStream#ObjectOutputStream(OutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public ObjectInputStream(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        verifySubclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        bin = new BlockDataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        handles = new HandleTable(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        vlist = new ValidationList();
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   340
        serialFilter = ObjectInputFilter.Config.getSerialFilter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        enableOverride = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        readStreamHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Provide a way for subclasses that are completely reimplementing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * ObjectInputStream to not have to allocate private data just used by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * implementation of ObjectInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     *
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   351
     * <p>The serialization filter is initialized to the value of
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   352
     * {@linkplain ObjectInputFilter.Config#getSerialFilter() the process-wide filter}.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   353
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <p>If there is a security manager installed, this method first calls the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * security manager's <code>checkPermission</code> method with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <code>SerializablePermission("enableSubclassImplementation")</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * permission to ensure it's ok to enable subclassing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @throws  SecurityException if a security manager exists and its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *          <code>checkPermission</code> method denies enabling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *          subclassing.
19047
08210fe86260 8021408: Fix misc doclint issues in java.util and java.io
darcy
parents: 16873
diff changeset
   362
     * @throws  IOException if an I/O error occurs while creating this stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * @see java.io.SerializablePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    protected ObjectInputStream() throws IOException, SecurityException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        bin = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        handles = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        vlist = null;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   374
        serialFilter = ObjectInputFilter.Config.getSerialFilter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        enableOverride = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Read an object from the ObjectInputStream.  The class of the object, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * signature of the class, and the values of the non-transient and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * non-static fields of the class and all of its supertypes are read.
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   382
     * Default deserializing for a class can be overridden using the writeObject
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * and readObject methods.  Objects referenced by this object are read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * transitively so that a complete equivalent graph of objects is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * reconstructed by readObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * <p>The root object is completely restored when all of its fields and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * objects it references are completely restored.  At this point the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * validation callbacks are executed in order based on their registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * priorities. The callbacks are registered by objects (in the readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * special methods) as they are individually restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   393
     * <p>The serialization filter, when not {@code null}, is invoked for
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   394
     * each object (regular or class) read to reconstruct the root object.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   395
     * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   396
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * <p>Exceptions are thrown for problems with the InputStream and for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * classes that should not be deserialized.  All exceptions are fatal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * the InputStream and leave it in an indeterminate state; it is up to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * caller to ignore or recover the stream state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @throws  ClassNotFoundException Class of a serialized object cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *          found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @throws  InvalidClassException Something is wrong with a class used by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *          serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @throws  StreamCorruptedException Control information in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *          stream is inconsistent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws  OptionalDataException Primitive data was found in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          stream instead of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @throws  IOException Any of the usual Input/Output related exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    public final Object readObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (enableOverride) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            return readObjectOverride();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // if nested read, passHandle contains handle of enclosing object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        int outerHandle = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            Object obj = readObject0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            handles.markDependency(outerHandle, passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            ClassNotFoundException ex = handles.lookupException(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                vlist.doCallbacks();
29220
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
   430
                freeze();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            passHandle = outerHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            if (closed && depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * This method is called by trusted subclasses of ObjectOutputStream that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * constructed ObjectOutputStream using the protected no-arg constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * The subclass is expected to provide an override method with the modifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * "final".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @return  the Object read from the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @throws  ClassNotFoundException Class definition of a serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *          cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @throws  OptionalDataException Primitive data was found in the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *          instead of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @throws  IOException if I/O errors occurred while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *          underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @see #ObjectInputStream()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @see #readObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    protected Object readObjectOverride()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * Reads an "unshared" object from the ObjectInputStream.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * identical to readObject, except that it prevents subsequent calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * readObject and readUnshared from returning additional references to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * deserialized instance obtained via this call.  Specifically:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *   <li>If readUnshared is called to deserialize a back-reference (the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *       stream representation of an object which has been written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *       previously to the stream), an ObjectStreamException will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *       thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *   <li>If readUnshared returns successfully, then any subsequent attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *       to deserialize back-references to the stream handle deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *       by readUnshared will cause an ObjectStreamException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * Deserializing an object via readUnshared invalidates the stream handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * associated with the returned object.  Note that this in itself does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * always guarantee that the reference returned by readUnshared is unique;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * the deserialized object may define a readResolve method which returns an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * object visible to other parties, or readUnshared may return a Class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * object or enum constant obtainable elsewhere in the stream or through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * external means. If the deserialized object defines a readResolve method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * and the invocation of that method returns an array, then readUnshared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * returns a shallow clone of that array; this guarantees that the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * array object is unique and cannot be obtained a second time from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * invocation of readObject or readUnshared on the ObjectInputStream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * even if the underlying data stream has been manipulated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   492
     * <p>The serialization filter, when not {@code null}, is invoked for
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   493
     * each object (regular or class) read to reconstruct the root object.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   494
     * See {@link #setObjectInputFilter(ObjectInputFilter) setObjectInputFilter} for details.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
   495
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * <p>ObjectInputStream subclasses which override this method can only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * constructed in security contexts possessing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * "enableSubclassImplementation" SerializablePermission; any attempt to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * instantiate such a subclass without this permission will cause a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * SecurityException to be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @return  reference to deserialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * @throws  ClassNotFoundException if class of an object to deserialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *          cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @throws  StreamCorruptedException if control information in the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *          is inconsistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @throws  ObjectStreamException if object to deserialize has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *          appeared in stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @throws  OptionalDataException if primitive data is next in stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @throws  IOException if an I/O error occurs during deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public Object readUnshared() throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        // if nested read, passHandle contains handle of enclosing object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        int outerHandle = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            Object obj = readObject0(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            handles.markDependency(outerHandle, passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            ClassNotFoundException ex = handles.lookupException(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                vlist.doCallbacks();
29220
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
   525
                freeze();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            passHandle = outerHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            if (closed && depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Read the non-static and non-transient fields of the current class from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * this stream.  This may only be called from the readObject method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * class being deserialized. It will throw the NotActiveException if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * called otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * @throws  ClassNotFoundException if the class of a serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *          could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @throws  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @throws  NotActiveException if the stream is not currently reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *          objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public void defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    {
20815
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   551
        SerialCallbackContext ctx = curContext;
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   552
        if (ctx == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            throw new NotActiveException("not in call to readObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
20815
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   555
        Object curObj = ctx.getObj();
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   556
        ObjectStreamClass curDesc = ctx.getDesc();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        bin.setBlockDataMode(false);
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
   558
        FieldValues vals = defaultReadFields(curObj, curDesc);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
   559
        if (curObj != null) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
   560
            defaultCheckFieldValues(curObj, curDesc, vals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
   561
            defaultSetFieldValues(curObj, curDesc, vals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
   562
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (!curDesc.hasWriteObjectData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
             * Fix for 4360508: since stream does not contain terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
             * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
             * knows to simulate end-of-custom-data behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            defaultDataEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        ClassNotFoundException ex = handles.lookupException(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (ex != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * Reads the persistent fields from the stream and makes them available by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return  the <code>GetField</code> object representing the persistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *          fields of the object being deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @throws  ClassNotFoundException if the class of a serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *          could not be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @throws  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * @throws  NotActiveException if the stream is not currently reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *          objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    public ObjectInputStream.GetField readFields()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    {
20815
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   594
        SerialCallbackContext ctx = curContext;
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   595
        if (ctx == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            throw new NotActiveException("not in call to readObject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        }
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
   598
        ctx.checkAndSetUsed();
20815
31d867b2565d 8014987: Augment serialization handling
smarks
parents: 16873
diff changeset
   599
        ObjectStreamClass curDesc = ctx.getDesc();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        bin.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        GetFieldImpl getField = new GetFieldImpl(curDesc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        getField.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        if (!curDesc.hasWriteObjectData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
             * Fix for 4360508: since stream does not contain terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
             * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
             * knows to simulate end-of-custom-data behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            defaultDataEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        return getField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * Register an object to be validated before the graph is returned.  While
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * similar to resolveObject these validations are called after the entire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * graph has been reconstituted.  Typically, a readObject method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * register the object with the stream so that when all of the objects are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * restored a final set of validations can be performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * @param   obj the object to receive the validation callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @param   prio controls the order of callbacks;zero is a good default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *          Use higher numbers to be called back earlier, lower numbers for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *          later callbacks. Within a priority, callbacks are processed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *          no particular order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @throws  NotActiveException The stream is not currently reading objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          so it is invalid to register a callback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * @throws  InvalidObjectException The validation object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    public void registerValidation(ObjectInputValidation obj, int prio)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        throws NotActiveException, InvalidObjectException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            throw new NotActiveException("stream inactive");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        vlist.register(obj, prio);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * Load the local class equivalent of the specified stream class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * description.  Subclasses may implement this method to allow classes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * be fetched from an alternate source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>The corresponding method in <code>ObjectOutputStream</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * <code>annotateClass</code>.  This method will be invoked only once for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * each unique class in the stream.  This method can be implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * subclasses to use an alternate loading mechanism but must return a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <code>Class</code> object. Once returned, if the class is not an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * class, its serialVersionUID is compared to the serialVersionUID of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * serialized class, and if there is a mismatch, the deserialization fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * and an {@link InvalidClassException} is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * <p>The default implementation of this method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * <code>ObjectInputStream</code> returns the result of calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *     Class.forName(desc.getName(), false, loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * </pre>
42440
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   660
     * where <code>loader</code> is the first class loader on the current
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   661
     * thread's stack (starting from the currently executing method) that is
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   662
     * neither the {@linkplain ClassLoader#getPlatformClassLoader() platform
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   663
     * class loader} nor its ancestor; otherwise, <code>loader</code> is the
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   664
     * <em>platform class loader</em>. If this call results in a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * <code>ClassNotFoundException</code> and the name of the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * <code>ObjectStreamClass</code> instance is the Java language keyword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * for a primitive type or void, then the <code>Class</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * representing that primitive type or void will be returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * (e.g., an <code>ObjectStreamClass</code> with the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * <code>"int"</code> will be resolved to <code>Integer.TYPE</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * Otherwise, the <code>ClassNotFoundException</code> will be thrown to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * the caller of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @param   desc an instance of class <code>ObjectStreamClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @return  a <code>Class</code> object corresponding to <code>desc</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @throws  IOException any of the usual Input/Output exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * @throws  ClassNotFoundException if class of a serialized object cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *          be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    protected Class<?> resolveClass(ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        String name = desc.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            return Class.forName(name, false, latestUserDefinedLoader());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        } catch (ClassNotFoundException ex) {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   687
            Class<?> cl = primClasses.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * Returns a proxy class that implements the interfaces named in a proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * class descriptor; subclasses may implement this method to read custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * data from the stream along with the descriptors for dynamic proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * classes, allowing them to use an alternate loading mechanism for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * interfaces and the proxy class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * <p>This method is called exactly once for each unique proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * descriptor in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * <p>The corresponding method in <code>ObjectOutputStream</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * <code>annotateProxyClass</code>.  For a given subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * <code>ObjectInputStream</code> that overrides this method, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <code>annotateProxyClass</code> method in the corresponding subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * <code>ObjectOutputStream</code> must write any data or objects read by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <p>The default implementation of this method in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <code>ObjectInputStream</code> returns the result of calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * <code>Proxy.getProxyClass</code> with the list of <code>Class</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * objects for the interfaces that are named in the <code>interfaces</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * parameter.  The <code>Class</code> object for each interface name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>i</code> is the value returned by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     *     Class.forName(i, false, loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * </pre>
42440
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   722
     * where <code>loader</code> is the first class loader on the current
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   723
     * thread's stack (starting from the currently executing method) that is
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   724
     * neither the {@linkplain ClassLoader#getPlatformClassLoader() platform
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   725
     * class loader} nor its ancestor; otherwise, <code>loader</code> is the
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
   726
     * <em>platform class loader</em>.
37899
1b8ec90a1e15 8155977: ObjectInputStream::resolveClass & resolveProxyClass for platform loader
mchung
parents: 37591
diff changeset
   727
     * Unless any of the resolved interfaces are non-public, this same value
1b8ec90a1e15 8155977: ObjectInputStream::resolveClass & resolveProxyClass for platform loader
mchung
parents: 37591
diff changeset
   728
     * of <code>loader</code> is also the class loader passed to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * <code>Proxy.getProxyClass</code>; if non-public interfaces are present,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * their class loader is passed instead (if more than one non-public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * interface class loader is encountered, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * <code>IllegalAccessError</code> is thrown).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * If <code>Proxy.getProxyClass</code> throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * <code>IllegalArgumentException</code>, <code>resolveProxyClass</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * will throw a <code>ClassNotFoundException</code> containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * <code>IllegalArgumentException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @param interfaces the list of interface names that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *                deserialized in the proxy class descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @return  a proxy class for the specified interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @throws        IOException any exception thrown by the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     *                <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     * @throws        ClassNotFoundException if the proxy class or any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
     *                named interfaces could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
     * @see ObjectOutputStream#annotateProxyClass(Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    protected Class<?> resolveProxyClass(String[] interfaces)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        ClassLoader latestLoader = latestUserDefinedLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        ClassLoader nonPublicLoader = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        boolean hasNonPublicInterface = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        // define proxy in class loader of non-public interface(s), if any
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
   756
        Class<?>[] classObjs = new Class<?>[interfaces.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        for (int i = 0; i < interfaces.length; i++) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
   758
            Class<?> cl = Class.forName(interfaces[i], false, latestLoader);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                if (hasNonPublicInterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                    if (nonPublicLoader != cl.getClassLoader()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                        throw new IllegalAccessError(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                            "conflicting non-public interface class loaders");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    nonPublicLoader = cl.getClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                    hasNonPublicInterface = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            classObjs[i] = cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        try {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35788
diff changeset
   773
            @SuppressWarnings("deprecation")
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35788
diff changeset
   774
            Class<?> proxyClass = Proxy.getProxyClass(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                hasNonPublicInterface ? nonPublicLoader : latestLoader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                classObjs);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35788
diff changeset
   777
            return proxyClass;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            throw new ClassNotFoundException(null, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * This method will allow trusted subclasses of ObjectInputStream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * substitute one object for another during deserialization. Replacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * objects is disabled until enableResolveObject is called. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * enableResolveObject method checks that the stream requesting to resolve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * object can be trusted. Every reference to serializable objects is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * to resolveObject.  To insure that the private state of objects is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * unintentionally exposed only trusted streams may use resolveObject.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * <p>This method is called after an object has been read but before it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * returned from readObject.  The default resolveObject method just returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * the same object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * <p>When a subclass is replacing objects it must insure that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * substituted object is compatible with every field where the reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     * will be stored.  Objects whose type is not a subclass of the type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * field or array element abort the serialization by raising an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * and the object is not be stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p>This method is called only once when each object is first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * encountered.  All subsequent references to the object will be redirected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * to the new object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * @param   obj object to be substituted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * @return  the substituted object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * @throws  IOException Any of the usual Input/Output exceptions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    protected Object resolveObject(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    /**
44261
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   815
     * Enables the stream to do replacement of objects read from the stream. When
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   816
     * enabled, the {@link #resolveObject} method is called for every object being
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
44261
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   819
     * <p>If object replacement is currently not enabled, and
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   820
     * {@code enable} is true, and there is a security manager installed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * this method first calls the security manager's
44261
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   822
     * {@code checkPermission} method with the
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   823
     * {@code SerializablePermission("enableSubstitution")} permission to
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   824
     * ensure that the caller is permitted to enable the stream to do replacement
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   825
     * of objects read from the stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
44261
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   827
     * @param   enable true for enabling use of {@code resolveObject} for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     *          every object being deserialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @return  the previous setting before this method was invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * @throws  SecurityException if a security manager exists and its
44261
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   831
     *          {@code checkPermission} method denies enabling the stream
124fd1218a88 8066640: clarify security checks in ObjectInputStream.enableResolveObject and ObjectOutputStream.enableReplaceObject
smarks
parents: 42446
diff changeset
   832
     *          to do replacement of objects read from the stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * @see SecurityManager#checkPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * @see java.io.SerializablePermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    protected boolean enableResolveObject(boolean enable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        throws SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        if (enable == enableResolve) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            return enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (enable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                sm.checkPermission(SUBSTITUTION_PERMISSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        enableResolve = enable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        return !enableResolve;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * The readStreamHeader method is provided to allow subclasses to read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * verify their own stream headers. It reads and verifies the magic number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * and version number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @throws  IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *          underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @throws  StreamCorruptedException if control information in the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *          is inconsistent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    protected void readStreamHeader()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        throws IOException, StreamCorruptedException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        short s0 = bin.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        short s1 = bin.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                String.format("invalid stream header: %04X%04X", s0, s1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Read a class descriptor from the serialization stream.  This method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * called when the ObjectInputStream expects a class descriptor as the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * item in the serialization stream.  Subclasses of ObjectInputStream may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * override this method to read in class descriptors that have been written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * in non-standard formats (by subclasses of ObjectOutputStream which have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * overridden the <code>writeClassDescriptor</code> method).  By default,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * this method reads class descriptors according to the format defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * the Object Serialization specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * @return  the class descriptor read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * @throws  ClassNotFoundException If the Class of a serialized object used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *          in the class descriptor representation cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @see java.io.ObjectOutputStream#writeClassDescriptor(java.io.ObjectStreamClass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    protected ObjectStreamClass readClassDescriptor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        ObjectStreamClass desc = new ObjectStreamClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        desc.readNonProxy(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Reads a byte of data. This method will block if no input is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @return  the byte read, or -1 if the end of the stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return bin.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Reads into an array of bytes.  This method will block until some input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * is available. Consider using java.io.DataInputStream.readFully to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * exactly 'length' bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @param   buf the buffer into which the data is read
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
   914
     * @param   off the start offset in the destination array {@code buf}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @param   len the maximum number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @return  the actual number of bytes read, -1 is returned when the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *          the stream is reached.
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
   918
     * @throws  NullPointerException if {@code buf} is {@code null}.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
   919
     * @throws  IndexOutOfBoundsException if {@code off} is negative,
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
   920
     *          {@code len} is negative, or {@code len} is greater than
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
   921
     *          {@code buf.length - off}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * @see java.io.DataInputStream#readFully(byte[],int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public int read(byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        if (buf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        return bin.read(buf, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * Returns the number of bytes that can be read without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * @return  the number of available bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @throws  IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *          underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        return bin.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * Closes the input stream. Must be called to release any resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
         * Even if stream already closed, propagate redundant close to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
         * underlying stream to stay consistent with previous implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        if (depth == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        bin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Reads in a boolean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * @return  the boolean read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
    public boolean readBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
        return bin.readBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     * Reads an 8 bit byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * @return  the 8 bit byte read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    public byte readByte() throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return bin.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Reads an unsigned 8 bit byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @return  the 8 bit byte read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    public int readUnsignedByte()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        return bin.readUnsignedByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * Reads a 16 bit char.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * @return  the 16 bit char read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
    public char readChar()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        return bin.readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * Reads a 16 bit short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * @return  the 16 bit short read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    public short readShort()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        return bin.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Reads an unsigned 16 bit short.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @return  the 16 bit short read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    public int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        return bin.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * Reads a 32 bit int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @return  the 32 bit integer read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
    public int readInt()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        return bin.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * Reads a 64 bit long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @return  the read 64 bit long.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    public long readLong()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        return bin.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Reads a 32 bit float.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @return  the 32 bit float read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    public float readFloat() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        return bin.readFloat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * Reads a 64 bit double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * @return  the 64 bit double read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    public double readDouble() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        return bin.readDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * Reads bytes, blocking until all bytes are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * @param   buf the buffer into which the data is read
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1079
     * @throws  NullPointerException If {@code buf} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
    public void readFully(byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        bin.readFully(buf, 0, buf.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Reads bytes, blocking until all bytes are read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     * @param   buf the buffer into which the data is read
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1091
     * @param   off the start offset into the data array {@code buf}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * @param   len the maximum number of bytes to read
37591
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1093
     * @throws  NullPointerException If {@code buf} is {@code null}.
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1094
     * @throws  IndexOutOfBoundsException If {@code off} is negative,
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1095
     *          {@code len} is negative, or {@code len} is greater than
b71bda3ce058 8154183: (spec) Spec of read(byte[],int,int) and readFully(byte[],int,int) is confusing/incomplete
bpb
parents: 37584
diff changeset
  1096
     *          {@code buf.length - off}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * @throws  EOFException If end of file is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @throws  IOException If other I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    public void readFully(byte[] buf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        int endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        bin.readFully(buf, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * Skips bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @param   len the number of bytes to be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * @return  the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * @throws  IOException If an I/O error has occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    public int skipBytes(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        return bin.skipBytes(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * Reads in a line that has been terminated by a \n, \r, \r\n or EOF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     * @return  a String copy of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @throws  IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     *          underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * @deprecated This method does not properly convert bytes to characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *          see DataInputStream for the details and alternatives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        return bin.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * Reads a String in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @return  the String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * @throws  IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     *          underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * @throws  UTFDataFormatException if read bytes do not represent a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     *          modified UTF-8 encoding of a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    public String readUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
        return bin.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
    /**
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1149
     * Returns the serialization filter for this stream.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1150
     * The serialization filter is the most recent filter set in
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1151
     * {@link #setObjectInputFilter setObjectInputFilter} or
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1152
     * the initial process-wide filter from
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1153
     * {@link ObjectInputFilter.Config#getSerialFilter() ObjectInputFilter.Config.getSerialFilter}.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1154
     *
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1155
     * @return the serialization filter for the stream; may be null
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1156
     * @since 9
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1157
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1158
    public final ObjectInputFilter getObjectInputFilter() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1159
        return serialFilter;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1160
    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1161
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1162
    /**
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1163
     * Set the serialization filter for the stream.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1164
     * The filter's {@link ObjectInputFilter#checkInput checkInput} method is called
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1165
     * for each class and reference in the stream.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1166
     * The filter can check any or all of the class, the array length, the number
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1167
     * of references, the depth of the graph, and the size of the input stream.
42446
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1168
     * The depth is the number of nested {@linkplain #readObject readObject}
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1169
     * calls starting with the reading of the root of the graph being deserialized
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1170
     * and the current object being deserialized.
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1171
     * The number of references is the cumulative number of objects and references
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1172
     * to objects already read from the stream including the current object being read.
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1173
     * The filter is invoked only when reading objects from the stream and for
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1174
     * not primitives.
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1175
     * <p>
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1176
     * If the filter returns {@link ObjectInputFilter.Status#REJECTED Status.REJECTED},
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1177
     * {@code null} or throws a {@link RuntimeException},
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1178
     * the active {@code readObject} or {@code readUnshared}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1179
     * throws {@link InvalidClassException}, otherwise deserialization
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1180
     * continues uninterrupted.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1181
     * <p>
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1182
     * The serialization filter is initialized to the value of
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1183
     * {@link ObjectInputFilter.Config#getSerialFilter() ObjectInputFilter.Config.getSerialFilter}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1184
     * when the {@code  ObjectInputStream} is constructed and can be set
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1185
     * to a custom filter only once.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1186
     *
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1187
     * @implSpec
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1188
     * The filter, when not {@code null}, is invoked during {@link #readObject readObject}
42446
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1189
     * and {@link #readUnshared readUnshared} for each object (regular or class) in the stream.
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1190
     * Strings are treated as primitives and do not invoke the filter.
397681315009 8170291: Unpredictable results of j.i.ObjectInputFilter::createFilter
rriggs
parents: 42440
diff changeset
  1191
     * The filter is called for:
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1192
     * <ul>
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1193
     *     <li>each object reference previously deserialized from the stream
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1194
     *     (class is {@code null}, arrayLength is -1),
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1195
     *     <li>each regular class (class is not {@code null}, arrayLength is -1),
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1196
     *     <li>each interface of a dynamic proxy and the dynamic proxy class itself
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1197
     *     (class is not {@code null}, arrayLength is -1),
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1198
     *     <li>each array is filtered using the array type and length of the array
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1199
     *     (class is the array type, arrayLength is the requested length),
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1200
     *     <li>each object replaced by its class' {@code readResolve} method
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1201
     *         is filtered using the replacement object's class, if not {@code null},
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1202
     *         and if it is an array, the arrayLength, otherwise -1,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1203
     *     <li>and each object replaced by {@link #resolveObject resolveObject}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1204
     *         is filtered using the replacement object's class, if not {@code null},
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1205
     *         and if it is an array, the arrayLength, otherwise -1.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1206
     * </ul>
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1207
     *
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1208
     * When the {@link ObjectInputFilter#checkInput checkInput} method is invoked
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1209
     * it is given access to the current class, the array length,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1210
     * the current number of references already read from the stream,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1211
     * the depth of nested calls to {@link #readObject readObject} or
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1212
     * {@link #readUnshared readUnshared},
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1213
     * and the implementation dependent number of bytes consumed from the input stream.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1214
     * <p>
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1215
     * Each call to {@link #readObject readObject} or
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1216
     * {@link #readUnshared readUnshared} increases the depth by 1
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1217
     * before reading an object and decreases by 1 before returning
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1218
     * normally or exceptionally.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1219
     * The depth starts at {@code 1} and increases for each nested object and
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1220
     * decrements when each nested call returns.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1221
     * The count of references in the stream starts at {@code 1} and
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1222
     * is increased before reading an object.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1223
     *
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1224
     * @param filter the filter, may be null
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1225
     * @throws SecurityException if there is security manager and the
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1226
     *       {@code SerializablePermission("serialFilter")} is not granted
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1227
     * @throws IllegalStateException if the {@linkplain #getObjectInputFilter() current filter}
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1228
     *       is not {@code null} and is not the process-wide filter
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1229
     * @since 9
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1230
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1231
    public final void setObjectInputFilter(ObjectInputFilter filter) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1232
        SecurityManager sm = System.getSecurityManager();
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1233
        if (sm != null) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1234
            sm.checkPermission(ObjectStreamConstants.SERIAL_FILTER_PERMISSION);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1235
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1236
        // Allow replacement of the process-wide filter if not already set
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1237
        if (serialFilter != null &&
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1238
                serialFilter != ObjectInputFilter.Config.getSerialFilter()) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1239
            throw new IllegalStateException("filter can not be set more than once");
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1240
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1241
        this.serialFilter = filter;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1242
    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1243
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1244
    /**
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1245
     * Invoke the serialization filter if non-null.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1246
     * If the filter rejects or an exception is thrown, throws InvalidClassException.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1247
     *
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1248
     * @param clazz the class; may be null
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1249
     * @param arrayLength the array length requested; use {@code -1} if not creating an array
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1250
     * @throws InvalidClassException if it rejected by the filter or
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1251
     *        a {@link RuntimeException} is thrown
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1252
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1253
    private void filterCheck(Class<?> clazz, int arrayLength)
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1254
            throws InvalidClassException {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1255
        if (serialFilter != null) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1256
            RuntimeException ex = null;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1257
            ObjectInputFilter.Status status;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1258
            try {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1259
                status = serialFilter.checkInput(new FilterValues(clazz, arrayLength,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1260
                        totalObjectRefs, depth, bin.getBytesRead()));
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1261
            } catch (RuntimeException e) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1262
                // Preventive interception of an exception to log
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1263
                status = ObjectInputFilter.Status.REJECTED;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1264
                ex = e;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1265
            }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1266
            if (Logging.filterLogger != null) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1267
                // Debug logging of filter checks that fail; Tracing for those that succeed
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1268
                Logging.filterLogger.log(status == null || status == ObjectInputFilter.Status.REJECTED
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1269
                                ? Logger.Level.DEBUG
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1270
                                : Logger.Level.TRACE,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1271
                        "ObjectInputFilter {0}: {1}, array length: {2}, nRefs: {3}, depth: {4}, bytes: {5}, ex: {6}",
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1272
                        status, clazz, arrayLength, totalObjectRefs, depth, bin.getBytesRead(),
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1273
                        Objects.toString(ex, "n/a"));
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1274
            }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1275
            if (status == null ||
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1276
                    status == ObjectInputFilter.Status.REJECTED) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1277
                InvalidClassException ice = new InvalidClassException("filter status: " + status);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1278
                ice.initCause(ex);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1279
                throw ice;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1280
            }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1281
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1282
    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1283
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1284
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * Provide access to the persistent fields read from the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31709
diff changeset
  1287
    public abstract static class GetField {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
         * Get the ObjectStreamClass that describes the fields in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
         * @return  the descriptor class that describes the serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
        public abstract ObjectStreamClass getObjectStreamClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         * Return true if the named field is defaulted and has no value in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         * @return true, if and only if the named field is defaulted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         * @throws IOException if there are I/O errors while reading from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
         *         the underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
         * @throws IllegalArgumentException if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
         *         correspond to a serializable field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        public abstract boolean defaulted(String name) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
         * Get the value of the named boolean field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
         * @return the value of the named <code>boolean</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
        public abstract boolean get(String name, boolean val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
         * Get the value of the named byte field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
         * @return the value of the named <code>byte</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        public abstract byte get(String name, byte val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
         * Get the value of the named char field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
         * @return the value of the named <code>char</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        public abstract char get(String name, char val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
         * Get the value of the named short field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
         * @return the value of the named <code>short</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        public abstract short get(String name, short val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         * Get the value of the named int field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
         * @return the value of the named <code>int</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        public abstract int get(String name, int val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
         * Get the value of the named long field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
         * @return the value of the named <code>long</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
        public abstract long get(String name, long val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
         * Get the value of the named float field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
         * @return the value of the named <code>float</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        public abstract float get(String name, float val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
         * Get the value of the named double field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
         * @return the value of the named <code>double</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        public abstract double get(String name, double val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
         * Get the value of the named Object field from the persistent field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
         * @param  name the name of the field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
         * @param  val the default value to use if <code>name</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
         *         have a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
         * @return the value of the named <code>Object</code> field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
         * @throws IOException if there are I/O errors while reading from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
         *         underlying <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
         * @throws IllegalArgumentException if type of <code>name</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
         *         not serializable or if the field type is incorrect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        public abstract Object get(String name, Object val) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * Verifies that this (possibly subclass) instance can be constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * without violating security constraints: the subclass must not override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * security-sensitive non-final methods, or else the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * "enableSubclassImplementation" SerializablePermission is checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    private void verifySubclass() {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1444
        Class<?> cl = getClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
        if (cl == ObjectInputStream.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (sm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
        WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        Boolean result = Caches.subclassAudits.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (result == null) {
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1456
            result = auditSubclass(cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            Caches.subclassAudits.putIfAbsent(key, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        }
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1459
        if (!result) {
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1460
            sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * Performs reflective checks on given subclass to verify that it doesn't
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1466
     * override security-sensitive non-final methods.  Returns TRUE if subclass
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1467
     * is "safe", FALSE otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     */
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1469
    private static Boolean auditSubclass(Class<?> subcl) {
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 37899
diff changeset
  1470
        return AccessController.doPrivileged(
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1471
            new PrivilegedAction<Boolean>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
                public Boolean run() {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  1473
                    for (Class<?> cl = subcl;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
                         cl != ObjectInputStream.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                         cl = cl.getSuperclass())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                            cl.getDeclaredMethod(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                                "readUnshared", (Class[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                            return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                            cl.getDeclaredMethod("readFields", (Class[]) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                            return Boolean.FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                        } catch (NoSuchMethodException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                    return Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * Clears internal data structures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    private void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
        handles.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        vlist.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * Underlying readObject implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    private Object readObject0(boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        boolean oldMode = bin.getBlockDataMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        if (oldMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
            int remain = bin.currentBlockRemaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
            if (remain > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                throw new OptionalDataException(remain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
            } else if (defaultDataEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                 * Fix for 4360508: stream is currently at the end of a field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                 * value block written via default serialization; since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                 * is no terminating TC_ENDBLOCKDATA tag, simulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                 * end-of-custom-data behavior explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                throw new OptionalDataException(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            bin.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        byte tc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        while ((tc = bin.peekByte()) == TC_RESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            bin.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            handleReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
        depth++;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1531
        totalObjectRefs++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
            switch (tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                case TC_NULL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                    return readNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                case TC_REFERENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    return readHandle(unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    return readClass(unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                case TC_CLASSDESC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                case TC_PROXYCLASSDESC:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    return readClassDesc(unshared);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                case TC_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                case TC_LONGSTRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    return checkResolve(readString(unshared));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    return checkResolve(readArray(unshared));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                case TC_ENUM:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    return checkResolve(readEnum(unshared));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                case TC_OBJECT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                    return checkResolve(readOrdinaryObject(unshared));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                case TC_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                    IOException ex = readFatalException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                    throw new WriteAbortedException("writing aborted", ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                case TC_BLOCKDATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                case TC_BLOCKDATALONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                    if (oldMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                        bin.peek();             // force header read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                        throw new OptionalDataException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                            bin.currentBlockRemaining());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                        throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
                            "unexpected block data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
                case TC_ENDBLOCKDATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                    if (oldMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
                        throw new OptionalDataException(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
                        throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
                            "unexpected end of block data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                    throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
                        String.format("invalid type code: %02X", tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
            depth--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
            bin.setBlockDataMode(oldMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * If resolveObject has been enabled and given object does not have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * exception associated with it, calls resolveObject to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * replacement for object, and updates handle table accordingly.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * replacement object, or echoes provided object if no replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * occurred.  Expects that passHandle is set to given object's handle prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * to calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    private Object checkResolve(Object obj) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        if (!enableResolve || handles.lookupException(passHandle) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
            return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
        Object rep = resolveObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        if (rep != obj) {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1608
            // The type of the original object has been filtered but resolveObject
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1609
            // may have replaced it;  filter the replacement's type
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1610
            if (rep != null) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1611
                if (rep.getClass().isArray()) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1612
                    filterCheck(rep.getClass(), Array.getLength(rep));
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1613
                } else {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1614
                    filterCheck(rep.getClass(), -1);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1615
                }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1616
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
            handles.setObject(passHandle, rep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        return rep;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * Reads string without allowing it to be replaced in stream.  Called from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * within ObjectStreamClass.read().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
    String readTypeString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        int oldHandle = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            byte tc = bin.peekByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            switch (tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
                case TC_NULL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
                    return (String) readNull();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
                case TC_REFERENCE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
                    return (String) readHandle(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
                case TC_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
                case TC_LONGSTRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                    return readString(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
                    throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                        String.format("invalid type code: %02X", tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            passHandle = oldHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * Reads in null code, sets passHandle to NULL_HANDLE and returns null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    private Object readNull() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        if (bin.readByte() != TC_NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        passHandle = NULL_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * Reads in object handle, sets passHandle to the read handle, and returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     * object associated with the handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    private Object readHandle(boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if (bin.readByte() != TC_REFERENCE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        passHandle = bin.readInt() - baseWireHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        if (passHandle < 0 || passHandle >= handles.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
            throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                String.format("invalid handle value: %08X", passHandle +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                baseWireHandle));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (unshared) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            // REMIND: what type of exception to throw here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            throw new InvalidObjectException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                "cannot read back reference as unshared");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        Object obj = handles.lookupObject(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        if (obj == unsharedMarker) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            // REMIND: what type of exception to throw here?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            throw new InvalidObjectException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
                "cannot read back reference to unshared object");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1687
        filterCheck(null, -1);       // just a check for number of references, depth, no class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * Reads in and returns class object.  Sets passHandle to class object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     * assigned handle.  Returns null if class is unresolvable (in which case a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * ClassNotFoundException will be associated with the class' handle in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * handle table).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1697
    private Class<?> readClass(boolean unshared) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        if (bin.readByte() != TC_CLASS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
        ObjectStreamClass desc = readClassDesc(false);
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1702
        Class<?> cl = desc.forClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        passHandle = handles.assign(unshared ? unsharedMarker : cl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        ClassNotFoundException resolveEx = desc.getResolveException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        if (resolveEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
            handles.markException(passHandle, resolveEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
        handles.finish(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
        return cl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * Reads in and returns (possibly null) class descriptor.  Sets passHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * to class descriptor's assigned handle.  If class descriptor cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * resolved to a class in the local VM, a ClassNotFoundException is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * associated with the class descriptor's handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    private ObjectStreamClass readClassDesc(boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        byte tc = bin.peekByte();
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1724
        ObjectStreamClass descriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        switch (tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
            case TC_NULL:
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1727
                descriptor = (ObjectStreamClass) readNull();
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1728
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
            case TC_REFERENCE:
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1730
                descriptor = (ObjectStreamClass) readHandle(unshared);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1731
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            case TC_PROXYCLASSDESC:
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1733
                descriptor = readProxyDesc(unshared);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1734
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
            case TC_CLASSDESC:
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1736
                descriptor = readNonProxyDesc(unshared);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1737
                break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                    String.format("invalid type code: %02X", tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1742
        if (descriptor != null) {
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1743
            validateDescriptor(descriptor);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1744
        }
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  1745
        return descriptor;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
16873
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1748
    private boolean isCustomSubclass() {
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1749
        // Return true if this class is a custom subclass of ObjectInputStream
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1750
        return getClass().getClassLoader()
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1751
                    != ObjectInputStream.class.getClassLoader();
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1752
    }
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1753
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * Reads in and returns class descriptor for a dynamic proxy class.  Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * passHandle to proxy class descriptor's assigned handle.  If proxy class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * descriptor cannot be resolved to a class in the local VM, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * ClassNotFoundException is associated with the descriptor's handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    private ObjectStreamClass readProxyDesc(boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        if (bin.readByte() != TC_PROXYCLASSDESC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        ObjectStreamClass desc = new ObjectStreamClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        int descHandle = handles.assign(unshared ? unsharedMarker : desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        passHandle = NULL_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        int numIfaces = bin.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        String[] ifaces = new String[numIfaces];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        for (int i = 0; i < numIfaces; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
            ifaces[i] = bin.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1777
        Class<?> cl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        ClassNotFoundException resolveEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
            if ((cl = resolveProxyClass(ifaces)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                resolveEx = new ClassNotFoundException("null class");
16873
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1783
            } else if (!Proxy.isProxyClass(cl)) {
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1784
                throw new InvalidClassException("Not a proxy");
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1785
            } else {
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1786
                // ReflectUtil.checkProxyPackageAccess makes a test
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1787
                // equivalent to isCustomSubclass so there's no need
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1788
                // to condition this call to isCustomSubclass == true here.
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1789
                ReflectUtil.checkProxyPackageAccess(
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1790
                        getClass().getClassLoader(),
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1791
                        cl.getInterfaces());
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1792
                // Filter the interfaces
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1793
                for (Class<?> clazz : cl.getInterfaces()) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1794
                    filterCheck(clazz, -1);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1795
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
            resolveEx = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        }
44756
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1800
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1801
        // Call filterCheck on the class before reading anything else
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1802
        filterCheck(cl, -1);
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1803
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        skipCustomData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
44756
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1806
        try {
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1807
            totalObjectRefs++;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1808
            depth++;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1809
            desc.initProxy(cl, resolveEx, readClassDesc(false));
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1810
        } finally {
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1811
            depth--;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1812
        }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1813
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        handles.finish(descHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        passHandle = descHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * Reads in and returns class descriptor for a class that is not a dynamic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     * proxy class.  Sets passHandle to class descriptor's assigned handle.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * class descriptor cannot be resolved to a class in the local VM, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     * ClassNotFoundException is associated with the descriptor's handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    private ObjectStreamClass readNonProxyDesc(boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        if (bin.readByte() != TC_CLASSDESC) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        ObjectStreamClass desc = new ObjectStreamClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        int descHandle = handles.assign(unshared ? unsharedMarker : desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        passHandle = NULL_HANDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  1836
        ObjectStreamClass readDesc;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
            readDesc = readClassDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            throw (IOException) new InvalidClassException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                "failed to read class descriptor").initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1844
        Class<?> cl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        ClassNotFoundException resolveEx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        bin.setBlockDataMode(true);
16873
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1847
        final boolean checksRequired = isCustomSubclass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            if ((cl = resolveClass(readDesc)) == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                resolveEx = new ClassNotFoundException("null class");
16873
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1851
            } else if (checksRequired) {
fb95bea093ea 8001322: Refactor deserialization
dfuchs
parents: 16100
diff changeset
  1852
                ReflectUtil.checkPackageAccess(cl);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            resolveEx = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
        }
44756
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1857
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1858
        // Call filterCheck on the class before reading anything else
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1859
        filterCheck(cl, -1);
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1860
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        skipCustomData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
44756
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1863
        try {
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1864
            totalObjectRefs++;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1865
            depth++;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1866
            desc.initNonProxy(readDesc, cl, resolveEx, readClassDesc(false));
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1867
        } finally {
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1868
            depth--;
b887cb49e622 8172299: Improve class processing
rriggs
parents: 44261
diff changeset
  1869
        }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1870
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        handles.finish(descHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        passHandle = descHandle;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1873
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
     * Reads in and returns new string.  Sets passHandle to new string's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     * assigned handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
    private String readString(boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
        String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        byte tc = bin.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        switch (tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            case TC_STRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                str = bin.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            case TC_LONGSTRING:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                str = bin.readLongUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
                    String.format("invalid type code: %02X", tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        passHandle = handles.assign(unshared ? unsharedMarker : str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        handles.finish(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     * Reads in and returns array object, or null if array class is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     * unresolvable.  Sets passHandle to array's assigned handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    private Object readArray(boolean unshared) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        if (bin.readByte() != TC_ARRAY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        ObjectStreamClass desc = readClassDesc(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        int len = bin.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1914
        filterCheck(desc.forClass(), len);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  1915
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        Object array = null;
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1917
        Class<?> cl, ccl = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        if ((cl = desc.forClass()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            ccl = cl.getComponentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            array = Array.newInstance(ccl, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        int arrayHandle = handles.assign(unshared ? unsharedMarker : array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        ClassNotFoundException resolveEx = desc.getResolveException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        if (resolveEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            handles.markException(arrayHandle, resolveEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        if (ccl == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                readObject0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        } else if (ccl.isPrimitive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            if (ccl == Integer.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                bin.readInts((int[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            } else if (ccl == Byte.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                bin.readFully((byte[]) array, 0, len, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            } else if (ccl == Long.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                bin.readLongs((long[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            } else if (ccl == Float.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
                bin.readFloats((float[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            } else if (ccl == Double.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
                bin.readDoubles((double[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            } else if (ccl == Short.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                bin.readShorts((short[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
            } else if (ccl == Character.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
                bin.readChars((char[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
            } else if (ccl == Boolean.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                bin.readBooleans((boolean[]) array, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
            Object[] oa = (Object[]) array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            for (int i = 0; i < len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
                oa[i] = readObject0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                handles.markDependency(arrayHandle, passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
        handles.finish(arrayHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
        passHandle = arrayHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     * Reads in and returns enum constant, or null if enum type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * unresolvable.  Sets passHandle to enum constant's assigned handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1970
    private Enum<?> readEnum(boolean unshared) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
        if (bin.readByte() != TC_ENUM) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        ObjectStreamClass desc = readClassDesc(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        if (!desc.isEnum()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            throw new InvalidClassException("non-enum class: " + desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        int enumHandle = handles.assign(unshared ? unsharedMarker : null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
        ClassNotFoundException resolveEx = desc.getResolveException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        if (resolveEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            handles.markException(enumHandle, resolveEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
        String name = readString(false);
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1987
        Enum<?> result = null;
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1988
        Class<?> cl = desc.forClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        if (cl != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
            try {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1991
                @SuppressWarnings("unchecked")
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1992
                Enum<?> en = Enum.valueOf((Class)cl, name);
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  1993
                result = en;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
            } catch (IllegalArgumentException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
                throw (IOException) new InvalidObjectException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
                    "enum constant " + name + " does not exist in " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                    cl).initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
            if (!unshared) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  2000
                handles.setObject(enumHandle, result);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        handles.finish(enumHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        passHandle = enumHandle;
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  2006
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * Reads and returns "ordinary" (i.e., not a String, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * ObjectStreamClass, array, or enum constant) object, or null if object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * class is unresolvable (in which case a ClassNotFoundException will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     * associated with object's handle).  Sets passHandle to object's assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    private Object readOrdinaryObject(boolean unshared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        if (bin.readByte() != TC_OBJECT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        ObjectStreamClass desc = readClassDesc(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        desc.checkDeserialize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
16090
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2026
        Class<?> cl = desc.forClass();
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2027
        if (cl == String.class || cl == Class.class
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2028
                || cl == ObjectStreamClass.class) {
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2029
            throw new InvalidClassException("invalid class descriptor");
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2030
        }
633bc7653c3b 7201070: Serialization to conform to protocol
smarks
parents: 11117
diff changeset
  2031
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        Object obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            obj = desc.isInstantiable() ? desc.newInstance() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            throw (IOException) new InvalidClassException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                desc.forClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                "unable to create instance").initCause(ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        passHandle = handles.assign(unshared ? unsharedMarker : obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        ClassNotFoundException resolveEx = desc.getResolveException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        if (resolveEx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            handles.markException(passHandle, resolveEx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        if (desc.isExternalizable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
            readExternalData((Externalizable) obj, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            readSerialData(obj, desc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        handles.finish(passHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        if (obj != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            handles.lookupException(passHandle) == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
            desc.hasReadResolveMethod())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
            Object rep = desc.invokeReadResolve(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
            if (unshared && rep.getClass().isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                rep = cloneArray(rep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            if (rep != obj) {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2064
                // Filter the replacement object
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2065
                if (rep != null) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2066
                    if (rep.getClass().isArray()) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2067
                        filterCheck(rep.getClass(), Array.getLength(rep));
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2068
                    } else {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2069
                        filterCheck(rep.getClass(), -1);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2070
                    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2071
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                handles.setObject(passHandle, obj = rep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * If obj is non-null, reads externalizable data by invoking readExternal()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     * method of obj; otherwise, attempts to skip over externalizable data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
     * Expects that passHandle is set to obj's handle before this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    private void readExternalData(Externalizable obj, ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  2088
        SerialCallbackContext oldContext = curContext;
31709
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2089
        if (oldContext != null)
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2090
            oldContext.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        curContext = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
            boolean blocked = desc.hasBlockExternalData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
            if (blocked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
            if (obj != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                    obj.readExternal(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                     * In most cases, the handle table has already propagated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                     * a CNFException to passHandle at this point; this mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                     * call is included to address cases where the readExternal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                     * method has cons'ed and thrown a new CNFException of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                     * own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                     handles.markException(passHandle, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
            if (blocked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                skipCustomData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        } finally {
31709
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2115
            if (oldContext != null)
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2116
                oldContext.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
            curContext = oldContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
         * At this point, if the externalizable data was not written in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
         * block-data form and either the externalizable class doesn't exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
         * locally (i.e., obj == null) or readExternal() just threw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
         * CNFException, then the stream is probably in an inconsistent state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
         * since some (or all) of the externalizable data may not have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
         * consumed.  Since there's no "correct" action to take in this case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
         * we mimic the behavior of past serialization implementations and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
         * blindly hope that the stream is in sync; if it isn't and additional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
         * externalizable data remains in the stream, a subsequent read will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
         * most likely throw a StreamCorruptedException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     * Reads (or attempts to skip, if obj is null or is tagged with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * ClassNotFoundException) instance data for each serializable class of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     * object in stream, from superclass to subclass.  Expects that passHandle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * is set to obj's handle before this method is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
    private void readSerialData(Object obj, ObjectStreamClass desc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2143
        // Best effort Failure Atomicity; slotValues will be non-null if field
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2144
        // values can be set after reading all field data in the hierarchy.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2145
        // Field values can only be set after reading all data if there are no
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2146
        // user observable methods in the hierarchy, readObject(NoData). The
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2147
        // top most Serializable class in the hierarchy can be skipped.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2148
        FieldValues[] slotValues = null;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2149
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2150
        boolean hasSpecialReadMethod = false;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2151
        for (int i = 1; i < slots.length; i++) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2152
            ObjectStreamClass slotDesc = slots[i].desc;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2153
            if (slotDesc.hasReadObjectMethod()
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2154
                  || slotDesc.hasReadObjectNoDataMethod()) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2155
                hasSpecialReadMethod = true;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2156
                break;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2157
            }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2158
        }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2159
        // No special read methods, can store values and defer setting.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2160
        if (!hasSpecialReadMethod)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2161
            slotValues = new FieldValues[slots.length];
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2162
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
        for (int i = 0; i < slots.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
            ObjectStreamClass slotDesc = slots[i].desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
            if (slots[i].hasData) {
31708
702a66d7e231 8076401: Serialize OIS data
chegar
parents: 30911
diff changeset
  2167
                if (obj == null || handles.lookupException(passHandle) != null) {
702a66d7e231 8076401: Serialize OIS data
chegar
parents: 30911
diff changeset
  2168
                    defaultReadFields(null, slotDesc); // skip field values
702a66d7e231 8076401: Serialize OIS data
chegar
parents: 30911
diff changeset
  2169
                } else if (slotDesc.hasReadObjectMethod()) {
37583
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2170
                    ThreadDeath t = null;
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2171
                    boolean reset = false;
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  2172
                    SerialCallbackContext oldContext = curContext;
31709
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2173
                    if (oldContext != null)
885627926ab1 8076405: Improve serial serialization
chegar
parents: 31708
diff changeset
  2174
                        oldContext.check();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                    try {
7030
90cf66131063 6559775: Race allows defaultReadObject to be invoked instead of readFields during deserialization
skoppar
parents: 5506
diff changeset
  2176
                        curContext = new SerialCallbackContext(obj, slotDesc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                        bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                        slotDesc.invokeReadObject(obj, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                    } catch (ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                         * In most cases, the handle table has already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                         * propagated a CNFException to passHandle at this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                         * point; this mark call is included to address cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                         * where the custom readObject method has cons'ed and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                         * thrown a new CNFException of its own.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                        handles.markException(passHandle, ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    } finally {
37583
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2190
                        do {
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2191
                            try {
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2192
                                curContext.setUsed();
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2193
                                if (oldContext!= null)
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2194
                                    oldContext.check();
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2195
                                curContext = oldContext;
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2196
                                reset = true;
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2197
                            } catch (ThreadDeath x) {
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2198
                                t = x;  // defer until reset is true
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2199
                            }
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2200
                        } while (!reset);
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2201
                        if (t != null)
60553c2df76e 8129952: Ensure thread consistency
chegar
parents: 36659
diff changeset
  2202
                            throw t;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                     * defaultDataEnd may have been set indirectly by custom
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                     * readObject() method when calling defaultReadObject() or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                     * readFields(); clear it to restore normal read behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    defaultDataEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                } else {
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2212
                    FieldValues vals = defaultReadFields(obj, slotDesc);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2213
                    if (slotValues != null) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2214
                        slotValues[i] = vals;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2215
                    } else if (obj != null) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2216
                        defaultCheckFieldValues(obj, slotDesc, vals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2217
                        defaultSetFieldValues(obj, slotDesc, vals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2218
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                }
31708
702a66d7e231 8076401: Serialize OIS data
chegar
parents: 30911
diff changeset
  2220
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                if (slotDesc.hasWriteObjectData()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                    skipCustomData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                    bin.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                if (obj != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                    slotDesc.hasReadObjectNoDataMethod() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                    handles.lookupException(passHandle) == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    slotDesc.invokeReadObjectNoData(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
        }
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2235
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2236
        if (obj != null && slotValues != null) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2237
            // Check that the non-primitive types are assignable for all slots
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2238
            // before assigning.
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2239
            for (int i = 0; i < slots.length; i++) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2240
                if (slotValues[i] != null)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2241
                    defaultCheckFieldValues(obj, slots[i].desc, slotValues[i]);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2242
            }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2243
            for (int i = 0; i < slots.length; i++) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2244
                if (slotValues[i] != null)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2245
                    defaultSetFieldValues(obj, slots[i].desc, slotValues[i]);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2246
            }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2247
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     * Skips over all block data and objects until TC_ENDBLOCKDATA is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
    private void skipCustomData() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        int oldHandle = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            if (bin.getBlockDataMode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                bin.skipBlockData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                bin.setBlockDataMode(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            switch (bin.peekByte()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                case TC_BLOCKDATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                case TC_BLOCKDATALONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                    bin.setBlockDataMode(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                case TC_ENDBLOCKDATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                    bin.readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                    passHandle = oldHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                    readObject0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2279
    private class FieldValues {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2280
        final byte[] primValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2281
        final Object[] objValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2282
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2283
        FieldValues(byte[] primValues, Object[] objValues) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2284
            this.primValues = primValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2285
            this.objValues = objValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2286
        }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2287
    }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2288
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     * Reads in values of serializable fields declared by given class
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2291
     * descriptor. Expects that passHandle is set to obj's handle before this
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2292
     * method is called.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
     */
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2294
    private FieldValues defaultReadFields(Object obj, ObjectStreamClass desc)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
    {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  2297
        Class<?> cl = desc.forClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
        if (cl != null && obj != null && !cl.isInstance(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
            throw new ClassCastException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2302
        byte[] primVals = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        int primDataSize = desc.getPrimDataSize();
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2304
        if (primDataSize > 0) {
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2305
            primVals = new byte[primDataSize];
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2306
            bin.readFully(primVals, 0, primDataSize, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
        }
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2308
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2309
        Object[] objVals = null;
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2310
        int numObjFields = desc.getNumObjFields();
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2311
        if (numObjFields > 0) {
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2312
            int objHandle = passHandle;
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2313
            ObjectStreamField[] fields = desc.getFields(false);
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2314
            objVals = new Object[numObjFields];
22936
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2315
            int numPrimFields = fields.length - objVals.length;
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2316
            for (int i = 0; i < objVals.length; i++) {
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2317
                ObjectStreamField f = fields[numPrimFields + i];
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2318
                objVals[i] = readObject0(f.isUnshared());
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2319
                if (f.getField() != null) {
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2320
                    handles.markDependency(objHandle, passHandle);
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2321
                }
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2322
            }
1263cd9144d7 8033778: ObjectIn/OutputStream improvements
chegar
parents: 21334
diff changeset
  2323
            passHandle = objHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        }
30911
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2325
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2326
        return new FieldValues(primVals, objVals);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2327
    }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2328
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2329
    /** Throws ClassCastException if any value is not assignable. */
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2330
    private void defaultCheckFieldValues(Object obj, ObjectStreamClass desc,
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2331
                                         FieldValues values) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2332
        Object[] objectValues = values.objValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2333
        if (objectValues != null)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2334
            desc.checkObjFieldValueTypes(obj, objectValues);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2335
    }
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2336
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2337
    /** Sets field values in obj. */
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2338
    private void defaultSetFieldValues(Object obj, ObjectStreamClass desc,
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2339
                                       FieldValues values) {
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2340
        byte[] primValues = values.primValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2341
        Object[] objectValues = values.objValues;
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2342
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2343
        if (primValues != null)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2344
            desc.setPrimFieldValues(obj, primValues);
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2345
        if (objectValues != null)
99e027461f4a 8071474: Better failure atomicity for default read object
chegar
parents: 29986
diff changeset
  2346
            desc.setObjFieldValues(obj, objectValues);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
     * Reads in and returns IOException that caused serialization to abort.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
     * All stream state is discarded prior to reading in fatal exception.  Sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
     * passHandle to fatal exception's handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
    private IOException readFatalException() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        if (bin.readByte() != TC_EXCEPTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        return (IOException) readObject0(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
     * If recursion depth is 0, clears internal data structures; otherwise,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
     * throws a StreamCorruptedException.  This method is called when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
     * TC_RESET typecode is encountered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    private void handleReset() throws StreamCorruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
        if (depth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
            throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                "unexpected reset; recursion depth: " + depth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
        clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
     * Converts specified span of bytes into float values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
    // REMIND: remove once hotspot inlines Float.intBitsToFloat
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
    private static native void bytesToFloats(byte[] src, int srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                                             float[] dst, int dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                                             int nfloats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     * Converts specified span of bytes into double values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    // REMIND: remove once hotspot inlines Double.longBitsToDouble
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
    private static native void bytesToDoubles(byte[] src, int srcpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                                              double[] dst, int dstpos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                                              int ndoubles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
    /**
42440
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
  2392
     * Returns the first non-null and non-platform class loader (not counting
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
  2393
     * class loaders of generated reflection implementation classes) up the
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
  2394
     * execution stack, or the platform class loader if only code from the
ec843594bcc6 8169653: Restore ObjectInputStream::resolveClass call stack default search order
chegar
parents: 41230
diff changeset
  2395
     * bootstrap and platform class loader is on the stack.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     */
10797
f51518b30c84 7104209: Cleanup and remove librmi (native library)
chegar
parents: 7803
diff changeset
  2397
    private static ClassLoader latestUserDefinedLoader() {
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 33674
diff changeset
  2398
        return jdk.internal.misc.VM.latestUserDefinedLoader();
10797
f51518b30c84 7104209: Cleanup and remove librmi (native library)
chegar
parents: 7803
diff changeset
  2399
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * Default GetField implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
    private class GetFieldImpl extends GetField {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
        /** class descriptor describing serializable fields */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
        private final ObjectStreamClass desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
        /** primitive field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
        private final byte[] primVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
        /** object field values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
        private final Object[] objVals;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
        /** object field value handles */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
        private final int[] objHandles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
         * Creates GetFieldImpl object for reading fields defined in given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
         * class descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
        GetFieldImpl(ObjectStreamClass desc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
            this.desc = desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            primVals = new byte[desc.getPrimDataSize()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
            objVals = new Object[desc.getNumObjFields()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
            objHandles = new int[objVals.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
        public ObjectStreamClass getObjectStreamClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
            return desc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        public boolean defaulted(String name) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            return (getFieldOffset(name, null) < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        public boolean get(String name, boolean val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            int off = getFieldOffset(name, Boolean.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
            return (off >= 0) ? Bits.getBoolean(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
        public byte get(String name, byte val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
            int off = getFieldOffset(name, Byte.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            return (off >= 0) ? primVals[off] : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        public char get(String name, char val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
            int off = getFieldOffset(name, Character.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            return (off >= 0) ? Bits.getChar(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        public short get(String name, short val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
            int off = getFieldOffset(name, Short.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            return (off >= 0) ? Bits.getShort(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
        public int get(String name, int val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            int off = getFieldOffset(name, Integer.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            return (off >= 0) ? Bits.getInt(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        public float get(String name, float val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            int off = getFieldOffset(name, Float.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            return (off >= 0) ? Bits.getFloat(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        public long get(String name, long val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            int off = getFieldOffset(name, Long.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
            return (off >= 0) ? Bits.getLong(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        public double get(String name, double val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            int off = getFieldOffset(name, Double.TYPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
            return (off >= 0) ? Bits.getDouble(primVals, off) : val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
        public Object get(String name, Object val) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
            int off = getFieldOffset(name, Object.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
            if (off >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                int objHandle = objHandles[off];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                handles.markDependency(passHandle, objHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                return (handles.lookupException(objHandle) == null) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                    objVals[off] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
                return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
         * Reads primitive and object field values from stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        void readFields() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
            bin.readFully(primVals, 0, primVals.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            int oldHandle = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
            ObjectStreamField[] fields = desc.getFields(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            int numPrimFields = fields.length - objVals.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
            for (int i = 0; i < objVals.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                objVals[i] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
                    readObject0(fields[numPrimFields + i].isUnshared());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                objHandles[i] = passHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
            passHandle = oldHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
         * Returns offset of field with given name and type.  A specified type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
         * of null matches all types, Object.class matches all non-primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
         * types, and any other non-null type matches assignable types only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
         * If no matching field is found in the (incoming) class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
         * descriptor but a matching field is present in the associated local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
         * class descriptor, returns -1.  Throws IllegalArgumentException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
         * neither incoming nor local class descriptor contains a match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
         */
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  2512
        private int getFieldOffset(String name, Class<?> type) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            ObjectStreamField field = desc.getField(name, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            if (field != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                return field.getOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
            } else if (desc.getLocalDesc().getField(name, type) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                throw new IllegalArgumentException("no such field " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                                                   " with type " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * Prioritized list of callbacks to be performed once object graph has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
     * completely deserialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
    private static class ValidationList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        private static class Callback {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
            final ObjectInputValidation obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
            final int priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            Callback next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            final AccessControlContext acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
            Callback(ObjectInputValidation obj, int priority, Callback next,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                AccessControlContext acc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                this.obj = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                this.priority = priority;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                this.next = next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                this.acc = acc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
        /** linked list of callbacks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        private Callback list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
         * Creates new (empty) ValidationList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
        ValidationList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
         * Registers callback.  Throws InvalidObjectException if callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
         * object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
        void register(ObjectInputValidation obj, int priority)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
            throws InvalidObjectException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
            if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                throw new InvalidObjectException("null callback");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
            Callback prev = null, cur = list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            while (cur != null && priority < cur.priority) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                prev = cur;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
                cur = cur.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
            AccessControlContext acc = AccessController.getContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
            if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                prev.next = new Callback(obj, priority, cur, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                list = new Callback(obj, priority, list, acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
         * Invokes all registered callbacks and clears the callback list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
         * Callbacks with higher priorities are called first; those with equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
         * priorities may be called in any order.  If any of the callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
         * throws an InvalidObjectException, the callback process is terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
         * and the exception propagated upwards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        void doCallbacks() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
                while (list != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
                    AccessController.doPrivileged(
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2591
                        new PrivilegedExceptionAction<Void>()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
                    {
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
  2593
                        public Void run() throws InvalidObjectException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
                            list.obj.validateObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
                    }, list.acc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
                    list = list.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            } catch (PrivilegedActionException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
                list = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
                throw (InvalidObjectException) ex.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
         * Resets the callback list to its initial (empty) state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
        public void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            list = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
    /**
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2615
     * Hold a snapshot of values to be passed to an ObjectInputFilter.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2616
     */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2617
    static class FilterValues implements ObjectInputFilter.FilterInfo {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2618
        final Class<?> clazz;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2619
        final long arrayLength;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2620
        final long totalObjectRefs;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2621
        final long depth;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2622
        final long streamBytes;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2623
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2624
        public FilterValues(Class<?> clazz, long arrayLength, long totalObjectRefs,
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2625
                            long depth, long streamBytes) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2626
            this.clazz = clazz;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2627
            this.arrayLength = arrayLength;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2628
            this.totalObjectRefs = totalObjectRefs;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2629
            this.depth = depth;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2630
            this.streamBytes = streamBytes;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2631
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2632
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2633
        @Override
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2634
        public Class<?> serialClass() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2635
            return clazz;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2636
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2637
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2638
        @Override
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2639
        public long arrayLength() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2640
            return arrayLength;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2641
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2642
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2643
        @Override
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2644
        public long references() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2645
            return totalObjectRefs;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2646
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2647
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2648
        @Override
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2649
        public long depth() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2650
            return depth;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2651
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2652
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2653
        @Override
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2654
        public long streamBytes() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2655
            return streamBytes;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2656
        }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2657
    }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2658
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2659
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
     * Input stream supporting single-byte peek operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
    private static class PeekInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
        /** underlying stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        private final InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        /** peeked byte */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
        private int peekb = -1;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2668
        /** total bytes read from the stream */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2669
        private long totalBytesRead = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
         * Creates new PeekInputStream on top of given underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
        PeekInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
            this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
         * Peeks at next byte value in stream.  Similar to read(), except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
         * that it does not consume the read value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
        int peek() throws IOException {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2683
            if (peekb >= 0) {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2684
                return peekb;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2685
            }
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2686
            peekb = in.read();
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2687
            totalBytesRead += peekb >= 0 ? 1 : 0;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2688
            return peekb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            if (peekb >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                int v = peekb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                peekb = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
            } else {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2697
                int nbytes = in.read();
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2698
                totalBytesRead += nbytes >= 0 ? 1 : 0;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2699
                return nbytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
        public int read(byte[] b, int off, int len) throws IOException {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2704
            int nbytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            } else if (peekb < 0) {
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2708
                nbytes = in.read(b, off, len);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2709
                totalBytesRead += nbytes >= 0 ? nbytes : 0;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2710
                return nbytes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                b[off++] = (byte) peekb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                peekb = -1;
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2715
                nbytes = in.read(b, off, len);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2716
                totalBytesRead += nbytes >= 0 ? nbytes : 0;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2717
                return (nbytes >= 0) ? (nbytes + 1) : 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
        void readFully(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
            int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
            while (n < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                int count = read(b, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                if (count < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                    throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                n += count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
        public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
            if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
            int skipped = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
            if (peekb >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                peekb = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                skipped++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                n--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
            }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2742
            n = skipped + in.skip(n);
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2743
            totalBytesRead += n;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2744
            return n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
            return in.available() + ((peekb >= 0) ? 1 : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2754
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2755
        public long getBytesRead() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2756
            return totalBytesRead;
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  2757
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
29220
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2760
    private static final Unsafe UNSAFE = Unsafe.getUnsafe();
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2761
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2762
    /**
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2763
     * Performs a "freeze" action, required to adhere to final field semantics.
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2764
     *
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2765
     * <p> This method can be called unconditionally before returning the graph,
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2766
     * from the topmost readObject call, since it is expected that the
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2767
     * additional cost of the freeze action is negligible compared to
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2768
     * reconstituting even the most simple graph.
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2769
     *
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2770
     * <p> Nested calls to readObject do not issue freeze actions because the
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2771
     * sub-graph returned from a nested call is not guaranteed to be fully
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2772
     * initialized yet (possible cycles).
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2773
     */
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2774
    private void freeze() {
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2775
        // Issue a StoreStore|StoreLoad fence, which is at least sufficient
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2776
        // to provide final-freeze semantics.
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2777
        UNSAFE.storeFence();
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2778
    }
b07abc731618 8074022: Serialization should issue a freeze action after reconstituting a graph that contains objects with final fields
chegar
parents: 28116
diff changeset
  2779
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * Input stream with two modes: in default mode, inputs data written in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * same format as DataOutputStream; in "block data" mode, inputs data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * bracketed by block data markers (see object serialization specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     * for details).  Buffering depends on block data mode: when in default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * mode, no data is buffered in advance; when in block data mode, all data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * for the current data block is read in at once (and buffered).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
    private class BlockDataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
        extends InputStream implements DataInput
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
        /** maximum data block length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
        private static final int MAX_BLOCK_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        /** maximum data block header length */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
        private static final int MAX_HEADER_SIZE = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
        /** (tunable) length of char buffer (for reading strings) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        private static final int CHAR_BUF_SIZE = 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
        /** readBlockHeader() return value indicating header read may block */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
        private static final int HEADER_BLOCKED = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        /** buffer for reading general/block data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        private final byte[] buf = new byte[MAX_BLOCK_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
        /** buffer for reading block data headers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
        private final byte[] hbuf = new byte[MAX_HEADER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
        /** char buffer for fast string reads */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
        private final char[] cbuf = new char[CHAR_BUF_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
        /** block data mode */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
        private boolean blkmode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        // block data state fields; values meaningful only when blkmode true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        /** current offset into buf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
        private int pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
        /** end offset of valid data in buf, or -1 if no more block data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
        private int end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
        /** number of bytes in current block yet to be read from stream */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        private int unread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        /** underlying stream (wrapped in peekable filter stream) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        private final PeekInputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        /** loopback stream (for data reads that span data blocks) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
        private final DataInputStream din;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
         * Creates new BlockDataInputStream on top of given underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
         * Block data mode is turned off by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
        BlockDataInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
            this.in = new PeekInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
            din = new DataInputStream(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
         * Sets block data mode to the given mode (true == on, false == off)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
         * and returns the previous mode value.  If the new mode is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
         * the old mode, no action is taken.  Throws IllegalStateException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
         * block data mode is being switched from on to off while unconsumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
         * block data is still present in the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        boolean setBlockDataMode(boolean newmode) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
            if (blkmode == newmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
                return blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
            if (newmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
                end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
                unread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
            } else if (pos < end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
                throw new IllegalStateException("unread block data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
            blkmode = newmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
            return !blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
         * Returns true if the stream is currently in block data mode, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
         * otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
        boolean getBlockDataMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
            return blkmode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
         * If in block data mode, skips to the end of the current group of data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
         * blocks (but does not unset block data mode).  If not in block data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         * mode, throws an IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        void skipBlockData() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
                throw new IllegalStateException("not in block data mode");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
            while (end >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                refill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
         * Attempts to read in the next block data header (if any).  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
         * canBlock is false and a full header cannot be read without possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
         * blocking, returns HEADER_BLOCKED, else if the next element in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
         * stream is a block data header, returns the block data length
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
         * specified by the header, else returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
        private int readBlockHeader(boolean canBlock) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
            if (defaultDataEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2886
                 * Fix for 4360508: stream is currently at the end of a field
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
                 * value block written via default serialization; since there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
                 * is no terminating TC_ENDBLOCKDATA tag, simulate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
                 * end-of-custom-data behavior explicitly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2893
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2894
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2895
                    int avail = canBlock ? Integer.MAX_VALUE : in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
                    if (avail == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
                        return HEADER_BLOCKED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
                    int tc = in.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
                    switch (tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
                        case TC_BLOCKDATA:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
                            if (avail < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
                                return HEADER_BLOCKED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
                            in.readFully(hbuf, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
                            return hbuf[1] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
                        case TC_BLOCKDATALONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
                            if (avail < 5) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
                                return HEADER_BLOCKED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
                            in.readFully(hbuf, 0, 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
                            int len = Bits.getInt(hbuf, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
                            if (len < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
                                throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                                    "illegal block data header length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
                                    len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                            return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                         * TC_RESETs may occur in between data blocks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                         * Unfortunately, this case must be parsed at a lower
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                         * level than other typecodes, since primitive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
                         * reads may span data blocks separated by a TC_RESET.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
                        case TC_RESET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
                            in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                            handleReset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                            if (tc >= 0 && (tc < TC_BASE || tc > TC_MAX)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                                throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
                                    String.format("invalid type code: %02X",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                                    tc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            } catch (EOFException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
                throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
                    "unexpected EOF while reading block data header");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
         * Refills internal buffer buf with block data.  Any data in buf at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
         * time of the call is considered consumed.  Sets the pos, end, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
         * unread fields to reflect the new amount of available block data; if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
         * the next element in the stream is not a data block, sets pos and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
         * unread to 0 and end to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
        private void refill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                    if (unread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                        int n =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                            in.read(buf, 0, Math.min(unread, MAX_BLOCK_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                        if (n >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
                            end = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
                            unread -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                            throw new StreamCorruptedException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
                                "unexpected EOF in middle of data block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
                        int n = readBlockHeader(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
                        if (n >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
                            end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
                            unread = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                            end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                            unread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
                } while (pos == end);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
            } catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                unread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                throw ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
         * If in block data mode, returns the number of unconsumed bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
         * remaining in the current data block.  If not in block data mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
         * throws an IllegalStateException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
        int currentBlockRemaining() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
                return (end >= 0) ? (end - pos) + unread : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
                throw new IllegalStateException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
         * Peeks at (but does not consume) and returns the next byte value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
         * the stream, or -1 if the end of the stream/block data (if in block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
         * data mode) has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
        int peek() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
                if (pos == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
                    refill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
                return (end >= 0) ? (buf[pos] & 0xFF) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
                return in.peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
         * Peeks at (but does not consume) and returns the next byte value in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
         * the stream, or throws EOFException if end of stream/block data has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
         * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
        byte peekByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
            int val = peek();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
            if (val < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
            return (byte) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
        /* ----------------- generic input stream methods ------------------ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
         * The following methods are equivalent to their counterparts in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
         * InputStream, except that they interpret data block boundaries and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
         * read the requested data from within data blocks when in block data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
         * mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
                if (pos == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
                    refill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
                return (end >= 0) ? (buf[pos++] & 0xFF) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
                return in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
        public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
            return read(b, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
        public long skip(long len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
            long remain = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
            while (remain > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
                if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
                    if (pos == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
                        refill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
                    if (end < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
                    int nread = (int) Math.min(remain, end - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
                    remain -= nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
                    pos += nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
                    int nread = (int) Math.min(remain, MAX_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
                    if ((nread = in.read(buf, 0, nread)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
                    remain -= nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            return len - remain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
                if ((pos == end) && (unread == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
                    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
                    while ((n = readBlockHeader(false)) == 0) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
                    switch (n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
                        case HEADER_BLOCKED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
                        case -1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3088
                            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
                            end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
                            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
                            end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
                            unread = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
                // avoid unnecessary call to in.available() if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
                int unreadAvail = (unread > 0) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
                    Math.min(in.available(), unread) : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
                return (end >= 0) ? (end - pos) + unreadAvail : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
                return in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
            if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
                end = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
                unread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
         * Attempts to read len bytes into byte array b at offset off.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
         * the number of bytes read, or -1 if the end of stream/block data has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
         * been reached.  If copy is true, reads values into an intermediate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
         * buffer before copying them to b (to avoid exposing a reference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
         * b).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
        int read(byte[] b, int off, int len, boolean copy) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
            if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
            } else if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
                if (pos == end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
                    refill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
                if (end < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
                int nread = Math.min(len, end - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
                System.arraycopy(buf, pos, b, off, nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
                pos += nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
                return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
            } else if (copy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
                int nread = in.read(buf, 0, Math.min(len, MAX_BLOCK_SIZE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
                if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
                    System.arraycopy(buf, 0, b, off, nread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
                return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
                return in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
        /* ----------------- primitive data input methods ------------------ */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
         * The following methods are equivalent to their counterparts in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
         * DataInputStream, except that they interpret data block boundaries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
         * and read the requested data from within data blocks when in block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
         * data mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
        public void readFully(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
            readFully(b, 0, b.length, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
        public void readFully(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
            readFully(b, off, len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
        public void readFully(byte[] b, int off, int len, boolean copy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                int n = read(b, off, len, copy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
                if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                    throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
                off += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
                len -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        public int skipBytes(int n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
            return din.skipBytes(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
        public boolean readBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
            int v = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
            if (v < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
            return (v != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        public byte readByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
            int v = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3192
            if (v < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3193
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
            return (byte) v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3197
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3198
        public int readUnsignedByte() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3199
            int v = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3200
            if (v < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3201
                throw new EOFException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3203
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
        public char readChar() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
                in.readFully(buf, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
            } else if (end - pos < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
                return din.readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
            char v = Bits.getChar(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3218
        public short readShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3221
                in.readFully(buf, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
            } else if (end - pos < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                return din.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            short v = Bits.getShort(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
        public int readUnsignedShort() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                in.readFully(buf, 0, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3234
            } else if (end - pos < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                return din.readUnsignedShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
            int v = Bits.getShort(buf, pos) & 0xFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
        public int readInt() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                in.readFully(buf, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
            } else if (end - pos < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                return din.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
            int v = Bits.getInt(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        public float readFloat() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
                in.readFully(buf, 0, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            } else if (end - pos < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                return din.readFloat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
            float v = Bits.getFloat(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
            pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
        public long readLong() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                in.readFully(buf, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
            } else if (end - pos < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                return din.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
            long v = Bits.getLong(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
        public double readDouble() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
                pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
                in.readFully(buf, 0, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
            } else if (end - pos < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                return din.readDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
            double v = Bits.getDouble(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
            pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
            return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
        public String readUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
            return readUTFBody(readUnsignedShort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 10797
diff changeset
  3294
        @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
        public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
            return din.readLine();      // deprecated, not worth optimizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
        /* -------------- primitive data array input methods --------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
         * The following methods read in spans of primitive data values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
         * Though equivalent to calling the corresponding primitive read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
         * methods repeatedly, these methods are optimized for reading groups
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
         * of primitive data values more efficiently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        void readBooleans(boolean[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
            int stop, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                    int span = Math.min(endoff - off, MAX_BLOCK_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                    in.readFully(buf, 0, span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                    stop = off + span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                } else if (end - pos < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                    v[off++] = din.readBoolean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                    stop = Math.min(endoff, off + end - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                    v[off++] = Bits.getBoolean(buf, pos++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
        void readChars(char[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
            int stop, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                    int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
                    in.readFully(buf, 0, span << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                    stop = off + span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
                } else if (end - pos < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
                    v[off++] = din.readChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
                    stop = Math.min(endoff, off + ((end - pos) >> 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                    v[off++] = Bits.getChar(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                    pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
        void readShorts(short[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
            int stop, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                    int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                    in.readFully(buf, 0, span << 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                    stop = off + span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                } else if (end - pos < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
                    v[off++] = din.readShort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                    stop = Math.min(endoff, off + ((end - pos) >> 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                    v[off++] = Bits.getShort(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                    pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
        void readInts(int[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
            int stop, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                    int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                    in.readFully(buf, 0, span << 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                    stop = off + span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                } else if (end - pos < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                    v[off++] = din.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                    stop = Math.min(endoff, off + ((end - pos) >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                    v[off++] = Bits.getInt(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                    pos += 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
        void readFloats(float[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
            int span, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
                    span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
                    in.readFully(buf, 0, span << 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                } else if (end - pos < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
                    v[off++] = din.readFloat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                    span = Math.min(endoff - off, ((end - pos) >> 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
                bytesToFloats(buf, pos, v, off, span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
                off += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
                pos += span << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
        void readLongs(long[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
            int stop, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                    int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                    in.readFully(buf, 0, span << 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                    stop = off + span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                } else if (end - pos < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
                    v[off++] = din.readLong();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                    stop = Math.min(endoff, off + ((end - pos) >> 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                while (off < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                    v[off++] = Bits.getLong(buf, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                    pos += 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
        void readDoubles(double[] v, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            int span, endoff = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
            while (off < endoff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                    span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                    in.readFully(buf, 0, span << 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                    pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                } else if (end - pos < 8) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                    v[off++] = din.readDouble();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                    span = Math.min(endoff - off, ((end - pos) >> 3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
                bytesToDoubles(buf, pos, v, off, span);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
                off += span;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                pos += span << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
         * Reads in string written in "long" UTF format.  "Long" UTF format is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
         * identical to standard UTF, except that it uses an 8 byte header
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
         * (instead of the standard 2 bytes) to convey the UTF encoding length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
        String readLongUTF() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
            return readUTFBody(readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
         * Reads in the "body" (i.e., the UTF representation minus the 2-byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
         * or 8-byte length header) of a UTF encoding, which occupies the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
         * utflen bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
        private String readUTFBody(long utflen) throws IOException {
35788
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3471
            StringBuilder sbuf;
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3472
            if (utflen > 0 && utflen < Integer.MAX_VALUE) {
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3473
                // a reasonable initial capacity based on the UTF length
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3474
                int initialCapacity = Math.min((int)utflen, 0xFFFF);
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3475
                sbuf = new StringBuilder(initialCapacity);
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3476
            } else {
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3477
                sbuf = new StringBuilder();
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3478
            }
04c8859c969b 8134424: BlockDataInputStream.readUTFBody: size local StringBuffer with the given length
chegar
parents: 34882
diff changeset
  3479
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            if (!blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
                end = pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
            while (utflen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
                int avail = end - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
                if (avail >= 3 || (long) avail == utflen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
                    utflen -= readUTFSpan(sbuf, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
                    if (blkmode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
                        // near block boundary, read one byte at a time
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                        utflen -= readUTFChar(sbuf, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
                        // shift and refill buffer manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
                        if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                            System.arraycopy(buf, pos, buf, 0, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                        end = (int) Math.min(MAX_BLOCK_SIZE, utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                        in.readFully(buf, avail, end - avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
            return sbuf.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
         * Reads span of UTF-encoded characters out of internal buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
         * (starting at offset pos and ending at or before offset end),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
         * consuming no more than utflen bytes.  Appends read characters to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
         * sbuf.  Returns the number of bytes consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
        private long readUTFSpan(StringBuilder sbuf, long utflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
            int cpos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
            int start = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
            int avail = Math.min(end - pos, CHAR_BUF_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
            // stop short of last char unless all of utf bytes in buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
            int stop = pos + ((utflen > avail) ? avail - 2 : (int) utflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
            boolean outOfBounds = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                while (pos < stop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    int b1, b2, b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                    b1 = buf[pos++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                    switch (b1 >> 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
                        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
                        case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
                        case 6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                        case 7:   // 1 byte format: 0xxxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                            cbuf[cpos++] = (char) b1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                        case 12:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                        case 13:  // 2 byte format: 110xxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                            b2 = buf[pos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                            if ((b2 & 0xC0) != 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
                                throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                            cbuf[cpos++] = (char) (((b1 & 0x1F) << 6) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
                                                   ((b2 & 0x3F) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
                        case 14:  // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
                            b3 = buf[pos + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
                            b2 = buf[pos + 0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
                            pos += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
                            if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
                                throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                            cbuf[cpos++] = (char) (((b1 & 0x0F) << 12) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                                                   ((b2 & 0x3F) << 6) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
                                                   ((b3 & 0x3F) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                        default:  // 10xx xxxx, 1111 xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                            throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            } catch (ArrayIndexOutOfBoundsException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                outOfBounds = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
                if (outOfBounds || (pos - start) > utflen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
                     * Fix for 4450867: if a malformed utf char causes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
                     * conversion loop to scan past the expected end of the utf
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
                     * string, only consume the expected number of utf bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
                    pos = start + (int) utflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
                    throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
            sbuf.append(cbuf, 0, cpos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            return pos - start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
         * Reads in single UTF-encoded character one byte at a time, appends
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
         * the character to sbuf, and returns the number of bytes consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
         * This method is used when reading in UTF strings written in block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
         * data mode to handle UTF-encoded characters which (potentially)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
         * straddle block-data boundaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
        private int readUTFChar(StringBuilder sbuf, long utflen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
            int b1, b2, b3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            b1 = readByte() & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
            switch (b1 >> 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3598
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3599
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3600
                case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3601
                case 5:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3602
                case 6:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3603
                case 7:     // 1 byte format: 0xxxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3604
                    sbuf.append((char) b1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
                    return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
                case 12:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
                case 13:    // 2 byte format: 110xxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
                    if (utflen < 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                        throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                    b2 = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
                    if ((b2 & 0xC0) != 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                        throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
                    sbuf.append((char) (((b1 & 0x1F) << 6) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
                                        ((b2 & 0x3F) << 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
                    return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
                case 14:    // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                    if (utflen < 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
                        if (utflen == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
                            readByte();         // consume remaining byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
                        throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
                    b2 = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
                    b3 = readByte();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
                    if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
                        throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
                    sbuf.append((char) (((b1 & 0x0F) << 12) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
                                        ((b2 & 0x3F) << 6) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                                        ((b3 & 0x3F) << 0)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
                    return 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
                default:   // 10xx xxxx, 1111 xxxx
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
                    throw new UTFDataFormatException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
        }
41230
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3641
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3642
        /**
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3643
         * Returns the number of bytes read from the input stream.
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3644
         * @return the number of bytes read from the input stream
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3645
         */
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3646
        long getBytesRead() {
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3647
            return in.getBytesRead();
0a8c1ba2b6fb 8155760: Implement Serialization Filtering
rriggs
parents: 40410
diff changeset
  3648
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
     * Unsynchronized table which tracks wire handle to object mappings, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
     * well as ClassNotFoundExceptions associated with deserialized objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
     * This class implements an exception-propagation algorithm for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
     * determining which objects should have ClassNotFoundExceptions associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
     * with them, taking into account cycles and discontinuities (e.g., skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
     * fields) in the object graph.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
     * <p>General use of the table is as follows: during deserialization, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
     * given object is first assigned a handle by calling the assign method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
     * This method leaves the assigned handle in an "open" state, wherein
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
     * dependencies on the exception status of other handles can be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
     * by calling the markDependency method, or an exception can be directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
     * associated with the handle by calling markException.  When a handle is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
     * tagged with an exception, the HandleTable assumes responsibility for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
     * propagating the exception to any other objects which depend
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
     * (transitively) on the exception-tagged object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
     * <p>Once all exception information/dependencies for the handle have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
     * registered, the handle should be "closed" by calling the finish method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
     * on it.  The act of finishing a handle allows the exception propagation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
     * algorithm to aggressively prune dependency links, lessening the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
     * performance/memory impact of exception tracking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
     * <p>Note that the exception propagation algorithm used depends on handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3676
     * being assigned/finished in LIFO order; however, for simplicity as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
     * as memory conservation, it does not enforce this constraint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
    // REMIND: add full description of exception propagation algorithm?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
    private static class HandleTable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
        /* status codes indicating whether object has associated exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
        private static final byte STATUS_OK = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
        private static final byte STATUS_UNKNOWN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
        private static final byte STATUS_EXCEPTION = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
        /** array mapping handle -> object status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
        byte[] status;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
        /** array mapping handle -> object/exception (depending on status) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
        Object[] entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
        /** array mapping handle -> list of dependent handles (if any) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
        HandleList[] deps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
        /** lowest unresolved dependency */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
        int lowDep = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
        /** number of handles in table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
        int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
         * Creates handle table with the given initial capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
        HandleTable(int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
            status = new byte[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
            entries = new Object[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
            deps = new HandleList[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
         * Assigns next available handle to given object, and returns assigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
         * handle.  Once object has been completely deserialized (and all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
         * dependencies on other objects identified), the handle should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
         * "closed" by passing it to finish().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
        int assign(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
            if (size >= entries.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
                grow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
            status[size] = STATUS_UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
            entries[size] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
            return size++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
         * Registers a dependency (in exception status) of one handle on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
         * another.  The dependent handle must be "open" (i.e., assigned, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
         * not finished yet).  No action is taken if either dependent or target
33403
d2bfc26f26cc 6823565: Excessive use of HandleList class in de-serialization code causes OutOfMemory
redestad
parents: 32649
diff changeset
  3726
         * handle is NULL_HANDLE. Additionally, no action is taken if the
d2bfc26f26cc 6823565: Excessive use of HandleList class in de-serialization code causes OutOfMemory
redestad
parents: 32649
diff changeset
  3727
         * dependent and target are the same.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
        void markDependency(int dependent, int target) {
33403
d2bfc26f26cc 6823565: Excessive use of HandleList class in de-serialization code causes OutOfMemory
redestad
parents: 32649
diff changeset
  3730
            if (dependent == target || dependent == NULL_HANDLE || target == NULL_HANDLE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
            switch (status[dependent]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
                case STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
                    switch (status[target]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
                        case STATUS_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                            // ignore dependencies on objs with no exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                        case STATUS_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                            // eagerly propagate exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                            markException(dependent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
                                (ClassNotFoundException) entries[target]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3745
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3747
                        case STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3748
                            // add to dependency list of target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3749
                            if (deps[target] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3750
                                deps[target] = new HandleList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3751
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3752
                            deps[target].add(dependent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3754
                            // remember lowest unresolved target seen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3755
                            if (lowDep < 0 || lowDep > target) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3756
                                lowDep = target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3757
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3758
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3760
                        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3761
                            throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3762
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3763
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3764
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3765
                case STATUS_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3766
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3768
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3769
                    throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3770
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3772
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3773
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3774
         * Associates a ClassNotFoundException (if one not already associated)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3775
         * with the currently active handle and propagates it to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3776
         * referencing objects as appropriate.  The specified handle must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3777
         * "open" (i.e., assigned, but not finished yet).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3778
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3779
        void markException(int handle, ClassNotFoundException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3780
            switch (status[handle]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3781
                case STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3782
                    status[handle] = STATUS_EXCEPTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3783
                    entries[handle] = ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3785
                    // propagate exception to dependents
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3786
                    HandleList dlist = deps[handle];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3787
                    if (dlist != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3788
                        int ndeps = dlist.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3789
                        for (int i = 0; i < ndeps; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3790
                            markException(dlist.get(i), ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3791
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3792
                        deps[handle] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3793
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3794
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3796
                case STATUS_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3799
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3800
                    throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3804
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3805
         * Marks given handle as finished, meaning that no new dependencies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3806
         * will be marked for handle.  Calls to the assign and finish methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3807
         * must occur in LIFO order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3808
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3809
        void finish(int handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3810
            int end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3811
            if (lowDep < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3812
                // no pending unknowns, only resolve current handle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3813
                end = handle + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3814
            } else if (lowDep >= handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3815
                // pending unknowns now clearable, resolve all upward handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3816
                end = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3817
                lowDep = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3818
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3819
                // unresolved backrefs present, can't resolve anything yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3820
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3821
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3823
            // change STATUS_UNKNOWN -> STATUS_OK in selected span of handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3824
            for (int i = handle; i < end; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3825
                switch (status[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3826
                    case STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3827
                        status[i] = STATUS_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3828
                        deps[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3829
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3831
                    case STATUS_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3832
                    case STATUS_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3833
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3835
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3836
                        throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3840
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3841
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3842
         * Assigns a new object to the given handle.  The object previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3843
         * associated with the handle is forgotten.  This method has no effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3844
         * if the given handle already has an exception associated with it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3845
         * This method may be called at any time after the handle is assigned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3846
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3847
        void setObject(int handle, Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3848
            switch (status[handle]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3849
                case STATUS_UNKNOWN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3850
                case STATUS_OK:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3851
                    entries[handle] = obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3852
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3854
                case STATUS_EXCEPTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3855
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3857
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3858
                    throw new InternalError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3859
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3862
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3863
         * Looks up and returns object associated with the given handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3864
         * Returns null if the given handle is NULL_HANDLE, or if it has an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3865
         * associated ClassNotFoundException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3866
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3867
        Object lookupObject(int handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3868
            return (handle != NULL_HANDLE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3869
                    status[handle] != STATUS_EXCEPTION) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3870
                entries[handle] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3873
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3874
         * Looks up and returns ClassNotFoundException associated with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3875
         * given handle.  Returns null if the given handle is NULL_HANDLE, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3876
         * if there is no ClassNotFoundException associated with the handle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3877
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3878
        ClassNotFoundException lookupException(int handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3879
            return (handle != NULL_HANDLE &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3880
                    status[handle] == STATUS_EXCEPTION) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3881
                (ClassNotFoundException) entries[handle] : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3884
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3885
         * Resets table to its initial state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3886
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3887
        void clear() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3888
            Arrays.fill(status, 0, size, (byte) 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3889
            Arrays.fill(entries, 0, size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3890
            Arrays.fill(deps, 0, size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3891
            lowDep = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3892
            size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3894
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3895
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3896
         * Returns number of handles registered in table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3897
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3898
        int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3899
            return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3901
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3902
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3903
         * Expands capacity of internal arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3904
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3905
        private void grow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3906
            int newCapacity = (entries.length << 1) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3907
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3908
            byte[] newStatus = new byte[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3909
            Object[] newEntries = new Object[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3910
            HandleList[] newDeps = new HandleList[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3912
            System.arraycopy(status, 0, newStatus, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3913
            System.arraycopy(entries, 0, newEntries, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3914
            System.arraycopy(deps, 0, newDeps, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3916
            status = newStatus;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3917
            entries = newEntries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3918
            deps = newDeps;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3919
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3921
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3922
         * Simple growable list of (integer) handles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3923
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3924
        private static class HandleList {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3925
            private int[] list = new int[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3926
            private int size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3928
            public HandleList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3931
            public void add(int handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3932
                if (size >= list.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3933
                    int[] newList = new int[list.length << 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3934
                    System.arraycopy(list, 0, newList, 0, list.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3935
                    list = newList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3936
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3937
                list[size++] = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3940
            public int get(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3941
                if (index >= size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3942
                    throw new ArrayIndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3943
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3944
                return list[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3946
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3947
            public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3948
                return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3949
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3952
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3953
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3954
     * Method for cloning arrays in case of using unsharing reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3955
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3956
    private static Object cloneArray(Object array) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3957
        if (array instanceof Object[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3958
            return ((Object[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3959
        } else if (array instanceof boolean[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3960
            return ((boolean[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3961
        } else if (array instanceof byte[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3962
            return ((byte[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3963
        } else if (array instanceof char[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3964
            return ((char[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3965
        } else if (array instanceof double[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3966
            return ((double[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3967
        } else if (array instanceof float[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3968
            return ((float[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3969
        } else if (array instanceof int[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3970
            return ((int[]) array).clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3971
        } else if (array instanceof long[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3972
            return ((long[]) array).clone();
7535
53c289115e11 6990094: ObjectInputStream cloneArray doesn't handle short[]
darcy
parents: 7030
diff changeset
  3973
        } else if (array instanceof short[]) {
53c289115e11 6990094: ObjectInputStream cloneArray doesn't handle short[]
darcy
parents: 7030
diff changeset
  3974
            return ((short[]) array).clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3975
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3976
            throw new AssertionError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3977
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3978
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3979
37584
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3980
    private void validateDescriptor(ObjectStreamClass descriptor) {
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3981
        ObjectStreamClassValidator validating = validator;
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3982
        if (validating != null) {
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3983
            validating.validateDescriptor(descriptor);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3984
        }
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3985
    }
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3986
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3987
    // controlled access to ObjectStreamClassValidator
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3988
    private volatile ObjectStreamClassValidator validator;
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3989
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3990
    private static void setValidator(ObjectInputStream ois, ObjectStreamClassValidator validator) {
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3991
        ois.validator = validator;
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3992
    }
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3993
    static {
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3994
        SharedSecrets.setJavaObjectInputStreamAccess(ObjectInputStream::setValidator);
0cc00d7a0755 8144430: Improve JMX connections
sjiang
parents: 37583
diff changeset
  3995
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3996
}