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