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