0N/A/*
2989N/A * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.io.ObjectStreamClass.WeakClassKey;
0N/Aimport java.lang.ref.ReferenceQueue;
0N/Aimport java.lang.reflect.Array;
0N/Aimport java.lang.reflect.Modifier;
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport java.security.AccessControlContext;
0N/Aimport java.security.AccessController;
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.PrivilegedActionException;
0N/Aimport java.security.PrivilegedExceptionAction;
0N/Aimport java.util.Arrays;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.concurrent.ConcurrentHashMap;
0N/Aimport java.util.concurrent.ConcurrentMap;
0N/Aimport java.util.concurrent.atomic.AtomicBoolean;
0N/Aimport static java.io.ObjectStreamClass.processQueue;
6017N/Aimport sun.reflect.misc.ReflectUtil;
0N/A
0N/A/**
0N/A * An ObjectInputStream deserializes primitive data and objects previously
0N/A * written using an ObjectOutputStream.
0N/A *
0N/A * <p>ObjectOutputStream and ObjectInputStream can provide an application with
0N/A * persistent storage for graphs of objects when used with a FileOutputStream
0N/A * and FileInputStream respectively. ObjectInputStream is used to recover
0N/A * those objects previously serialized. Other uses include passing objects
0N/A * between hosts using a socket stream or for marshaling and unmarshaling
0N/A * arguments and parameters in a remote communication system.
0N/A *
0N/A * <p>ObjectInputStream ensures that the types of all objects in the graph
0N/A * created from the stream match the classes present in the Java Virtual
0N/A * Machine. Classes are loaded as required using the standard mechanisms.
0N/A *
0N/A * <p>Only objects that support the java.io.Serializable or
0N/A * java.io.Externalizable interface can be read from streams.
0N/A *
0N/A * <p>The method <code>readObject</code> is used to read an object from the
0N/A * stream. Java's safe casting should be used to get the desired type. In
0N/A * Java, strings and arrays are objects and are treated as objects during
0N/A * serialization. When read they need to be cast to the expected type.
0N/A *
0N/A * <p>Primitive data types can be read from the stream using the appropriate
0N/A * method on DataInput.
0N/A *
0N/A * <p>The default deserialization mechanism for objects restores the contents
0N/A * of each field to the value and type it had when it was written. Fields
0N/A * declared as transient or static are ignored by the deserialization process.
0N/A * References to other objects cause those objects to be read from the stream
0N/A * as necessary. Graphs of objects are restored correctly using a reference
0N/A * sharing mechanism. New objects are always allocated when deserializing,
0N/A * which prevents existing objects from being overwritten.
0N/A *
0N/A * <p>Reading an object is analogous to running the constructors of a new
0N/A * object. Memory is allocated for the object and initialized to zero (NULL).
0N/A * No-arg constructors are invoked for the non-serializable classes and then
0N/A * the fields of the serializable classes are restored from the stream starting
0N/A * with the serializable class closest to java.lang.object and finishing with
0N/A * the object's most specific class.
0N/A *
0N/A * <p>For example to read from a stream as written by the example in
0N/A * ObjectOutputStream:
0N/A * <br>
0N/A * <pre>
0N/A * FileInputStream fis = new FileInputStream("t.tmp");
0N/A * ObjectInputStream ois = new ObjectInputStream(fis);
0N/A *
0N/A * int i = ois.readInt();
0N/A * String today = (String) ois.readObject();
0N/A * Date date = (Date) ois.readObject();
0N/A *
0N/A * ois.close();
0N/A * </pre>
0N/A *
0N/A * <p>Classes control how they are serialized by implementing either the
0N/A * java.io.Serializable or java.io.Externalizable interfaces.
0N/A *
0N/A * <p>Implementing the Serializable interface allows object serialization to
0N/A * save and restore the entire state of the object and it allows classes to
0N/A * evolve between the time the stream is written and the time it is read. It
0N/A * automatically traverses references between objects, saving and restoring
0N/A * entire graphs.
0N/A *
0N/A * <p>Serializable classes that require special handling during the
0N/A * serialization and deserialization process should implement the following
0N/A * methods:<p>
0N/A *
0N/A * <pre>
0N/A * private void writeObject(java.io.ObjectOutputStream stream)
0N/A * throws IOException;
0N/A * private void readObject(java.io.ObjectInputStream stream)
0N/A * throws IOException, ClassNotFoundException;
0N/A * private void readObjectNoData()
0N/A * throws ObjectStreamException;
0N/A * </pre>
0N/A *
0N/A * <p>The readObject method is responsible for reading and restoring the state
0N/A * of the object for its particular class using data written to the stream by
0N/A * the corresponding writeObject method. The method does not need to concern
0N/A * itself with the state belonging to its superclasses or subclasses. State is
0N/A * restored by reading data from the ObjectInputStream for the individual
0N/A * fields and making assignments to the appropriate fields of the object.
0N/A * Reading primitive data types is supported by DataInput.
0N/A *
0N/A * <p>Any attempt to read object data which exceeds the boundaries of the
0N/A * custom data written by the corresponding writeObject method will cause an
0N/A * OptionalDataException to be thrown with an eof field value of true.
0N/A * Non-object reads which exceed the end of the allotted data will reflect the
0N/A * end of data in the same way that they would indicate the end of the stream:
0N/A * bytewise reads will return -1 as the byte read or number of bytes read, and
0N/A * primitive reads will throw EOFExceptions. If there is no corresponding
0N/A * writeObject method, then the end of default serialized data marks the end of
0N/A * the allotted data.
0N/A *
0N/A * <p>Primitive and object read calls issued from within a readExternal method
0N/A * behave in the same manner--if the stream is already positioned at the end of
0N/A * data written by the corresponding writeExternal method, object reads will
0N/A * throw OptionalDataExceptions with eof set to true, bytewise reads will
0N/A * return -1, and primitive reads will throw EOFExceptions. Note that this
0N/A * behavior does not hold for streams written with the old
0N/A * <code>ObjectStreamConstants.PROTOCOL_VERSION_1</code> protocol, in which the
0N/A * end of data written by writeExternal methods is not demarcated, and hence
0N/A * cannot be detected.
0N/A *
0N/A * <p>The readObjectNoData method is responsible for initializing the state of
0N/A * the object for its particular class in the event that the serialization
0N/A * stream does not list the given class as a superclass of the object being
0N/A * deserialized. This may occur in cases where the receiving party uses a
0N/A * different version of the deserialized instance's class than the sending
0N/A * party, and the receiver's version extends classes that are not extended by
0N/A * the sender's version. This may also occur if the serialization stream has
0N/A * been tampered; hence, readObjectNoData is useful for initializing
0N/A * deserialized objects properly despite a "hostile" or incomplete source
0N/A * stream.
0N/A *
0N/A * <p>Serialization does not read or assign values to the fields of any object
0N/A * that does not implement the java.io.Serializable interface. Subclasses of
0N/A * Objects that are not serializable can be serializable. In this case the
0N/A * non-serializable class must have a no-arg constructor to allow its fields to
0N/A * be initialized. In this case it is the responsibility of the subclass to
0N/A * save and restore the state of the non-serializable class. It is frequently
0N/A * the case that the fields of that class are accessible (public, package, or
0N/A * protected) or that there are get and set methods that can be used to restore
0N/A * the state.
0N/A *
0N/A * <p>Any exception that occurs while deserializing an object will be caught by
0N/A * the ObjectInputStream and abort the reading process.
0N/A *
0N/A * <p>Implementing the Externalizable interface allows the object to assume
0N/A * complete control over the contents and format of the object's serialized
0N/A * form. The methods of the Externalizable interface, writeExternal and
0N/A * readExternal, are called to save and restore the objects state. When
0N/A * implemented by a class they can write and read their own state using all of
0N/A * the methods of ObjectOutput and ObjectInput. It is the responsibility of
0N/A * the objects to handle any versioning that occurs.
0N/A *
0N/A * <p>Enum constants are deserialized differently than ordinary serializable or
0N/A * externalizable objects. The serialized form of an enum constant consists
0N/A * solely of its name; field values of the constant are not transmitted. To
0N/A * deserialize an enum constant, ObjectInputStream reads the constant name from
0N/A * the stream; the deserialized constant is then obtained by calling the static
0N/A * method <code>Enum.valueOf(Class, String)</code> with the enum constant's
0N/A * base type and the received constant name as arguments. Like other
0N/A * serializable or externalizable objects, enum constants can function as the
0N/A * targets of back references appearing subsequently in the serialization
0N/A * stream. The process by which enum constants are deserialized cannot be
0N/A * customized: any class-specific readObject, readObjectNoData, and readResolve
0N/A * methods defined by enum types are ignored during deserialization.
0N/A * Similarly, any serialPersistentFields or serialVersionUID field declarations
0N/A * are also ignored--all enum types have a fixed serialVersionUID of 0L.
0N/A *
0N/A * @author Mike Warres
0N/A * @author Roger Riggs
0N/A * @see java.io.DataInput
0N/A * @see java.io.ObjectOutputStream
0N/A * @see java.io.Serializable
0N/A * @see <a href="../../../platform/serialization/spec/input.html"> Object Serialization Specification, Section 3, Object Input Classes</a>
0N/A * @since JDK1.1
0N/A */
0N/Apublic class ObjectInputStream
0N/A extends InputStream implements ObjectInput, ObjectStreamConstants
0N/A{
0N/A /** handle value representing null */
0N/A private static final int NULL_HANDLE = -1;
0N/A
0N/A /** marker for unshared objects in internal handle table */
0N/A private static final Object unsharedMarker = new Object();
0N/A
0N/A /** table mapping primitive type names to corresponding class objects */
28N/A private static final HashMap<String, Class<?>> primClasses
3323N/A = new HashMap<>(8, 1.0F);
0N/A static {
0N/A primClasses.put("boolean", boolean.class);
0N/A primClasses.put("byte", byte.class);
0N/A primClasses.put("char", char.class);
0N/A primClasses.put("short", short.class);
0N/A primClasses.put("int", int.class);
0N/A primClasses.put("long", long.class);
0N/A primClasses.put("float", float.class);
0N/A primClasses.put("double", double.class);
0N/A primClasses.put("void", void.class);
0N/A }
0N/A
0N/A private static class Caches {
0N/A /** cache of subclass security audit results */
0N/A static final ConcurrentMap<WeakClassKey,Boolean> subclassAudits =
3323N/A new ConcurrentHashMap<>();
0N/A
0N/A /** queue for WeakReferences to audited subclasses */
0N/A static final ReferenceQueue<Class<?>> subclassAuditsQueue =
3323N/A new ReferenceQueue<>();
0N/A }
0N/A
0N/A /** filter stream for handling block data conversion */
0N/A private final BlockDataInputStream bin;
0N/A /** validation callback list */
0N/A private final ValidationList vlist;
0N/A /** recursion depth */
0N/A private int depth;
0N/A /** whether stream is closed */
0N/A private boolean closed;
0N/A
0N/A /** wire handle -> obj/exception map */
0N/A private final HandleTable handles;
0N/A /** scratch field for passing handle values up/down call stack */
0N/A private int passHandle = NULL_HANDLE;
0N/A /** flag set when at end of field value block with no TC_ENDBLOCKDATA */
0N/A private boolean defaultDataEnd = false;
0N/A
0N/A /** buffer for reading primitive field values */
0N/A private byte[] primVals;
0N/A
0N/A /** if true, invoke readObjectOverride() instead of readObject() */
0N/A private final boolean enableOverride;
0N/A /** if true, invoke resolveObject() */
0N/A private boolean enableResolve;
0N/A
0N/A /**
0N/A * Context during upcalls to class-defined readObject methods; holds
0N/A * object currently being deserialized and descriptor for current class.
0N/A * Null when not during readObject upcall.
0N/A */
2989N/A private SerialCallbackContext curContext;
0N/A
0N/A /**
0N/A * Creates an ObjectInputStream that reads from the specified InputStream.
0N/A * A serialization stream header is read from the stream and verified.
0N/A * This constructor will block until the corresponding ObjectOutputStream
0N/A * has written and flushed the header.
0N/A *
0N/A * <p>If a security manager is installed, this constructor will check for
0N/A * the "enableSubclassImplementation" SerializablePermission when invoked
0N/A * directly or indirectly by the constructor of a subclass which overrides
0N/A * the ObjectInputStream.readFields or ObjectInputStream.readUnshared
0N/A * methods.
0N/A *
0N/A * @param in input stream to read from
0N/A * @throws StreamCorruptedException if the stream header is incorrect
0N/A * @throws IOException if an I/O error occurs while reading stream header
0N/A * @throws SecurityException if untrusted subclass illegally overrides
0N/A * security-sensitive methods
0N/A * @throws NullPointerException if <code>in</code> is <code>null</code>
0N/A * @see ObjectInputStream#ObjectInputStream()
0N/A * @see ObjectInputStream#readFields()
0N/A * @see ObjectOutputStream#ObjectOutputStream(OutputStream)
0N/A */
0N/A public ObjectInputStream(InputStream in) throws IOException {
0N/A verifySubclass();
0N/A bin = new BlockDataInputStream(in);
0N/A handles = new HandleTable(10);
0N/A vlist = new ValidationList();
0N/A enableOverride = false;
0N/A readStreamHeader();
0N/A bin.setBlockDataMode(true);
0N/A }
0N/A
0N/A /**
0N/A * Provide a way for subclasses that are completely reimplementing
0N/A * ObjectInputStream to not have to allocate private data just used by this
0N/A * implementation of ObjectInputStream.
0N/A *
0N/A * <p>If there is a security manager installed, this method first calls the
0N/A * security manager's <code>checkPermission</code> method with the
0N/A * <code>SerializablePermission("enableSubclassImplementation")</code>
0N/A * permission to ensure it's ok to enable subclassing.
0N/A *
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkPermission</code> method denies enabling
0N/A * subclassing.
0N/A * @see SecurityManager#checkPermission
0N/A * @see java.io.SerializablePermission
0N/A */
0N/A protected ObjectInputStream() throws IOException, SecurityException {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
0N/A }
0N/A bin = null;
0N/A handles = null;
0N/A vlist = null;
0N/A enableOverride = true;
0N/A }
0N/A
0N/A /**
0N/A * Read an object from the ObjectInputStream. The class of the object, the
0N/A * signature of the class, and the values of the non-transient and
0N/A * non-static fields of the class and all of its supertypes are read.
0N/A * Default deserializing for a class can be overriden using the writeObject
0N/A * and readObject methods. Objects referenced by this object are read
0N/A * transitively so that a complete equivalent graph of objects is
0N/A * reconstructed by readObject.
0N/A *
0N/A * <p>The root object is completely restored when all of its fields and the
0N/A * objects it references are completely restored. At this point the object
0N/A * validation callbacks are executed in order based on their registered
0N/A * priorities. The callbacks are registered by objects (in the readObject
0N/A * special methods) as they are individually restored.
0N/A *
0N/A * <p>Exceptions are thrown for problems with the InputStream and for
0N/A * classes that should not be deserialized. All exceptions are fatal to
0N/A * the InputStream and leave it in an indeterminate state; it is up to the
0N/A * caller to ignore or recover the stream state.
0N/A *
0N/A * @throws ClassNotFoundException Class of a serialized object cannot be
0N/A * found.
0N/A * @throws InvalidClassException Something is wrong with a class used by
0N/A * serialization.
0N/A * @throws StreamCorruptedException Control information in the
0N/A * stream is inconsistent.
0N/A * @throws OptionalDataException Primitive data was found in the
0N/A * stream instead of objects.
0N/A * @throws IOException Any of the usual Input/Output related exceptions.
0N/A */
0N/A public final Object readObject()
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A if (enableOverride) {
0N/A return readObjectOverride();
0N/A }
0N/A
0N/A // if nested read, passHandle contains handle of enclosing object
0N/A int outerHandle = passHandle;
0N/A try {
0N/A Object obj = readObject0(false);
0N/A handles.markDependency(outerHandle, passHandle);
0N/A ClassNotFoundException ex = handles.lookupException(passHandle);
0N/A if (ex != null) {
0N/A throw ex;
0N/A }
0N/A if (depth == 0) {
0N/A vlist.doCallbacks();
0N/A }
0N/A return obj;
0N/A } finally {
0N/A passHandle = outerHandle;
0N/A if (closed && depth == 0) {
0N/A clear();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method is called by trusted subclasses of ObjectOutputStream that
0N/A * constructed ObjectOutputStream using the protected no-arg constructor.
0N/A * The subclass is expected to provide an override method with the modifier
0N/A * "final".
0N/A *
0N/A * @return the Object read from the stream.
0N/A * @throws ClassNotFoundException Class definition of a serialized object
0N/A * cannot be found.
0N/A * @throws OptionalDataException Primitive data was found in the stream
0N/A * instead of objects.
0N/A * @throws IOException if I/O errors occurred while reading from the
0N/A * underlying stream
0N/A * @see #ObjectInputStream()
0N/A * @see #readObject()
0N/A * @since 1.2
0N/A */
0N/A protected Object readObjectOverride()
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Reads an "unshared" object from the ObjectInputStream. This method is
0N/A * identical to readObject, except that it prevents subsequent calls to
0N/A * readObject and readUnshared from returning additional references to the
0N/A * deserialized instance obtained via this call. Specifically:
0N/A * <ul>
0N/A * <li>If readUnshared is called to deserialize a back-reference (the
0N/A * stream representation of an object which has been written
0N/A * previously to the stream), an ObjectStreamException will be
0N/A * thrown.
0N/A *
0N/A * <li>If readUnshared returns successfully, then any subsequent attempts
0N/A * to deserialize back-references to the stream handle deserialized
0N/A * by readUnshared will cause an ObjectStreamException to be thrown.
0N/A * </ul>
0N/A * Deserializing an object via readUnshared invalidates the stream handle
0N/A * associated with the returned object. Note that this in itself does not
0N/A * always guarantee that the reference returned by readUnshared is unique;
0N/A * the deserialized object may define a readResolve method which returns an
0N/A * object visible to other parties, or readUnshared may return a Class
0N/A * object or enum constant obtainable elsewhere in the stream or through
0N/A * external means. If the deserialized object defines a readResolve method
0N/A * and the invocation of that method returns an array, then readUnshared
0N/A * returns a shallow clone of that array; this guarantees that the returned
0N/A * array object is unique and cannot be obtained a second time from an
0N/A * invocation of readObject or readUnshared on the ObjectInputStream,
0N/A * even if the underlying data stream has been manipulated.
0N/A *
0N/A * <p>ObjectInputStream subclasses which override this method can only be
0N/A * constructed in security contexts possessing the
0N/A * "enableSubclassImplementation" SerializablePermission; any attempt to
0N/A * instantiate such a subclass without this permission will cause a
0N/A * SecurityException to be thrown.
0N/A *
0N/A * @return reference to deserialized object
0N/A * @throws ClassNotFoundException if class of an object to deserialize
0N/A * cannot be found
0N/A * @throws StreamCorruptedException if control information in the stream
0N/A * is inconsistent
0N/A * @throws ObjectStreamException if object to deserialize has already
0N/A * appeared in stream
0N/A * @throws OptionalDataException if primitive data is next in stream
0N/A * @throws IOException if an I/O error occurs during deserialization
0N/A * @since 1.4
0N/A */
0N/A public Object readUnshared() throws IOException, ClassNotFoundException {
0N/A // if nested read, passHandle contains handle of enclosing object
0N/A int outerHandle = passHandle;
0N/A try {
0N/A Object obj = readObject0(true);
0N/A handles.markDependency(outerHandle, passHandle);
0N/A ClassNotFoundException ex = handles.lookupException(passHandle);
0N/A if (ex != null) {
0N/A throw ex;
0N/A }
0N/A if (depth == 0) {
0N/A vlist.doCallbacks();
0N/A }
0N/A return obj;
0N/A } finally {
0N/A passHandle = outerHandle;
0N/A if (closed && depth == 0) {
0N/A clear();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Read the non-static and non-transient fields of the current class from
0N/A * this stream. This may only be called from the readObject method of the
0N/A * class being deserialized. It will throw the NotActiveException if it is
0N/A * called otherwise.
0N/A *
0N/A * @throws ClassNotFoundException if the class of a serialized object
0N/A * could not be found.
0N/A * @throws IOException if an I/O error occurs.
0N/A * @throws NotActiveException if the stream is not currently reading
0N/A * objects.
0N/A */
0N/A public void defaultReadObject()
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A if (curContext == null) {
0N/A throw new NotActiveException("not in call to readObject");
0N/A }
0N/A Object curObj = curContext.getObj();
0N/A ObjectStreamClass curDesc = curContext.getDesc();
0N/A bin.setBlockDataMode(false);
0N/A defaultReadFields(curObj, curDesc);
0N/A bin.setBlockDataMode(true);
0N/A if (!curDesc.hasWriteObjectData()) {
0N/A /*
0N/A * Fix for 4360508: since stream does not contain terminating
0N/A * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere
0N/A * knows to simulate end-of-custom-data behavior.
0N/A */
0N/A defaultDataEnd = true;
0N/A }
0N/A ClassNotFoundException ex = handles.lookupException(passHandle);
0N/A if (ex != null) {
0N/A throw ex;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads the persistent fields from the stream and makes them available by
0N/A * name.
0N/A *
0N/A * @return the <code>GetField</code> object representing the persistent
0N/A * fields of the object being deserialized
0N/A * @throws ClassNotFoundException if the class of a serialized object
0N/A * could not be found.
0N/A * @throws IOException if an I/O error occurs.
0N/A * @throws NotActiveException if the stream is not currently reading
0N/A * objects.
0N/A * @since 1.2
0N/A */
0N/A public ObjectInputStream.GetField readFields()
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A if (curContext == null) {
0N/A throw new NotActiveException("not in call to readObject");
0N/A }
0N/A Object curObj = curContext.getObj();
0N/A ObjectStreamClass curDesc = curContext.getDesc();
0N/A bin.setBlockDataMode(false);
0N/A GetFieldImpl getField = new GetFieldImpl(curDesc);
0N/A getField.readFields();
0N/A bin.setBlockDataMode(true);
0N/A if (!curDesc.hasWriteObjectData()) {
0N/A /*
0N/A * Fix for 4360508: since stream does not contain terminating
0N/A * TC_ENDBLOCKDATA tag, set flag so that reading code elsewhere
0N/A * knows to simulate end-of-custom-data behavior.
0N/A */
0N/A defaultDataEnd = true;
0N/A }
0N/A
0N/A return getField;
0N/A }
0N/A
0N/A /**
0N/A * Register an object to be validated before the graph is returned. While
0N/A * similar to resolveObject these validations are called after the entire
0N/A * graph has been reconstituted. Typically, a readObject method will
0N/A * register the object with the stream so that when all of the objects are
0N/A * restored a final set of validations can be performed.
0N/A *
0N/A * @param obj the object to receive the validation callback.
0N/A * @param prio controls the order of callbacks;zero is a good default.
0N/A * Use higher numbers to be called back earlier, lower numbers for
0N/A * later callbacks. Within a priority, callbacks are processed in
0N/A * no particular order.
0N/A * @throws NotActiveException The stream is not currently reading objects
0N/A * so it is invalid to register a callback.
0N/A * @throws InvalidObjectException The validation object is null.
0N/A */
0N/A public void registerValidation(ObjectInputValidation obj, int prio)
0N/A throws NotActiveException, InvalidObjectException
0N/A {
0N/A if (depth == 0) {
0N/A throw new NotActiveException("stream inactive");
0N/A }
0N/A vlist.register(obj, prio);
0N/A }
0N/A
0N/A /**
0N/A * Load the local class equivalent of the specified stream class
0N/A * description. Subclasses may implement this method to allow classes to
0N/A * be fetched from an alternate source.
0N/A *
0N/A * <p>The corresponding method in <code>ObjectOutputStream</code> is
0N/A * <code>annotateClass</code>. This method will be invoked only once for
0N/A * each unique class in the stream. This method can be implemented by
0N/A * subclasses to use an alternate loading mechanism but must return a
0N/A * <code>Class</code> object. Once returned, if the class is not an array
0N/A * class, its serialVersionUID is compared to the serialVersionUID of the
0N/A * serialized class, and if there is a mismatch, the deserialization fails
0N/A * and an {@link InvalidClassException} is thrown.
0N/A *
0N/A * <p>The default implementation of this method in
0N/A * <code>ObjectInputStream</code> returns the result of calling
0N/A * <pre>
0N/A * Class.forName(desc.getName(), false, loader)
0N/A * </pre>
0N/A * where <code>loader</code> is determined as follows: if there is a
0N/A * method on the current thread's stack whose declaring class was
0N/A * defined by a user-defined class loader (and was not a generated to
0N/A * implement reflective invocations), then <code>loader</code> is class
0N/A * loader corresponding to the closest such method to the currently
0N/A * executing frame; otherwise, <code>loader</code> is
0N/A * <code>null</code>. If this call results in a
0N/A * <code>ClassNotFoundException</code> and the name of the passed
0N/A * <code>ObjectStreamClass</code> instance is the Java language keyword
0N/A * for a primitive type or void, then the <code>Class</code> object
0N/A * representing that primitive type or void will be returned
0N/A * (e.g., an <code>ObjectStreamClass</code> with the name
0N/A * <code>"int"</code> will be resolved to <code>Integer.TYPE</code>).
0N/A * Otherwise, the <code>ClassNotFoundException</code> will be thrown to
0N/A * the caller of this method.
0N/A *
0N/A * @param desc an instance of class <code>ObjectStreamClass</code>
0N/A * @return a <code>Class</code> object corresponding to <code>desc</code>
0N/A * @throws IOException any of the usual Input/Output exceptions.
0N/A * @throws ClassNotFoundException if class of a serialized object cannot
0N/A * be found.
0N/A */
0N/A protected Class<?> resolveClass(ObjectStreamClass desc)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A String name = desc.getName();
0N/A try {
0N/A return Class.forName(name, false, latestUserDefinedLoader());
0N/A } catch (ClassNotFoundException ex) {
28N/A Class<?> cl = primClasses.get(name);
0N/A if (cl != null) {
0N/A return cl;
0N/A } else {
0N/A throw ex;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a proxy class that implements the interfaces named in a proxy
0N/A * class descriptor; subclasses may implement this method to read custom
0N/A * data from the stream along with the descriptors for dynamic proxy
0N/A * classes, allowing them to use an alternate loading mechanism for the
0N/A * interfaces and the proxy class.
0N/A *
0N/A * <p>This method is called exactly once for each unique proxy class
0N/A * descriptor in the stream.
0N/A *
0N/A * <p>The corresponding method in <code>ObjectOutputStream</code> is
0N/A * <code>annotateProxyClass</code>. For a given subclass of
0N/A * <code>ObjectInputStream</code> that overrides this method, the
0N/A * <code>annotateProxyClass</code> method in the corresponding subclass of
0N/A * <code>ObjectOutputStream</code> must write any data or objects read by
0N/A * this method.
0N/A *
0N/A * <p>The default implementation of this method in
0N/A * <code>ObjectInputStream</code> returns the result of calling
0N/A * <code>Proxy.getProxyClass</code> with the list of <code>Class</code>
0N/A * objects for the interfaces that are named in the <code>interfaces</code>
0N/A * parameter. The <code>Class</code> object for each interface name
0N/A * <code>i</code> is the value returned by calling
0N/A * <pre>
0N/A * Class.forName(i, false, loader)
0N/A * </pre>
0N/A * where <code>loader</code> is that of the first non-<code>null</code>
0N/A * class loader up the execution stack, or <code>null</code> if no
0N/A * non-<code>null</code> class loaders are on the stack (the same class
0N/A * loader choice used by the <code>resolveClass</code> method). Unless any
0N/A * of the resolved interfaces are non-public, this same value of
0N/A * <code>loader</code> is also the class loader passed to
0N/A * <code>Proxy.getProxyClass</code>; if non-public interfaces are present,
0N/A * their class loader is passed instead (if more than one non-public
0N/A * interface class loader is encountered, an
0N/A * <code>IllegalAccessError</code> is thrown).
0N/A * If <code>Proxy.getProxyClass</code> throws an
0N/A * <code>IllegalArgumentException</code>, <code>resolveProxyClass</code>
0N/A * will throw a <code>ClassNotFoundException</code> containing the
0N/A * <code>IllegalArgumentException</code>.
0N/A *
0N/A * @param interfaces the list of interface names that were
0N/A * deserialized in the proxy class descriptor
0N/A * @return a proxy class for the specified interfaces
0N/A * @throws IOException any exception thrown by the underlying
0N/A * <code>InputStream</code>
0N/A * @throws ClassNotFoundException if the proxy class or any of the
0N/A * named interfaces could not be found
0N/A * @see ObjectOutputStream#annotateProxyClass(Class)
0N/A * @since 1.3
0N/A */
0N/A protected Class<?> resolveProxyClass(String[] interfaces)
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A ClassLoader latestLoader = latestUserDefinedLoader();
0N/A ClassLoader nonPublicLoader = null;
0N/A boolean hasNonPublicInterface = false;
0N/A
0N/A // define proxy in class loader of non-public interface(s), if any
0N/A Class[] classObjs = new Class[interfaces.length];
0N/A for (int i = 0; i < interfaces.length; i++) {
0N/A Class cl = Class.forName(interfaces[i], false, latestLoader);
0N/A if ((cl.getModifiers() & Modifier.PUBLIC) == 0) {
0N/A if (hasNonPublicInterface) {
0N/A if (nonPublicLoader != cl.getClassLoader()) {
0N/A throw new IllegalAccessError(
0N/A "conflicting non-public interface class loaders");
0N/A }
0N/A } else {
0N/A nonPublicLoader = cl.getClassLoader();
0N/A hasNonPublicInterface = true;
0N/A }
0N/A }
0N/A classObjs[i] = cl;
0N/A }
0N/A try {
0N/A return Proxy.getProxyClass(
0N/A hasNonPublicInterface ? nonPublicLoader : latestLoader,
0N/A classObjs);
0N/A } catch (IllegalArgumentException e) {
0N/A throw new ClassNotFoundException(null, e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method will allow trusted subclasses of ObjectInputStream to
0N/A * substitute one object for another during deserialization. Replacing
0N/A * objects is disabled until enableResolveObject is called. The
0N/A * enableResolveObject method checks that the stream requesting to resolve
0N/A * object can be trusted. Every reference to serializable objects is passed
0N/A * to resolveObject. To insure that the private state of objects is not
0N/A * unintentionally exposed only trusted streams may use resolveObject.
0N/A *
0N/A * <p>This method is called after an object has been read but before it is
0N/A * returned from readObject. The default resolveObject method just returns
0N/A * the same object.
0N/A *
0N/A * <p>When a subclass is replacing objects it must insure that the
0N/A * substituted object is compatible with every field where the reference
0N/A * will be stored. Objects whose type is not a subclass of the type of the
0N/A * field or array element abort the serialization by raising an exception
0N/A * and the object is not be stored.
0N/A *
0N/A * <p>This method is called only once when each object is first
0N/A * encountered. All subsequent references to the object will be redirected
0N/A * to the new object.
0N/A *
0N/A * @param obj object to be substituted
0N/A * @return the substituted object
0N/A * @throws IOException Any of the usual Input/Output exceptions.
0N/A */
0N/A protected Object resolveObject(Object obj) throws IOException {
0N/A return obj;
0N/A }
0N/A
0N/A /**
0N/A * Enable the stream to allow objects read from the stream to be replaced.
0N/A * When enabled, the resolveObject method is called for every object being
0N/A * deserialized.
0N/A *
0N/A * <p>If <i>enable</i> is true, and there is a security manager installed,
0N/A * this method first calls the security manager's
0N/A * <code>checkPermission</code> method with the
0N/A * <code>SerializablePermission("enableSubstitution")</code> permission to
0N/A * ensure it's ok to enable the stream to allow objects read from the
0N/A * stream to be replaced.
0N/A *
0N/A * @param enable true for enabling use of <code>resolveObject</code> for
0N/A * every object being deserialized
0N/A * @return the previous setting before this method was invoked
0N/A * @throws SecurityException if a security manager exists and its
0N/A * <code>checkPermission</code> method denies enabling the stream
0N/A * to allow objects read from the stream to be replaced.
0N/A * @see SecurityManager#checkPermission
0N/A * @see java.io.SerializablePermission
0N/A */
0N/A protected boolean enableResolveObject(boolean enable)
0N/A throws SecurityException
0N/A {
0N/A if (enable == enableResolve) {
0N/A return enable;
0N/A }
0N/A if (enable) {
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm != null) {
0N/A sm.checkPermission(SUBSTITUTION_PERMISSION);
0N/A }
0N/A }
0N/A enableResolve = enable;
0N/A return !enableResolve;
0N/A }
0N/A
0N/A /**
0N/A * The readStreamHeader method is provided to allow subclasses to read and
0N/A * verify their own stream headers. It reads and verifies the magic number
0N/A * and version number.
0N/A *
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws StreamCorruptedException if control information in the stream
0N/A * is inconsistent
0N/A */
0N/A protected void readStreamHeader()
0N/A throws IOException, StreamCorruptedException
0N/A {
0N/A short s0 = bin.readShort();
0N/A short s1 = bin.readShort();
0N/A if (s0 != STREAM_MAGIC || s1 != STREAM_VERSION) {
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid stream header: %04X%04X", s0, s1));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Read a class descriptor from the serialization stream. This method is
0N/A * called when the ObjectInputStream expects a class descriptor as the next
0N/A * item in the serialization stream. Subclasses of ObjectInputStream may
0N/A * override this method to read in class descriptors that have been written
0N/A * in non-standard formats (by subclasses of ObjectOutputStream which have
0N/A * overridden the <code>writeClassDescriptor</code> method). By default,
0N/A * this method reads class descriptors according to the format defined in
0N/A * the Object Serialization specification.
0N/A *
0N/A * @return the class descriptor read
0N/A * @throws IOException If an I/O error has occurred.
0N/A * @throws ClassNotFoundException If the Class of a serialized object used
0N/A * in the class descriptor representation cannot be found
0N/A * @see java.io.ObjectOutputStream#writeClassDescriptor(java.io.ObjectStreamClass)
0N/A * @since 1.3
0N/A */
0N/A protected ObjectStreamClass readClassDescriptor()
0N/A throws IOException, ClassNotFoundException
0N/A {
0N/A ObjectStreamClass desc = new ObjectStreamClass();
0N/A desc.readNonProxy(this);
0N/A return desc;
0N/A }
0N/A
0N/A /**
0N/A * Reads a byte of data. This method will block if no input is available.
0N/A *
0N/A * @return the byte read, or -1 if the end of the stream is reached.
0N/A * @throws IOException If an I/O error has occurred.
0N/A */
0N/A public int read() throws IOException {
0N/A return bin.read();
0N/A }
0N/A
0N/A /**
0N/A * Reads into an array of bytes. This method will block until some input
0N/A * is available. Consider using java.io.DataInputStream.readFully to read
0N/A * exactly 'length' bytes.
0N/A *
0N/A * @param buf the buffer into which the data is read
0N/A * @param off the start offset of the data
0N/A * @param len the maximum number of bytes read
0N/A * @return the actual number of bytes read, -1 is returned when the end of
0N/A * the stream is reached.
0N/A * @throws IOException If an I/O error has occurred.
0N/A * @see java.io.DataInputStream#readFully(byte[],int,int)
0N/A */
0N/A public int read(byte[] buf, int off, int len) throws IOException {
0N/A if (buf == null) {
0N/A throw new NullPointerException();
0N/A }
0N/A int endoff = off + len;
0N/A if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A return bin.read(buf, off, len, false);
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of bytes that can be read without blocking.
0N/A *
0N/A * @return the number of available bytes.
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A */
0N/A public int available() throws IOException {
0N/A return bin.available();
0N/A }
0N/A
0N/A /**
0N/A * Closes the input stream. Must be called to release any resources
0N/A * associated with the stream.
0N/A *
0N/A * @throws IOException If an I/O error has occurred.
0N/A */
0N/A public void close() throws IOException {
0N/A /*
0N/A * Even if stream already closed, propagate redundant close to
0N/A * underlying stream to stay consistent with previous implementations.
0N/A */
0N/A closed = true;
0N/A if (depth == 0) {
0N/A clear();
0N/A }
0N/A bin.close();
0N/A }
0N/A
0N/A /**
0N/A * Reads in a boolean.
0N/A *
0N/A * @return the boolean read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public boolean readBoolean() throws IOException {
0N/A return bin.readBoolean();
0N/A }
0N/A
0N/A /**
0N/A * Reads an 8 bit byte.
0N/A *
0N/A * @return the 8 bit byte read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public byte readByte() throws IOException {
0N/A return bin.readByte();
0N/A }
0N/A
0N/A /**
0N/A * Reads an unsigned 8 bit byte.
0N/A *
0N/A * @return the 8 bit byte read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public int readUnsignedByte() throws IOException {
0N/A return bin.readUnsignedByte();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 16 bit char.
0N/A *
0N/A * @return the 16 bit char read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public char readChar() throws IOException {
0N/A return bin.readChar();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 16 bit short.
0N/A *
0N/A * @return the 16 bit short read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public short readShort() throws IOException {
0N/A return bin.readShort();
0N/A }
0N/A
0N/A /**
0N/A * Reads an unsigned 16 bit short.
0N/A *
0N/A * @return the 16 bit short read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public int readUnsignedShort() throws IOException {
0N/A return bin.readUnsignedShort();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 32 bit int.
0N/A *
0N/A * @return the 32 bit integer read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public int readInt() throws IOException {
0N/A return bin.readInt();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 64 bit long.
0N/A *
0N/A * @return the read 64 bit long.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public long readLong() throws IOException {
0N/A return bin.readLong();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 32 bit float.
0N/A *
0N/A * @return the 32 bit float read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public float readFloat() throws IOException {
0N/A return bin.readFloat();
0N/A }
0N/A
0N/A /**
0N/A * Reads a 64 bit double.
0N/A *
0N/A * @return the 64 bit double read.
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public double readDouble() throws IOException {
0N/A return bin.readDouble();
0N/A }
0N/A
0N/A /**
0N/A * Reads bytes, blocking until all bytes are read.
0N/A *
0N/A * @param buf the buffer into which the data is read
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public void readFully(byte[] buf) throws IOException {
0N/A bin.readFully(buf, 0, buf.length, false);
0N/A }
0N/A
0N/A /**
0N/A * Reads bytes, blocking until all bytes are read.
0N/A *
0N/A * @param buf the buffer into which the data is read
0N/A * @param off the start offset of the data
0N/A * @param len the maximum number of bytes to read
0N/A * @throws EOFException If end of file is reached.
0N/A * @throws IOException If other I/O error has occurred.
0N/A */
0N/A public void readFully(byte[] buf, int off, int len) throws IOException {
0N/A int endoff = off + len;
0N/A if (off < 0 || len < 0 || endoff > buf.length || endoff < 0) {
0N/A throw new IndexOutOfBoundsException();
0N/A }
0N/A bin.readFully(buf, off, len, false);
0N/A }
0N/A
0N/A /**
0N/A * Skips bytes.
0N/A *
0N/A * @param len the number of bytes to be skipped
0N/A * @return the actual number of bytes skipped.
0N/A * @throws IOException If an I/O error has occurred.
0N/A */
0N/A public int skipBytes(int len) throws IOException {
0N/A return bin.skipBytes(len);
0N/A }
0N/A
0N/A /**
0N/A * Reads in a line that has been terminated by a \n, \r, \r\n or EOF.
0N/A *
0N/A * @return a String copy of the line.
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @deprecated This method does not properly convert bytes to characters.
0N/A * see DataInputStream for the details and alternatives.
0N/A */
0N/A @Deprecated
0N/A public String readLine() throws IOException {
0N/A return bin.readLine();
0N/A }
0N/A
0N/A /**
0N/A * Reads a String in
0N/A * <a href="DataInput.html#modified-utf-8">modified UTF-8</a>
0N/A * format.
0N/A *
0N/A * @return the String.
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws UTFDataFormatException if read bytes do not represent a valid
0N/A * modified UTF-8 encoding of a string
0N/A */
0N/A public String readUTF() throws IOException {
0N/A return bin.readUTF();
0N/A }
0N/A
0N/A /**
0N/A * Provide access to the persistent fields read from the input stream.
0N/A */
0N/A public static abstract class GetField {
0N/A
0N/A /**
0N/A * Get the ObjectStreamClass that describes the fields in the stream.
0N/A *
0N/A * @return the descriptor class that describes the serializable fields
0N/A */
0N/A public abstract ObjectStreamClass getObjectStreamClass();
0N/A
0N/A /**
0N/A * Return true if the named field is defaulted and has no value in this
0N/A * stream.
0N/A *
0N/A * @param name the name of the field
0N/A * @return true, if and only if the named field is defaulted
0N/A * @throws IOException if there are I/O errors while reading from
0N/A * the underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if <code>name</code> does not
0N/A * correspond to a serializable field
0N/A */
0N/A public abstract boolean defaulted(String name) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named boolean field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>boolean</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract boolean get(String name, boolean val)
0N/A throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named byte field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>byte</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract byte get(String name, byte val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named char field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>char</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract char get(String name, char val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named short field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>short</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract short get(String name, short val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named int field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>int</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract int get(String name, int val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named long field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>long</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract long get(String name, long val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named float field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>float</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract float get(String name, float val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named double field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>double</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract double get(String name, double val) throws IOException;
0N/A
0N/A /**
0N/A * Get the value of the named Object field from the persistent field.
0N/A *
0N/A * @param name the name of the field
0N/A * @param val the default value to use if <code>name</code> does not
0N/A * have a value
0N/A * @return the value of the named <code>Object</code> field
0N/A * @throws IOException if there are I/O errors while reading from the
0N/A * underlying <code>InputStream</code>
0N/A * @throws IllegalArgumentException if type of <code>name</code> is
0N/A * not serializable or if the field type is incorrect
0N/A */
0N/A public abstract Object get(String name, Object val) throws IOException;
0N/A }
0N/A
0N/A /**
0N/A * Verifies that this (possibly subclass) instance can be constructed
0N/A * without violating security constraints: the subclass must not override
0N/A * security-sensitive non-final methods, or else the
0N/A * "enableSubclassImplementation" SerializablePermission is checked.
0N/A */
0N/A private void verifySubclass() {
0N/A Class cl = getClass();
0N/A if (cl == ObjectInputStream.class) {
0N/A return;
0N/A }
0N/A SecurityManager sm = System.getSecurityManager();
0N/A if (sm == null) {
0N/A return;
0N/A }
0N/A processQueue(Caches.subclassAuditsQueue, Caches.subclassAudits);
0N/A WeakClassKey key = new WeakClassKey(cl, Caches.subclassAuditsQueue);
0N/A Boolean result = Caches.subclassAudits.get(key);
0N/A if (result == null) {
0N/A result = Boolean.valueOf(auditSubclass(cl));
0N/A Caches.subclassAudits.putIfAbsent(key, result);
0N/A }
0N/A if (result.booleanValue()) {
0N/A return;
0N/A }
0N/A sm.checkPermission(SUBCLASS_IMPLEMENTATION_PERMISSION);
0N/A }
0N/A
0N/A /**
0N/A * Performs reflective checks on given subclass to verify that it doesn't
0N/A * override security-sensitive non-final methods. Returns true if subclass
0N/A * is "safe", false otherwise.
0N/A */
28N/A private static boolean auditSubclass(final Class<?> subcl) {
0N/A Boolean result = AccessController.doPrivileged(
0N/A new PrivilegedAction<Boolean>() {
0N/A public Boolean run() {
28N/A for (Class<?> cl = subcl;
0N/A cl != ObjectInputStream.class;
0N/A cl = cl.getSuperclass())
0N/A {
0N/A try {
0N/A cl.getDeclaredMethod(
0N/A "readUnshared", (Class[]) null);
0N/A return Boolean.FALSE;
0N/A } catch (NoSuchMethodException ex) {
0N/A }
0N/A try {
0N/A cl.getDeclaredMethod("readFields", (Class[]) null);
0N/A return Boolean.FALSE;
0N/A } catch (NoSuchMethodException ex) {
0N/A }
0N/A }
0N/A return Boolean.TRUE;
0N/A }
0N/A }
0N/A );
0N/A return result.booleanValue();
0N/A }
0N/A
0N/A /**
0N/A * Clears internal data structures.
0N/A */
0N/A private void clear() {
0N/A handles.clear();
0N/A vlist.clear();
0N/A }
0N/A
0N/A /**
0N/A * Underlying readObject implementation.
0N/A */
0N/A private Object readObject0(boolean unshared) throws IOException {
0N/A boolean oldMode = bin.getBlockDataMode();
0N/A if (oldMode) {
0N/A int remain = bin.currentBlockRemaining();
0N/A if (remain > 0) {
0N/A throw new OptionalDataException(remain);
0N/A } else if (defaultDataEnd) {
0N/A /*
0N/A * Fix for 4360508: stream is currently at the end of a field
0N/A * value block written via default serialization; since there
0N/A * is no terminating TC_ENDBLOCKDATA tag, simulate
0N/A * end-of-custom-data behavior explicitly.
0N/A */
0N/A throw new OptionalDataException(true);
0N/A }
0N/A bin.setBlockDataMode(false);
0N/A }
0N/A
0N/A byte tc;
0N/A while ((tc = bin.peekByte()) == TC_RESET) {
0N/A bin.readByte();
0N/A handleReset();
0N/A }
0N/A
0N/A depth++;
0N/A try {
0N/A switch (tc) {
0N/A case TC_NULL:
0N/A return readNull();
0N/A
0N/A case TC_REFERENCE:
0N/A return readHandle(unshared);
0N/A
0N/A case TC_CLASS:
0N/A return readClass(unshared);
0N/A
0N/A case TC_CLASSDESC:
0N/A case TC_PROXYCLASSDESC:
0N/A return readClassDesc(unshared);
0N/A
0N/A case TC_STRING:
0N/A case TC_LONGSTRING:
0N/A return checkResolve(readString(unshared));
0N/A
0N/A case TC_ARRAY:
0N/A return checkResolve(readArray(unshared));
0N/A
0N/A case TC_ENUM:
0N/A return checkResolve(readEnum(unshared));
0N/A
0N/A case TC_OBJECT:
0N/A return checkResolve(readOrdinaryObject(unshared));
0N/A
0N/A case TC_EXCEPTION:
0N/A IOException ex = readFatalException();
0N/A throw new WriteAbortedException("writing aborted", ex);
0N/A
0N/A case TC_BLOCKDATA:
0N/A case TC_BLOCKDATALONG:
0N/A if (oldMode) {
0N/A bin.setBlockDataMode(true);
0N/A bin.peek(); // force header read
0N/A throw new OptionalDataException(
0N/A bin.currentBlockRemaining());
0N/A } else {
0N/A throw new StreamCorruptedException(
0N/A "unexpected block data");
0N/A }
0N/A
0N/A case TC_ENDBLOCKDATA:
0N/A if (oldMode) {
0N/A throw new OptionalDataException(true);
0N/A } else {
0N/A throw new StreamCorruptedException(
0N/A "unexpected end of block data");
0N/A }
0N/A
0N/A default:
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid type code: %02X", tc));
0N/A }
0N/A } finally {
0N/A depth--;
0N/A bin.setBlockDataMode(oldMode);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * If resolveObject has been enabled and given object does not have an
0N/A * exception associated with it, calls resolveObject to determine
0N/A * replacement for object, and updates handle table accordingly. Returns
0N/A * replacement object, or echoes provided object if no replacement
0N/A * occurred. Expects that passHandle is set to given object's handle prior
0N/A * to calling this method.
0N/A */
0N/A private Object checkResolve(Object obj) throws IOException {
0N/A if (!enableResolve || handles.lookupException(passHandle) != null) {
0N/A return obj;
0N/A }
0N/A Object rep = resolveObject(obj);
0N/A if (rep != obj) {
0N/A handles.setObject(passHandle, rep);
0N/A }
0N/A return rep;
0N/A }
0N/A
0N/A /**
0N/A * Reads string without allowing it to be replaced in stream. Called from
0N/A * within ObjectStreamClass.read().
0N/A */
0N/A String readTypeString() throws IOException {
0N/A int oldHandle = passHandle;
0N/A try {
0N/A byte tc = bin.peekByte();
0N/A switch (tc) {
0N/A case TC_NULL:
0N/A return (String) readNull();
0N/A
0N/A case TC_REFERENCE:
0N/A return (String) readHandle(false);
0N/A
0N/A case TC_STRING:
0N/A case TC_LONGSTRING:
0N/A return readString(false);
0N/A
0N/A default:
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid type code: %02X", tc));
0N/A }
0N/A } finally {
0N/A passHandle = oldHandle;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads in null code, sets passHandle to NULL_HANDLE and returns null.
0N/A */
0N/A private Object readNull() throws IOException {
0N/A if (bin.readByte() != TC_NULL) {
0N/A throw new InternalError();
0N/A }
0N/A passHandle = NULL_HANDLE;
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Reads in object handle, sets passHandle to the read handle, and returns
0N/A * object associated with the handle.
0N/A */
0N/A private Object readHandle(boolean unshared) throws IOException {
0N/A if (bin.readByte() != TC_REFERENCE) {
0N/A throw new InternalError();
0N/A }
0N/A passHandle = bin.readInt() - baseWireHandle;
0N/A if (passHandle < 0 || passHandle >= handles.size()) {
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid handle value: %08X", passHandle +
0N/A baseWireHandle));
0N/A }
0N/A if (unshared) {
0N/A // REMIND: what type of exception to throw here?
0N/A throw new InvalidObjectException(
0N/A "cannot read back reference as unshared");
0N/A }
0N/A
0N/A Object obj = handles.lookupObject(passHandle);
0N/A if (obj == unsharedMarker) {
0N/A // REMIND: what type of exception to throw here?
0N/A throw new InvalidObjectException(
0N/A "cannot read back reference to unshared object");
0N/A }
0N/A return obj;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns class object. Sets passHandle to class object's
0N/A * assigned handle. Returns null if class is unresolvable (in which case a
0N/A * ClassNotFoundException will be associated with the class' handle in the
0N/A * handle table).
0N/A */
0N/A private Class readClass(boolean unshared) throws IOException {
0N/A if (bin.readByte() != TC_CLASS) {
0N/A throw new InternalError();
0N/A }
0N/A ObjectStreamClass desc = readClassDesc(false);
0N/A Class cl = desc.forClass();
0N/A passHandle = handles.assign(unshared ? unsharedMarker : cl);
0N/A
0N/A ClassNotFoundException resolveEx = desc.getResolveException();
0N/A if (resolveEx != null) {
0N/A handles.markException(passHandle, resolveEx);
0N/A }
0N/A
0N/A handles.finish(passHandle);
0N/A return cl;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns (possibly null) class descriptor. Sets passHandle
0N/A * to class descriptor's assigned handle. If class descriptor cannot be
0N/A * resolved to a class in the local VM, a ClassNotFoundException is
0N/A * associated with the class descriptor's handle.
0N/A */
0N/A private ObjectStreamClass readClassDesc(boolean unshared)
0N/A throws IOException
0N/A {
0N/A byte tc = bin.peekByte();
0N/A switch (tc) {
0N/A case TC_NULL:
0N/A return (ObjectStreamClass) readNull();
0N/A
0N/A case TC_REFERENCE:
0N/A return (ObjectStreamClass) readHandle(unshared);
0N/A
0N/A case TC_PROXYCLASSDESC:
0N/A return readProxyDesc(unshared);
0N/A
0N/A case TC_CLASSDESC:
0N/A return readNonProxyDesc(unshared);
0N/A
0N/A default:
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid type code: %02X", tc));
0N/A }
0N/A }
0N/A
6017N/A private boolean isCustomSubclass() {
6017N/A // Return true if this class is a custom subclass of ObjectInputStream
6017N/A return getClass().getClassLoader()
6017N/A != ObjectInputStream.class.getClassLoader();
6017N/A }
6017N/A
0N/A /**
0N/A * Reads in and returns class descriptor for a dynamic proxy class. Sets
0N/A * passHandle to proxy class descriptor's assigned handle. If proxy class
0N/A * descriptor cannot be resolved to a class in the local VM, a
0N/A * ClassNotFoundException is associated with the descriptor's handle.
0N/A */
0N/A private ObjectStreamClass readProxyDesc(boolean unshared)
0N/A throws IOException
0N/A {
0N/A if (bin.readByte() != TC_PROXYCLASSDESC) {
0N/A throw new InternalError();
0N/A }
0N/A
0N/A ObjectStreamClass desc = new ObjectStreamClass();
0N/A int descHandle = handles.assign(unshared ? unsharedMarker : desc);
0N/A passHandle = NULL_HANDLE;
0N/A
0N/A int numIfaces = bin.readInt();
0N/A String[] ifaces = new String[numIfaces];
0N/A for (int i = 0; i < numIfaces; i++) {
0N/A ifaces[i] = bin.readUTF();
0N/A }
0N/A
0N/A Class cl = null;
0N/A ClassNotFoundException resolveEx = null;
0N/A bin.setBlockDataMode(true);
0N/A try {
0N/A if ((cl = resolveProxyClass(ifaces)) == null) {
0N/A resolveEx = new ClassNotFoundException("null class");
6017N/A } else if (!Proxy.isProxyClass(cl)) {
6017N/A throw new InvalidClassException("Not a proxy");
6017N/A } else {
6017N/A // ReflectUtil.checkProxyPackageAccess makes a test
6017N/A // equivalent to isCustomSubclass so there's no need
6017N/A // to condition this call to isCustomSubclass == true here.
6017N/A ReflectUtil.checkProxyPackageAccess(
6017N/A getClass().getClassLoader(),
6017N/A cl.getInterfaces());
0N/A }
0N/A } catch (ClassNotFoundException ex) {
0N/A resolveEx = ex;
0N/A }
0N/A skipCustomData();
0N/A
0N/A desc.initProxy(cl, resolveEx, readClassDesc(false));
0N/A
0N/A handles.finish(descHandle);
0N/A passHandle = descHandle;
0N/A return desc;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns class descriptor for a class that is not a dynamic
0N/A * proxy class. Sets passHandle to class descriptor's assigned handle. If
0N/A * class descriptor cannot be resolved to a class in the local VM, a
0N/A * ClassNotFoundException is associated with the descriptor's handle.
0N/A */
0N/A private ObjectStreamClass readNonProxyDesc(boolean unshared)
0N/A throws IOException
0N/A {
0N/A if (bin.readByte() != TC_CLASSDESC) {
0N/A throw new InternalError();
0N/A }
0N/A
0N/A ObjectStreamClass desc = new ObjectStreamClass();
0N/A int descHandle = handles.assign(unshared ? unsharedMarker : desc);
0N/A passHandle = NULL_HANDLE;
0N/A
0N/A ObjectStreamClass readDesc = null;
0N/A try {
0N/A readDesc = readClassDescriptor();
0N/A } catch (ClassNotFoundException ex) {
0N/A throw (IOException) new InvalidClassException(
0N/A "failed to read class descriptor").initCause(ex);
0N/A }
0N/A
0N/A Class cl = null;
0N/A ClassNotFoundException resolveEx = null;
0N/A bin.setBlockDataMode(true);
6017N/A final boolean checksRequired = isCustomSubclass();
0N/A try {
0N/A if ((cl = resolveClass(readDesc)) == null) {
0N/A resolveEx = new ClassNotFoundException("null class");
6017N/A } else if (checksRequired) {
6017N/A ReflectUtil.checkPackageAccess(cl);
0N/A }
0N/A } catch (ClassNotFoundException ex) {
0N/A resolveEx = ex;
0N/A }
0N/A skipCustomData();
0N/A
0N/A desc.initNonProxy(readDesc, cl, resolveEx, readClassDesc(false));
0N/A
0N/A handles.finish(descHandle);
0N/A passHandle = descHandle;
0N/A return desc;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns new string. Sets passHandle to new string's
0N/A * assigned handle.
0N/A */
0N/A private String readString(boolean unshared) throws IOException {
0N/A String str;
0N/A byte tc = bin.readByte();
0N/A switch (tc) {
0N/A case TC_STRING:
0N/A str = bin.readUTF();
0N/A break;
0N/A
0N/A case TC_LONGSTRING:
0N/A str = bin.readLongUTF();
0N/A break;
0N/A
0N/A default:
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid type code: %02X", tc));
0N/A }
0N/A passHandle = handles.assign(unshared ? unsharedMarker : str);
0N/A handles.finish(passHandle);
0N/A return str;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns array object, or null if array class is
0N/A * unresolvable. Sets passHandle to array's assigned handle.
0N/A */
0N/A private Object readArray(boolean unshared) throws IOException {
0N/A if (bin.readByte() != TC_ARRAY) {
0N/A throw new InternalError();
0N/A }
0N/A
0N/A ObjectStreamClass desc = readClassDesc(false);
0N/A int len = bin.readInt();
0N/A
0N/A Object array = null;
0N/A Class cl, ccl = null;
0N/A if ((cl = desc.forClass()) != null) {
0N/A ccl = cl.getComponentType();
0N/A array = Array.newInstance(ccl, len);
0N/A }
0N/A
0N/A int arrayHandle = handles.assign(unshared ? unsharedMarker : array);
0N/A ClassNotFoundException resolveEx = desc.getResolveException();
0N/A if (resolveEx != null) {
0N/A handles.markException(arrayHandle, resolveEx);
0N/A }
0N/A
0N/A if (ccl == null) {
0N/A for (int i = 0; i < len; i++) {
0N/A readObject0(false);
0N/A }
0N/A } else if (ccl.isPrimitive()) {
0N/A if (ccl == Integer.TYPE) {
0N/A bin.readInts((int[]) array, 0, len);
0N/A } else if (ccl == Byte.TYPE) {
0N/A bin.readFully((byte[]) array, 0, len, true);
0N/A } else if (ccl == Long.TYPE) {
0N/A bin.readLongs((long[]) array, 0, len);
0N/A } else if (ccl == Float.TYPE) {
0N/A bin.readFloats((float[]) array, 0, len);
0N/A } else if (ccl == Double.TYPE) {
0N/A bin.readDoubles((double[]) array, 0, len);
0N/A } else if (ccl == Short.TYPE) {
0N/A bin.readShorts((short[]) array, 0, len);
0N/A } else if (ccl == Character.TYPE) {
0N/A bin.readChars((char[]) array, 0, len);
0N/A } else if (ccl == Boolean.TYPE) {
0N/A bin.readBooleans((boolean[]) array, 0, len);
0N/A } else {
0N/A throw new InternalError();
0N/A }
0N/A } else {
0N/A Object[] oa = (Object[]) array;
0N/A for (int i = 0; i < len; i++) {
0N/A oa[i] = readObject0(false);
0N/A handles.markDependency(arrayHandle, passHandle);
0N/A }
0N/A }
0N/A
0N/A handles.finish(arrayHandle);
0N/A passHandle = arrayHandle;
0N/A return array;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns enum constant, or null if enum type is
0N/A * unresolvable. Sets passHandle to enum constant's assigned handle.
0N/A */
0N/A private Enum readEnum(boolean unshared) throws IOException {
0N/A if (bin.readByte() != TC_ENUM) {
0N/A throw new InternalError();
0N/A }
0N/A
0N/A ObjectStreamClass desc = readClassDesc(false);
0N/A if (!desc.isEnum()) {
0N/A throw new InvalidClassException("non-enum class: " + desc);
0N/A }
0N/A
0N/A int enumHandle = handles.assign(unshared ? unsharedMarker : null);
0N/A ClassNotFoundException resolveEx = desc.getResolveException();
0N/A if (resolveEx != null) {
0N/A handles.markException(enumHandle, resolveEx);
0N/A }
0N/A
0N/A String name = readString(false);
0N/A Enum en = null;
0N/A Class cl = desc.forClass();
0N/A if (cl != null) {
0N/A try {
0N/A en = Enum.valueOf(cl, name);
0N/A } catch (IllegalArgumentException ex) {
0N/A throw (IOException) new InvalidObjectException(
0N/A "enum constant " + name + " does not exist in " +
0N/A cl).initCause(ex);
0N/A }
0N/A if (!unshared) {
0N/A handles.setObject(enumHandle, en);
0N/A }
0N/A }
0N/A
0N/A handles.finish(enumHandle);
0N/A passHandle = enumHandle;
0N/A return en;
0N/A }
0N/A
0N/A /**
0N/A * Reads and returns "ordinary" (i.e., not a String, Class,
0N/A * ObjectStreamClass, array, or enum constant) object, or null if object's
0N/A * class is unresolvable (in which case a ClassNotFoundException will be
0N/A * associated with object's handle). Sets passHandle to object's assigned
0N/A * handle.
0N/A */
0N/A private Object readOrdinaryObject(boolean unshared)
0N/A throws IOException
0N/A {
0N/A if (bin.readByte() != TC_OBJECT) {
0N/A throw new InternalError();
0N/A }
0N/A
0N/A ObjectStreamClass desc = readClassDesc(false);
0N/A desc.checkDeserialize();
0N/A
5699N/A Class<?> cl = desc.forClass();
5699N/A if (cl == String.class || cl == Class.class
5699N/A || cl == ObjectStreamClass.class) {
5699N/A throw new InvalidClassException("invalid class descriptor");
5699N/A }
5699N/A
0N/A Object obj;
0N/A try {
0N/A obj = desc.isInstantiable() ? desc.newInstance() : null;
0N/A } catch (Exception ex) {
0N/A throw (IOException) new InvalidClassException(
0N/A desc.forClass().getName(),
0N/A "unable to create instance").initCause(ex);
0N/A }
0N/A
0N/A passHandle = handles.assign(unshared ? unsharedMarker : obj);
0N/A ClassNotFoundException resolveEx = desc.getResolveException();
0N/A if (resolveEx != null) {
0N/A handles.markException(passHandle, resolveEx);
0N/A }
0N/A
0N/A if (desc.isExternalizable()) {
0N/A readExternalData((Externalizable) obj, desc);
0N/A } else {
0N/A readSerialData(obj, desc);
0N/A }
0N/A
0N/A handles.finish(passHandle);
0N/A
0N/A if (obj != null &&
0N/A handles.lookupException(passHandle) == null &&
0N/A desc.hasReadResolveMethod())
0N/A {
0N/A Object rep = desc.invokeReadResolve(obj);
0N/A if (unshared && rep.getClass().isArray()) {
0N/A rep = cloneArray(rep);
0N/A }
0N/A if (rep != obj) {
0N/A handles.setObject(passHandle, obj = rep);
0N/A }
0N/A }
0N/A
0N/A return obj;
0N/A }
0N/A
0N/A /**
0N/A * If obj is non-null, reads externalizable data by invoking readExternal()
0N/A * method of obj; otherwise, attempts to skip over externalizable data.
0N/A * Expects that passHandle is set to obj's handle before this method is
0N/A * called.
0N/A */
0N/A private void readExternalData(Externalizable obj, ObjectStreamClass desc)
0N/A throws IOException
0N/A {
2989N/A SerialCallbackContext oldContext = curContext;
0N/A curContext = null;
0N/A try {
0N/A boolean blocked = desc.hasBlockExternalData();
0N/A if (blocked) {
0N/A bin.setBlockDataMode(true);
0N/A }
0N/A if (obj != null) {
0N/A try {
0N/A obj.readExternal(this);
0N/A } catch (ClassNotFoundException ex) {
0N/A /*
0N/A * In most cases, the handle table has already propagated
0N/A * a CNFException to passHandle at this point; this mark
0N/A * call is included to address cases where the readExternal
0N/A * method has cons'ed and thrown a new CNFException of its
0N/A * own.
0N/A */
0N/A handles.markException(passHandle, ex);
0N/A }
0N/A }
0N/A if (blocked) {
0N/A skipCustomData();
0N/A }
0N/A } finally {
0N/A curContext = oldContext;
0N/A }
0N/A /*
0N/A * At this point, if the externalizable data was not written in
0N/A * block-data form and either the externalizable class doesn't exist
0N/A * locally (i.e., obj == null) or readExternal() just threw a
0N/A * CNFException, then the stream is probably in an inconsistent state,
0N/A * since some (or all) of the externalizable data may not have been
0N/A * consumed. Since there's no "correct" action to take in this case,
0N/A * we mimic the behavior of past serialization implementations and
0N/A * blindly hope that the stream is in sync; if it isn't and additional
0N/A * externalizable data remains in the stream, a subsequent read will
0N/A * most likely throw a StreamCorruptedException.
0N/A */
0N/A }
0N/A
0N/A /**
0N/A * Reads (or attempts to skip, if obj is null or is tagged with a
0N/A * ClassNotFoundException) instance data for each serializable class of
0N/A * object in stream, from superclass to subclass. Expects that passHandle
0N/A * is set to obj's handle before this method is called.
0N/A */
0N/A private void readSerialData(Object obj, ObjectStreamClass desc)
0N/A throws IOException
0N/A {
0N/A ObjectStreamClass.ClassDataSlot[] slots = desc.getClassDataLayout();
0N/A for (int i = 0; i < slots.length; i++) {
0N/A ObjectStreamClass slotDesc = slots[i].desc;
0N/A
0N/A if (slots[i].hasData) {
0N/A if (obj != null &&
0N/A slotDesc.hasReadObjectMethod() &&
0N/A handles.lookupException(passHandle) == null)
0N/A {
2989N/A SerialCallbackContext oldContext = curContext;
0N/A
0N/A try {
2989N/A curContext = new SerialCallbackContext(obj, slotDesc);
0N/A
0N/A bin.setBlockDataMode(true);
0N/A slotDesc.invokeReadObject(obj, this);
0N/A } catch (ClassNotFoundException ex) {
0N/A /*
0N/A * In most cases, the handle table has already
0N/A * propagated a CNFException to passHandle at this
0N/A * point; this mark call is included to address cases
0N/A * where the custom readObject method has cons'ed and
0N/A * thrown a new CNFException of its own.
0N/A */
0N/A handles.markException(passHandle, ex);
0N/A } finally {
0N/A curContext.setUsed();
0N/A curContext = oldContext;
0N/A }
0N/A
0N/A /*
0N/A * defaultDataEnd may have been set indirectly by custom
0N/A * readObject() method when calling defaultReadObject() or
0N/A * readFields(); clear it to restore normal read behavior.
0N/A */
0N/A defaultDataEnd = false;
0N/A } else {
0N/A defaultReadFields(obj, slotDesc);
0N/A }
0N/A if (slotDesc.hasWriteObjectData()) {
0N/A skipCustomData();
0N/A } else {
0N/A bin.setBlockDataMode(false);
0N/A }
0N/A } else {
0N/A if (obj != null &&
0N/A slotDesc.hasReadObjectNoDataMethod() &&
0N/A handles.lookupException(passHandle) == null)
0N/A {
0N/A slotDesc.invokeReadObjectNoData(obj);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Skips over all block data and objects until TC_ENDBLOCKDATA is
0N/A * encountered.
0N/A */
0N/A private void skipCustomData() throws IOException {
0N/A int oldHandle = passHandle;
0N/A for (;;) {
0N/A if (bin.getBlockDataMode()) {
0N/A bin.skipBlockData();
0N/A bin.setBlockDataMode(false);
0N/A }
0N/A switch (bin.peekByte()) {
0N/A case TC_BLOCKDATA:
0N/A case TC_BLOCKDATALONG:
0N/A bin.setBlockDataMode(true);
0N/A break;
0N/A
0N/A case TC_ENDBLOCKDATA:
0N/A bin.readByte();
0N/A passHandle = oldHandle;
0N/A return;
0N/A
0N/A default:
0N/A readObject0(false);
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads in values of serializable fields declared by given class
0N/A * descriptor. If obj is non-null, sets field values in obj. Expects that
0N/A * passHandle is set to obj's handle before this method is called.
0N/A */
0N/A private void defaultReadFields(Object obj, ObjectStreamClass desc)
0N/A throws IOException
0N/A {
0N/A // REMIND: is isInstance check necessary?
0N/A Class cl = desc.forClass();
0N/A if (cl != null && obj != null && !cl.isInstance(obj)) {
0N/A throw new ClassCastException();
0N/A }
0N/A
0N/A int primDataSize = desc.getPrimDataSize();
0N/A if (primVals == null || primVals.length < primDataSize) {
0N/A primVals = new byte[primDataSize];
0N/A }
0N/A bin.readFully(primVals, 0, primDataSize, false);
0N/A if (obj != null) {
0N/A desc.setPrimFieldValues(obj, primVals);
0N/A }
0N/A
0N/A int objHandle = passHandle;
0N/A ObjectStreamField[] fields = desc.getFields(false);
0N/A Object[] objVals = new Object[desc.getNumObjFields()];
0N/A int numPrimFields = fields.length - objVals.length;
0N/A for (int i = 0; i < objVals.length; i++) {
0N/A ObjectStreamField f = fields[numPrimFields + i];
0N/A objVals[i] = readObject0(f.isUnshared());
0N/A if (f.getField() != null) {
0N/A handles.markDependency(objHandle, passHandle);
0N/A }
0N/A }
0N/A if (obj != null) {
0N/A desc.setObjFieldValues(obj, objVals);
0N/A }
0N/A passHandle = objHandle;
0N/A }
0N/A
0N/A /**
0N/A * Reads in and returns IOException that caused serialization to abort.
0N/A * All stream state is discarded prior to reading in fatal exception. Sets
0N/A * passHandle to fatal exception's handle.
0N/A */
0N/A private IOException readFatalException() throws IOException {
0N/A if (bin.readByte() != TC_EXCEPTION) {
0N/A throw new InternalError();
0N/A }
0N/A clear();
0N/A return (IOException) readObject0(false);
0N/A }
0N/A
0N/A /**
0N/A * If recursion depth is 0, clears internal data structures; otherwise,
0N/A * throws a StreamCorruptedException. This method is called when a
0N/A * TC_RESET typecode is encountered.
0N/A */
0N/A private void handleReset() throws StreamCorruptedException {
0N/A if (depth > 0) {
0N/A throw new StreamCorruptedException(
0N/A "unexpected reset; recursion depth: " + depth);
0N/A }
0N/A clear();
0N/A }
0N/A
0N/A /**
0N/A * Converts specified span of bytes into float values.
0N/A */
0N/A // REMIND: remove once hotspot inlines Float.intBitsToFloat
0N/A private static native void bytesToFloats(byte[] src, int srcpos,
0N/A float[] dst, int dstpos,
0N/A int nfloats);
0N/A
0N/A /**
0N/A * Converts specified span of bytes into double values.
0N/A */
0N/A // REMIND: remove once hotspot inlines Double.longBitsToDouble
0N/A private static native void bytesToDoubles(byte[] src, int srcpos,
0N/A double[] dst, int dstpos,
0N/A int ndoubles);
0N/A
0N/A /**
0N/A * Returns the first non-null class loader (not counting class loaders of
0N/A * generated reflection implementation classes) up the execution stack, or
0N/A * null if only code from the null class loader is on the stack. This
0N/A * method is also called via reflection by the following RMI-IIOP class:
0N/A *
0N/A * com.sun.corba.se.internal.util.JDKClassLoader
0N/A *
0N/A * This method should not be removed or its signature changed without
0N/A * corresponding modifications to the above class.
0N/A */
5536N/A private static ClassLoader latestUserDefinedLoader() {
5536N/A return sun.misc.VM.latestUserDefinedLoader();
5536N/A }
0N/A
0N/A /**
0N/A * Default GetField implementation.
0N/A */
0N/A private class GetFieldImpl extends GetField {
0N/A
0N/A /** class descriptor describing serializable fields */
0N/A private final ObjectStreamClass desc;
0N/A /** primitive field values */
0N/A private final byte[] primVals;
0N/A /** object field values */
0N/A private final Object[] objVals;
0N/A /** object field value handles */
0N/A private final int[] objHandles;
0N/A
0N/A /**
0N/A * Creates GetFieldImpl object for reading fields defined in given
0N/A * class descriptor.
0N/A */
0N/A GetFieldImpl(ObjectStreamClass desc) {
0N/A this.desc = desc;
0N/A primVals = new byte[desc.getPrimDataSize()];
0N/A objVals = new Object[desc.getNumObjFields()];
0N/A objHandles = new int[objVals.length];
0N/A }
0N/A
0N/A public ObjectStreamClass getObjectStreamClass() {
0N/A return desc;
0N/A }
0N/A
0N/A public boolean defaulted(String name) throws IOException {
0N/A return (getFieldOffset(name, null) < 0);
0N/A }
0N/A
0N/A public boolean get(String name, boolean val) throws IOException {
0N/A int off = getFieldOffset(name, Boolean.TYPE);
0N/A return (off >= 0) ? Bits.getBoolean(primVals, off) : val;
0N/A }
0N/A
0N/A public byte get(String name, byte val) throws IOException {
0N/A int off = getFieldOffset(name, Byte.TYPE);
0N/A return (off >= 0) ? primVals[off] : val;
0N/A }
0N/A
0N/A public char get(String name, char val) throws IOException {
0N/A int off = getFieldOffset(name, Character.TYPE);
0N/A return (off >= 0) ? Bits.getChar(primVals, off) : val;
0N/A }
0N/A
0N/A public short get(String name, short val) throws IOException {
0N/A int off = getFieldOffset(name, Short.TYPE);
0N/A return (off >= 0) ? Bits.getShort(primVals, off) : val;
0N/A }
0N/A
0N/A public int get(String name, int val) throws IOException {
0N/A int off = getFieldOffset(name, Integer.TYPE);
0N/A return (off >= 0) ? Bits.getInt(primVals, off) : val;
0N/A }
0N/A
0N/A public float get(String name, float val) throws IOException {
0N/A int off = getFieldOffset(name, Float.TYPE);
0N/A return (off >= 0) ? Bits.getFloat(primVals, off) : val;
0N/A }
0N/A
0N/A public long get(String name, long val) throws IOException {
0N/A int off = getFieldOffset(name, Long.TYPE);
0N/A return (off >= 0) ? Bits.getLong(primVals, off) : val;
0N/A }
0N/A
0N/A public double get(String name, double val) throws IOException {
0N/A int off = getFieldOffset(name, Double.TYPE);
0N/A return (off >= 0) ? Bits.getDouble(primVals, off) : val;
0N/A }
0N/A
0N/A public Object get(String name, Object val) throws IOException {
0N/A int off = getFieldOffset(name, Object.class);
0N/A if (off >= 0) {
0N/A int objHandle = objHandles[off];
0N/A handles.markDependency(passHandle, objHandle);
0N/A return (handles.lookupException(objHandle) == null) ?
0N/A objVals[off] : null;
0N/A } else {
0N/A return val;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads primitive and object field values from stream.
0N/A */
0N/A void readFields() throws IOException {
0N/A bin.readFully(primVals, 0, primVals.length, false);
0N/A
0N/A int oldHandle = passHandle;
0N/A ObjectStreamField[] fields = desc.getFields(false);
0N/A int numPrimFields = fields.length - objVals.length;
0N/A for (int i = 0; i < objVals.length; i++) {
0N/A objVals[i] =
0N/A readObject0(fields[numPrimFields + i].isUnshared());
0N/A objHandles[i] = passHandle;
0N/A }
0N/A passHandle = oldHandle;
0N/A }
0N/A
0N/A /**
0N/A * Returns offset of field with given name and type. A specified type
0N/A * of null matches all types, Object.class matches all non-primitive
0N/A * types, and any other non-null type matches assignable types only.
0N/A * If no matching field is found in the (incoming) class
0N/A * descriptor but a matching field is present in the associated local
0N/A * class descriptor, returns -1. Throws IllegalArgumentException if
0N/A * neither incoming nor local class descriptor contains a match.
0N/A */
0N/A private int getFieldOffset(String name, Class type) {
0N/A ObjectStreamField field = desc.getField(name, type);
0N/A if (field != null) {
0N/A return field.getOffset();
0N/A } else if (desc.getLocalDesc().getField(name, type) != null) {
0N/A return -1;
0N/A } else {
0N/A throw new IllegalArgumentException("no such field " + name +
0N/A " with type " + type);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Prioritized list of callbacks to be performed once object graph has been
0N/A * completely deserialized.
0N/A */
0N/A private static class ValidationList {
0N/A
0N/A private static class Callback {
0N/A final ObjectInputValidation obj;
0N/A final int priority;
0N/A Callback next;
0N/A final AccessControlContext acc;
0N/A
0N/A Callback(ObjectInputValidation obj, int priority, Callback next,
0N/A AccessControlContext acc)
0N/A {
0N/A this.obj = obj;
0N/A this.priority = priority;
0N/A this.next = next;
0N/A this.acc = acc;
0N/A }
0N/A }
0N/A
0N/A /** linked list of callbacks */
0N/A private Callback list;
0N/A
0N/A /**
0N/A * Creates new (empty) ValidationList.
0N/A */
0N/A ValidationList() {
0N/A }
0N/A
0N/A /**
0N/A * Registers callback. Throws InvalidObjectException if callback
0N/A * object is null.
0N/A */
0N/A void register(ObjectInputValidation obj, int priority)
0N/A throws InvalidObjectException
0N/A {
0N/A if (obj == null) {
0N/A throw new InvalidObjectException("null callback");
0N/A }
0N/A
0N/A Callback prev = null, cur = list;
0N/A while (cur != null && priority < cur.priority) {
0N/A prev = cur;
0N/A cur = cur.next;
0N/A }
0N/A AccessControlContext acc = AccessController.getContext();
0N/A if (prev != null) {
0N/A prev.next = new Callback(obj, priority, cur, acc);
0N/A } else {
0N/A list = new Callback(obj, priority, list, acc);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Invokes all registered callbacks and clears the callback list.
0N/A * Callbacks with higher priorities are called first; those with equal
0N/A * priorities may be called in any order. If any of the callbacks
0N/A * throws an InvalidObjectException, the callback process is terminated
0N/A * and the exception propagated upwards.
0N/A */
0N/A void doCallbacks() throws InvalidObjectException {
0N/A try {
0N/A while (list != null) {
0N/A AccessController.doPrivileged(
28N/A new PrivilegedExceptionAction<Void>()
0N/A {
28N/A public Void run() throws InvalidObjectException {
0N/A list.obj.validateObject();
0N/A return null;
0N/A }
0N/A }, list.acc);
0N/A list = list.next;
0N/A }
0N/A } catch (PrivilegedActionException ex) {
0N/A list = null;
0N/A throw (InvalidObjectException) ex.getException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resets the callback list to its initial (empty) state.
0N/A */
0N/A public void clear() {
0N/A list = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Input stream supporting single-byte peek operations.
0N/A */
0N/A private static class PeekInputStream extends InputStream {
0N/A
0N/A /** underlying stream */
0N/A private final InputStream in;
0N/A /** peeked byte */
0N/A private int peekb = -1;
0N/A
0N/A /**
0N/A * Creates new PeekInputStream on top of given underlying stream.
0N/A */
0N/A PeekInputStream(InputStream in) {
0N/A this.in = in;
0N/A }
0N/A
0N/A /**
0N/A * Peeks at next byte value in stream. Similar to read(), except
0N/A * that it does not consume the read value.
0N/A */
0N/A int peek() throws IOException {
0N/A return (peekb >= 0) ? peekb : (peekb = in.read());
0N/A }
0N/A
0N/A public int read() throws IOException {
0N/A if (peekb >= 0) {
0N/A int v = peekb;
0N/A peekb = -1;
0N/A return v;
0N/A } else {
0N/A return in.read();
0N/A }
0N/A }
0N/A
0N/A public int read(byte[] b, int off, int len) throws IOException {
0N/A if (len == 0) {
0N/A return 0;
0N/A } else if (peekb < 0) {
0N/A return in.read(b, off, len);
0N/A } else {
0N/A b[off++] = (byte) peekb;
0N/A len--;
0N/A peekb = -1;
0N/A int n = in.read(b, off, len);
0N/A return (n >= 0) ? (n + 1) : 1;
0N/A }
0N/A }
0N/A
0N/A void readFully(byte[] b, int off, int len) throws IOException {
0N/A int n = 0;
0N/A while (n < len) {
0N/A int count = read(b, off + n, len - n);
0N/A if (count < 0) {
0N/A throw new EOFException();
0N/A }
0N/A n += count;
0N/A }
0N/A }
0N/A
0N/A public long skip(long n) throws IOException {
0N/A if (n <= 0) {
0N/A return 0;
0N/A }
0N/A int skipped = 0;
0N/A if (peekb >= 0) {
0N/A peekb = -1;
0N/A skipped++;
0N/A n--;
0N/A }
0N/A return skipped + skip(n);
0N/A }
0N/A
0N/A public int available() throws IOException {
0N/A return in.available() + ((peekb >= 0) ? 1 : 0);
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A in.close();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Input stream with two modes: in default mode, inputs data written in the
0N/A * same format as DataOutputStream; in "block data" mode, inputs data
0N/A * bracketed by block data markers (see object serialization specification
0N/A * for details). Buffering depends on block data mode: when in default
0N/A * mode, no data is buffered in advance; when in block data mode, all data
0N/A * for the current data block is read in at once (and buffered).
0N/A */
0N/A private class BlockDataInputStream
0N/A extends InputStream implements DataInput
0N/A {
0N/A /** maximum data block length */
0N/A private static final int MAX_BLOCK_SIZE = 1024;
0N/A /** maximum data block header length */
0N/A private static final int MAX_HEADER_SIZE = 5;
0N/A /** (tunable) length of char buffer (for reading strings) */
0N/A private static final int CHAR_BUF_SIZE = 256;
0N/A /** readBlockHeader() return value indicating header read may block */
0N/A private static final int HEADER_BLOCKED = -2;
0N/A
0N/A /** buffer for reading general/block data */
0N/A private final byte[] buf = new byte[MAX_BLOCK_SIZE];
0N/A /** buffer for reading block data headers */
0N/A private final byte[] hbuf = new byte[MAX_HEADER_SIZE];
0N/A /** char buffer for fast string reads */
0N/A private final char[] cbuf = new char[CHAR_BUF_SIZE];
0N/A
0N/A /** block data mode */
0N/A private boolean blkmode = false;
0N/A
0N/A // block data state fields; values meaningful only when blkmode true
0N/A /** current offset into buf */
0N/A private int pos = 0;
0N/A /** end offset of valid data in buf, or -1 if no more block data */
0N/A private int end = -1;
0N/A /** number of bytes in current block yet to be read from stream */
0N/A private int unread = 0;
0N/A
0N/A /** underlying stream (wrapped in peekable filter stream) */
0N/A private final PeekInputStream in;
0N/A /** loopback stream (for data reads that span data blocks) */
0N/A private final DataInputStream din;
0N/A
0N/A /**
0N/A * Creates new BlockDataInputStream on top of given underlying stream.
0N/A * Block data mode is turned off by default.
0N/A */
0N/A BlockDataInputStream(InputStream in) {
0N/A this.in = new PeekInputStream(in);
0N/A din = new DataInputStream(this);
0N/A }
0N/A
0N/A /**
0N/A * Sets block data mode to the given mode (true == on, false == off)
0N/A * and returns the previous mode value. If the new mode is the same as
0N/A * the old mode, no action is taken. Throws IllegalStateException if
0N/A * block data mode is being switched from on to off while unconsumed
0N/A * block data is still present in the stream.
0N/A */
0N/A boolean setBlockDataMode(boolean newmode) throws IOException {
0N/A if (blkmode == newmode) {
0N/A return blkmode;
0N/A }
0N/A if (newmode) {
0N/A pos = 0;
0N/A end = 0;
0N/A unread = 0;
0N/A } else if (pos < end) {
0N/A throw new IllegalStateException("unread block data");
0N/A }
0N/A blkmode = newmode;
0N/A return !blkmode;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the stream is currently in block data mode, false
0N/A * otherwise.
0N/A */
0N/A boolean getBlockDataMode() {
0N/A return blkmode;
0N/A }
0N/A
0N/A /**
0N/A * If in block data mode, skips to the end of the current group of data
0N/A * blocks (but does not unset block data mode). If not in block data
0N/A * mode, throws an IllegalStateException.
0N/A */
0N/A void skipBlockData() throws IOException {
0N/A if (!blkmode) {
0N/A throw new IllegalStateException("not in block data mode");
0N/A }
0N/A while (end >= 0) {
0N/A refill();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Attempts to read in the next block data header (if any). If
0N/A * canBlock is false and a full header cannot be read without possibly
0N/A * blocking, returns HEADER_BLOCKED, else if the next element in the
0N/A * stream is a block data header, returns the block data length
0N/A * specified by the header, else returns -1.
0N/A */
0N/A private int readBlockHeader(boolean canBlock) throws IOException {
0N/A if (defaultDataEnd) {
0N/A /*
0N/A * Fix for 4360508: stream is currently at the end of a field
0N/A * value block written via default serialization; since there
0N/A * is no terminating TC_ENDBLOCKDATA tag, simulate
0N/A * end-of-custom-data behavior explicitly.
0N/A */
0N/A return -1;
0N/A }
0N/A try {
0N/A for (;;) {
0N/A int avail = canBlock ? Integer.MAX_VALUE : in.available();
0N/A if (avail == 0) {
0N/A return HEADER_BLOCKED;
0N/A }
0N/A
0N/A int tc = in.peek();
0N/A switch (tc) {
0N/A case TC_BLOCKDATA:
0N/A if (avail < 2) {
0N/A return HEADER_BLOCKED;
0N/A }
0N/A in.readFully(hbuf, 0, 2);
0N/A return hbuf[1] & 0xFF;
0N/A
0N/A case TC_BLOCKDATALONG:
0N/A if (avail < 5) {
0N/A return HEADER_BLOCKED;
0N/A }
0N/A in.readFully(hbuf, 0, 5);
0N/A int len = Bits.getInt(hbuf, 1);
0N/A if (len < 0) {
0N/A throw new StreamCorruptedException(
0N/A "illegal block data header length: " +
0N/A len);
0N/A }
0N/A return len;
0N/A
0N/A /*
0N/A * TC_RESETs may occur in between data blocks.
0N/A * Unfortunately, this case must be parsed at a lower
0N/A * level than other typecodes, since primitive data
0N/A * reads may span data blocks separated by a TC_RESET.
0N/A */
0N/A case TC_RESET:
0N/A in.read();
0N/A handleReset();
0N/A break;
0N/A
0N/A default:
0N/A if (tc >= 0 && (tc < TC_BASE || tc > TC_MAX)) {
0N/A throw new StreamCorruptedException(
0N/A String.format("invalid type code: %02X",
0N/A tc));
0N/A }
0N/A return -1;
0N/A }
0N/A }
0N/A } catch (EOFException ex) {
0N/A throw new StreamCorruptedException(
0N/A "unexpected EOF while reading block data header");
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Refills internal buffer buf with block data. Any data in buf at the
0N/A * time of the call is considered consumed. Sets the pos, end, and
0N/A * unread fields to reflect the new amount of available block data; if
0N/A * the next element in the stream is not a data block, sets pos and
0N/A * unread to 0 and end to -1.
0N/A */
0N/A private void refill() throws IOException {
0N/A try {
0N/A do {
0N/A pos = 0;
0N/A if (unread > 0) {
0N/A int n =
0N/A in.read(buf, 0, Math.min(unread, MAX_BLOCK_SIZE));
0N/A if (n >= 0) {
0N/A end = n;
0N/A unread -= n;
0N/A } else {
0N/A throw new StreamCorruptedException(
0N/A "unexpected EOF in middle of data block");
0N/A }
0N/A } else {
0N/A int n = readBlockHeader(true);
0N/A if (n >= 0) {
0N/A end = 0;
0N/A unread = n;
0N/A } else {
0N/A end = -1;
0N/A unread = 0;
0N/A }
0N/A }
0N/A } while (pos == end);
0N/A } catch (IOException ex) {
0N/A pos = 0;
0N/A end = -1;
0N/A unread = 0;
0N/A throw ex;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * If in block data mode, returns the number of unconsumed bytes
0N/A * remaining in the current data block. If not in block data mode,
0N/A * throws an IllegalStateException.
0N/A */
0N/A int currentBlockRemaining() {
0N/A if (blkmode) {
0N/A return (end >= 0) ? (end - pos) + unread : 0;
0N/A } else {
0N/A throw new IllegalStateException();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Peeks at (but does not consume) and returns the next byte value in
0N/A * the stream, or -1 if the end of the stream/block data (if in block
0N/A * data mode) has been reached.
0N/A */
0N/A int peek() throws IOException {
0N/A if (blkmode) {
0N/A if (pos == end) {
0N/A refill();
0N/A }
0N/A return (end >= 0) ? (buf[pos] & 0xFF) : -1;
0N/A } else {
0N/A return in.peek();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Peeks at (but does not consume) and returns the next byte value in
0N/A * the stream, or throws EOFException if end of stream/block data has
0N/A * been reached.
0N/A */
0N/A byte peekByte() throws IOException {
0N/A int val = peek();
0N/A if (val < 0) {
0N/A throw new EOFException();
0N/A }
0N/A return (byte) val;
0N/A }
0N/A
0N/A
0N/A /* ----------------- generic input stream methods ------------------ */
0N/A /*
0N/A * The following methods are equivalent to their counterparts in
0N/A * InputStream, except that they interpret data block boundaries and
0N/A * read the requested data from within data blocks when in block data
0N/A * mode.
0N/A */
0N/A
0N/A public int read() throws IOException {
0N/A if (blkmode) {
0N/A if (pos == end) {
0N/A refill();
0N/A }
0N/A return (end >= 0) ? (buf[pos++] & 0xFF) : -1;
0N/A } else {
0N/A return in.read();
0N/A }
0N/A }
0N/A
0N/A public int read(byte[] b, int off, int len) throws IOException {
0N/A return read(b, off, len, false);
0N/A }
0N/A
0N/A public long skip(long len) throws IOException {
0N/A long remain = len;
0N/A while (remain > 0) {
0N/A if (blkmode) {
0N/A if (pos == end) {
0N/A refill();
0N/A }
0N/A if (end < 0) {
0N/A break;
0N/A }
0N/A int nread = (int) Math.min(remain, end - pos);
0N/A remain -= nread;
0N/A pos += nread;
0N/A } else {
0N/A int nread = (int) Math.min(remain, MAX_BLOCK_SIZE);
0N/A if ((nread = in.read(buf, 0, nread)) < 0) {
0N/A break;
0N/A }
0N/A remain -= nread;
0N/A }
0N/A }
0N/A return len - remain;
0N/A }
0N/A
0N/A public int available() throws IOException {
0N/A if (blkmode) {
0N/A if ((pos == end) && (unread == 0)) {
0N/A int n;
0N/A while ((n = readBlockHeader(false)) == 0) ;
0N/A switch (n) {
0N/A case HEADER_BLOCKED:
0N/A break;
0N/A
0N/A case -1:
0N/A pos = 0;
0N/A end = -1;
0N/A break;
0N/A
0N/A default:
0N/A pos = 0;
0N/A end = 0;
0N/A unread = n;
0N/A break;
0N/A }
0N/A }
0N/A // avoid unnecessary call to in.available() if possible
0N/A int unreadAvail = (unread > 0) ?
0N/A Math.min(in.available(), unread) : 0;
0N/A return (end >= 0) ? (end - pos) + unreadAvail : 0;
0N/A } else {
0N/A return in.available();
0N/A }
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A if (blkmode) {
0N/A pos = 0;
0N/A end = -1;
0N/A unread = 0;
0N/A }
0N/A in.close();
0N/A }
0N/A
0N/A /**
0N/A * Attempts to read len bytes into byte array b at offset off. Returns
0N/A * the number of bytes read, or -1 if the end of stream/block data has
0N/A * been reached. If copy is true, reads values into an intermediate
0N/A * buffer before copying them to b (to avoid exposing a reference to
0N/A * b).
0N/A */
0N/A int read(byte[] b, int off, int len, boolean copy) throws IOException {
0N/A if (len == 0) {
0N/A return 0;
0N/A } else if (blkmode) {
0N/A if (pos == end) {
0N/A refill();
0N/A }
0N/A if (end < 0) {
0N/A return -1;
0N/A }
0N/A int nread = Math.min(len, end - pos);
0N/A System.arraycopy(buf, pos, b, off, nread);
0N/A pos += nread;
0N/A return nread;
0N/A } else if (copy) {
0N/A int nread = in.read(buf, 0, Math.min(len, MAX_BLOCK_SIZE));
0N/A if (nread > 0) {
0N/A System.arraycopy(buf, 0, b, off, nread);
0N/A }
0N/A return nread;
0N/A } else {
0N/A return in.read(b, off, len);
0N/A }
0N/A }
0N/A
0N/A /* ----------------- primitive data input methods ------------------ */
0N/A /*
0N/A * The following methods are equivalent to their counterparts in
0N/A * DataInputStream, except that they interpret data block boundaries
0N/A * and read the requested data from within data blocks when in block
0N/A * data mode.
0N/A */
0N/A
0N/A public void readFully(byte[] b) throws IOException {
0N/A readFully(b, 0, b.length, false);
0N/A }
0N/A
0N/A public void readFully(byte[] b, int off, int len) throws IOException {
0N/A readFully(b, off, len, false);
0N/A }
0N/A
0N/A public void readFully(byte[] b, int off, int len, boolean copy)
0N/A throws IOException
0N/A {
0N/A while (len > 0) {
0N/A int n = read(b, off, len, copy);
0N/A if (n < 0) {
0N/A throw new EOFException();
0N/A }
0N/A off += n;
0N/A len -= n;
0N/A }
0N/A }
0N/A
0N/A public int skipBytes(int n) throws IOException {
0N/A return din.skipBytes(n);
0N/A }
0N/A
0N/A public boolean readBoolean() throws IOException {
0N/A int v = read();
0N/A if (v < 0) {
0N/A throw new EOFException();
0N/A }
0N/A return (v != 0);
0N/A }
0N/A
0N/A public byte readByte() throws IOException {
0N/A int v = read();
0N/A if (v < 0) {
0N/A throw new EOFException();
0N/A }
0N/A return (byte) v;
0N/A }
0N/A
0N/A public int readUnsignedByte() throws IOException {
0N/A int v = read();
0N/A if (v < 0) {
0N/A throw new EOFException();
0N/A }
0N/A return v;
0N/A }
0N/A
0N/A public char readChar() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 2);
0N/A } else if (end - pos < 2) {
0N/A return din.readChar();
0N/A }
0N/A char v = Bits.getChar(buf, pos);
0N/A pos += 2;
0N/A return v;
0N/A }
0N/A
0N/A public short readShort() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 2);
0N/A } else if (end - pos < 2) {
0N/A return din.readShort();
0N/A }
0N/A short v = Bits.getShort(buf, pos);
0N/A pos += 2;
0N/A return v;
0N/A }
0N/A
0N/A public int readUnsignedShort() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 2);
0N/A } else if (end - pos < 2) {
0N/A return din.readUnsignedShort();
0N/A }
0N/A int v = Bits.getShort(buf, pos) & 0xFFFF;
0N/A pos += 2;
0N/A return v;
0N/A }
0N/A
0N/A public int readInt() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 4);
0N/A } else if (end - pos < 4) {
0N/A return din.readInt();
0N/A }
0N/A int v = Bits.getInt(buf, pos);
0N/A pos += 4;
0N/A return v;
0N/A }
0N/A
0N/A public float readFloat() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 4);
0N/A } else if (end - pos < 4) {
0N/A return din.readFloat();
0N/A }
0N/A float v = Bits.getFloat(buf, pos);
0N/A pos += 4;
0N/A return v;
0N/A }
0N/A
0N/A public long readLong() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 8);
0N/A } else if (end - pos < 8) {
0N/A return din.readLong();
0N/A }
0N/A long v = Bits.getLong(buf, pos);
0N/A pos += 8;
0N/A return v;
0N/A }
0N/A
0N/A public double readDouble() throws IOException {
0N/A if (!blkmode) {
0N/A pos = 0;
0N/A in.readFully(buf, 0, 8);
0N/A } else if (end - pos < 8) {
0N/A return din.readDouble();
0N/A }
0N/A double v = Bits.getDouble(buf, pos);
0N/A pos += 8;
0N/A return v;
0N/A }
0N/A
0N/A public String readUTF() throws IOException {
0N/A return readUTFBody(readUnsignedShort());
0N/A }
0N/A
0N/A public String readLine() throws IOException {
0N/A return din.readLine(); // deprecated, not worth optimizing
0N/A }
0N/A
0N/A /* -------------- primitive data array input methods --------------- */
0N/A /*
0N/A * The following methods read in spans of primitive data values.
0N/A * Though equivalent to calling the corresponding primitive read
0N/A * methods repeatedly, these methods are optimized for reading groups
0N/A * of primitive data values more efficiently.
0N/A */
0N/A
0N/A void readBooleans(boolean[] v, int off, int len) throws IOException {
0N/A int stop, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A int span = Math.min(endoff - off, MAX_BLOCK_SIZE);
0N/A in.readFully(buf, 0, span);
0N/A stop = off + span;
0N/A pos = 0;
0N/A } else if (end - pos < 1) {
0N/A v[off++] = din.readBoolean();
0N/A continue;
0N/A } else {
0N/A stop = Math.min(endoff, off + end - pos);
0N/A }
0N/A
0N/A while (off < stop) {
0N/A v[off++] = Bits.getBoolean(buf, pos++);
0N/A }
0N/A }
0N/A }
0N/A
0N/A void readChars(char[] v, int off, int len) throws IOException {
0N/A int stop, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 1);
0N/A in.readFully(buf, 0, span << 1);
0N/A stop = off + span;
0N/A pos = 0;
0N/A } else if (end - pos < 2) {
0N/A v[off++] = din.readChar();
0N/A continue;
0N/A } else {
0N/A stop = Math.min(endoff, off + ((end - pos) >> 1));
0N/A }
0N/A
0N/A while (off < stop) {
0N/A v[off++] = Bits.getChar(buf, pos);
0N/A pos += 2;
0N/A }
0N/A }
0N/A }
0N/A
0N/A void readShorts(short[] v, int off, int len) throws IOException {
0N/A int stop, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 1);
0N/A in.readFully(buf, 0, span << 1);
0N/A stop = off + span;
0N/A pos = 0;
0N/A } else if (end - pos < 2) {
0N/A v[off++] = din.readShort();
0N/A continue;
0N/A } else {
0N/A stop = Math.min(endoff, off + ((end - pos) >> 1));
0N/A }
0N/A
0N/A while (off < stop) {
0N/A v[off++] = Bits.getShort(buf, pos);
0N/A pos += 2;
0N/A }
0N/A }
0N/A }
0N/A
0N/A void readInts(int[] v, int off, int len) throws IOException {
0N/A int stop, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 2);
0N/A in.readFully(buf, 0, span << 2);
0N/A stop = off + span;
0N/A pos = 0;
0N/A } else if (end - pos < 4) {
0N/A v[off++] = din.readInt();
0N/A continue;
0N/A } else {
0N/A stop = Math.min(endoff, off + ((end - pos) >> 2));
0N/A }
0N/A
0N/A while (off < stop) {
0N/A v[off++] = Bits.getInt(buf, pos);
0N/A pos += 4;
0N/A }
0N/A }
0N/A }
0N/A
0N/A void readFloats(float[] v, int off, int len) throws IOException {
0N/A int span, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 2);
0N/A in.readFully(buf, 0, span << 2);
0N/A pos = 0;
0N/A } else if (end - pos < 4) {
0N/A v[off++] = din.readFloat();
0N/A continue;
0N/A } else {
0N/A span = Math.min(endoff - off, ((end - pos) >> 2));
0N/A }
0N/A
0N/A bytesToFloats(buf, pos, v, off, span);
0N/A off += span;
0N/A pos += span << 2;
0N/A }
0N/A }
0N/A
0N/A void readLongs(long[] v, int off, int len) throws IOException {
0N/A int stop, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A int span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 3);
0N/A in.readFully(buf, 0, span << 3);
0N/A stop = off + span;
0N/A pos = 0;
0N/A } else if (end - pos < 8) {
0N/A v[off++] = din.readLong();
0N/A continue;
0N/A } else {
0N/A stop = Math.min(endoff, off + ((end - pos) >> 3));
0N/A }
0N/A
0N/A while (off < stop) {
0N/A v[off++] = Bits.getLong(buf, pos);
0N/A pos += 8;
0N/A }
0N/A }
0N/A }
0N/A
0N/A void readDoubles(double[] v, int off, int len) throws IOException {
0N/A int span, endoff = off + len;
0N/A while (off < endoff) {
0N/A if (!blkmode) {
0N/A span = Math.min(endoff - off, MAX_BLOCK_SIZE >> 3);
0N/A in.readFully(buf, 0, span << 3);
0N/A pos = 0;
0N/A } else if (end - pos < 8) {
0N/A v[off++] = din.readDouble();
0N/A continue;
0N/A } else {
0N/A span = Math.min(endoff - off, ((end - pos) >> 3));
0N/A }
0N/A
0N/A bytesToDoubles(buf, pos, v, off, span);
0N/A off += span;
0N/A pos += span << 3;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads in string written in "long" UTF format. "Long" UTF format is
0N/A * identical to standard UTF, except that it uses an 8 byte header
0N/A * (instead of the standard 2 bytes) to convey the UTF encoding length.
0N/A */
0N/A String readLongUTF() throws IOException {
0N/A return readUTFBody(readLong());
0N/A }
0N/A
0N/A /**
0N/A * Reads in the "body" (i.e., the UTF representation minus the 2-byte
0N/A * or 8-byte length header) of a UTF encoding, which occupies the next
0N/A * utflen bytes.
0N/A */
0N/A private String readUTFBody(long utflen) throws IOException {
0N/A StringBuilder sbuf = new StringBuilder();
0N/A if (!blkmode) {
0N/A end = pos = 0;
0N/A }
0N/A
0N/A while (utflen > 0) {
0N/A int avail = end - pos;
0N/A if (avail >= 3 || (long) avail == utflen) {
0N/A utflen -= readUTFSpan(sbuf, utflen);
0N/A } else {
0N/A if (blkmode) {
0N/A // near block boundary, read one byte at a time
0N/A utflen -= readUTFChar(sbuf, utflen);
0N/A } else {
0N/A // shift and refill buffer manually
0N/A if (avail > 0) {
0N/A System.arraycopy(buf, pos, buf, 0, avail);
0N/A }
0N/A pos = 0;
0N/A end = (int) Math.min(MAX_BLOCK_SIZE, utflen);
0N/A in.readFully(buf, avail, end - avail);
0N/A }
0N/A }
0N/A }
0N/A
0N/A return sbuf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Reads span of UTF-encoded characters out of internal buffer
0N/A * (starting at offset pos and ending at or before offset end),
0N/A * consuming no more than utflen bytes. Appends read characters to
0N/A * sbuf. Returns the number of bytes consumed.
0N/A */
0N/A private long readUTFSpan(StringBuilder sbuf, long utflen)
0N/A throws IOException
0N/A {
0N/A int cpos = 0;
0N/A int start = pos;
0N/A int avail = Math.min(end - pos, CHAR_BUF_SIZE);
0N/A // stop short of last char unless all of utf bytes in buffer
0N/A int stop = pos + ((utflen > avail) ? avail - 2 : (int) utflen);
0N/A boolean outOfBounds = false;
0N/A
0N/A try {
0N/A while (pos < stop) {
0N/A int b1, b2, b3;
0N/A b1 = buf[pos++] & 0xFF;
0N/A switch (b1 >> 4) {
0N/A case 0:
0N/A case 1:
0N/A case 2:
0N/A case 3:
0N/A case 4:
0N/A case 5:
0N/A case 6:
0N/A case 7: // 1 byte format: 0xxxxxxx
0N/A cbuf[cpos++] = (char) b1;
0N/A break;
0N/A
0N/A case 12:
0N/A case 13: // 2 byte format: 110xxxxx 10xxxxxx
0N/A b2 = buf[pos++];
0N/A if ((b2 & 0xC0) != 0x80) {
0N/A throw new UTFDataFormatException();
0N/A }
0N/A cbuf[cpos++] = (char) (((b1 & 0x1F) << 6) |
0N/A ((b2 & 0x3F) << 0));
0N/A break;
0N/A
0N/A case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
0N/A b3 = buf[pos + 1];
0N/A b2 = buf[pos + 0];
0N/A pos += 2;
0N/A if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) {
0N/A throw new UTFDataFormatException();
0N/A }
0N/A cbuf[cpos++] = (char) (((b1 & 0x0F) << 12) |
0N/A ((b2 & 0x3F) << 6) |
0N/A ((b3 & 0x3F) << 0));
0N/A break;
0N/A
0N/A default: // 10xx xxxx, 1111 xxxx
0N/A throw new UTFDataFormatException();
0N/A }
0N/A }
0N/A } catch (ArrayIndexOutOfBoundsException ex) {
0N/A outOfBounds = true;
0N/A } finally {
0N/A if (outOfBounds || (pos - start) > utflen) {
0N/A /*
0N/A * Fix for 4450867: if a malformed utf char causes the
0N/A * conversion loop to scan past the expected end of the utf
0N/A * string, only consume the expected number of utf bytes.
0N/A */
0N/A pos = start + (int) utflen;
0N/A throw new UTFDataFormatException();
0N/A }
0N/A }
0N/A
0N/A sbuf.append(cbuf, 0, cpos);
0N/A return pos - start;
0N/A }
0N/A
0N/A /**
0N/A * Reads in single UTF-encoded character one byte at a time, appends
0N/A * the character to sbuf, and returns the number of bytes consumed.
0N/A * This method is used when reading in UTF strings written in block
0N/A * data mode to handle UTF-encoded characters which (potentially)
0N/A * straddle block-data boundaries.
0N/A */
0N/A private int readUTFChar(StringBuilder sbuf, long utflen)
0N/A throws IOException
0N/A {
0N/A int b1, b2, b3;
0N/A b1 = readByte() & 0xFF;
0N/A switch (b1 >> 4) {
0N/A case 0:
0N/A case 1:
0N/A case 2:
0N/A case 3:
0N/A case 4:
0N/A case 5:
0N/A case 6:
0N/A case 7: // 1 byte format: 0xxxxxxx
0N/A sbuf.append((char) b1);
0N/A return 1;
0N/A
0N/A case 12:
0N/A case 13: // 2 byte format: 110xxxxx 10xxxxxx
0N/A if (utflen < 2) {
0N/A throw new UTFDataFormatException();
0N/A }
0N/A b2 = readByte();
0N/A if ((b2 & 0xC0) != 0x80) {
0N/A throw new UTFDataFormatException();
0N/A }
0N/A sbuf.append((char) (((b1 & 0x1F) << 6) |
0N/A ((b2 & 0x3F) << 0)));
0N/A return 2;
0N/A
0N/A case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
0N/A if (utflen < 3) {
0N/A if (utflen == 2) {
0N/A readByte(); // consume remaining byte
0N/A }
0N/A throw new UTFDataFormatException();
0N/A }
0N/A b2 = readByte();
0N/A b3 = readByte();
0N/A if ((b2 & 0xC0) != 0x80 || (b3 & 0xC0) != 0x80) {
0N/A throw new UTFDataFormatException();
0N/A }
0N/A sbuf.append((char) (((b1 & 0x0F) << 12) |
0N/A ((b2 & 0x3F) << 6) |
0N/A ((b3 & 0x3F) << 0)));
0N/A return 3;
0N/A
0N/A default: // 10xx xxxx, 1111 xxxx
0N/A throw new UTFDataFormatException();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Unsynchronized table which tracks wire handle to object mappings, as
0N/A * well as ClassNotFoundExceptions associated with deserialized objects.
0N/A * This class implements an exception-propagation algorithm for
0N/A * determining which objects should have ClassNotFoundExceptions associated
0N/A * with them, taking into account cycles and discontinuities (e.g., skipped
0N/A * fields) in the object graph.
0N/A *
0N/A * <p>General use of the table is as follows: during deserialization, a
0N/A * given object is first assigned a handle by calling the assign method.
0N/A * This method leaves the assigned handle in an "open" state, wherein
0N/A * dependencies on the exception status of other handles can be registered
0N/A * by calling the markDependency method, or an exception can be directly
0N/A * associated with the handle by calling markException. When a handle is
0N/A * tagged with an exception, the HandleTable assumes responsibility for
0N/A * propagating the exception to any other objects which depend
0N/A * (transitively) on the exception-tagged object.
0N/A *
0N/A * <p>Once all exception information/dependencies for the handle have been
0N/A * registered, the handle should be "closed" by calling the finish method
0N/A * on it. The act of finishing a handle allows the exception propagation
0N/A * algorithm to aggressively prune dependency links, lessening the
0N/A * performance/memory impact of exception tracking.
0N/A *
0N/A * <p>Note that the exception propagation algorithm used depends on handles
0N/A * being assigned/finished in LIFO order; however, for simplicity as well
0N/A * as memory conservation, it does not enforce this constraint.
0N/A */
0N/A // REMIND: add full description of exception propagation algorithm?
0N/A private static class HandleTable {
0N/A
0N/A /* status codes indicating whether object has associated exception */
0N/A private static final byte STATUS_OK = 1;
0N/A private static final byte STATUS_UNKNOWN = 2;
0N/A private static final byte STATUS_EXCEPTION = 3;
0N/A
0N/A /** array mapping handle -> object status */
0N/A byte[] status;
0N/A /** array mapping handle -> object/exception (depending on status) */
0N/A Object[] entries;
0N/A /** array mapping handle -> list of dependent handles (if any) */
0N/A HandleList[] deps;
0N/A /** lowest unresolved dependency */
0N/A int lowDep = -1;
0N/A /** number of handles in table */
0N/A int size = 0;
0N/A
0N/A /**
0N/A * Creates handle table with the given initial capacity.
0N/A */
0N/A HandleTable(int initialCapacity) {
0N/A status = new byte[initialCapacity];
0N/A entries = new Object[initialCapacity];
0N/A deps = new HandleList[initialCapacity];
0N/A }
0N/A
0N/A /**
0N/A * Assigns next available handle to given object, and returns assigned
0N/A * handle. Once object has been completely deserialized (and all
0N/A * dependencies on other objects identified), the handle should be
0N/A * "closed" by passing it to finish().
0N/A */
0N/A int assign(Object obj) {
0N/A if (size >= entries.length) {
0N/A grow();
0N/A }
0N/A status[size] = STATUS_UNKNOWN;
0N/A entries[size] = obj;
0N/A return size++;
0N/A }
0N/A
0N/A /**
0N/A * Registers a dependency (in exception status) of one handle on
0N/A * another. The dependent handle must be "open" (i.e., assigned, but
0N/A * not finished yet). No action is taken if either dependent or target
0N/A * handle is NULL_HANDLE.
0N/A */
0N/A void markDependency(int dependent, int target) {
0N/A if (dependent == NULL_HANDLE || target == NULL_HANDLE) {
0N/A return;
0N/A }
0N/A switch (status[dependent]) {
0N/A
0N/A case STATUS_UNKNOWN:
0N/A switch (status[target]) {
0N/A case STATUS_OK:
0N/A // ignore dependencies on objs with no exception
0N/A break;
0N/A
0N/A case STATUS_EXCEPTION:
0N/A // eagerly propagate exception
0N/A markException(dependent,
0N/A (ClassNotFoundException) entries[target]);
0N/A break;
0N/A
0N/A case STATUS_UNKNOWN:
0N/A // add to dependency list of target
0N/A if (deps[target] == null) {
0N/A deps[target] = new HandleList();
0N/A }
0N/A deps[target].add(dependent);
0N/A
0N/A // remember lowest unresolved target seen
0N/A if (lowDep < 0 || lowDep > target) {
0N/A lowDep = target;
0N/A }
0N/A break;
0N/A
0N/A default:
0N/A throw new InternalError();
0N/A }
0N/A break;
0N/A
0N/A case STATUS_EXCEPTION:
0N/A break;
0N/A
0N/A default:
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Associates a ClassNotFoundException (if one not already associated)
0N/A * with the currently active handle and propagates it to other
0N/A * referencing objects as appropriate. The specified handle must be
0N/A * "open" (i.e., assigned, but not finished yet).
0N/A */
0N/A void markException(int handle, ClassNotFoundException ex) {
0N/A switch (status[handle]) {
0N/A case STATUS_UNKNOWN:
0N/A status[handle] = STATUS_EXCEPTION;
0N/A entries[handle] = ex;
0N/A
0N/A // propagate exception to dependents
0N/A HandleList dlist = deps[handle];
0N/A if (dlist != null) {
0N/A int ndeps = dlist.size();
0N/A for (int i = 0; i < ndeps; i++) {
0N/A markException(dlist.get(i), ex);
0N/A }
0N/A deps[handle] = null;
0N/A }
0N/A break;
0N/A
0N/A case STATUS_EXCEPTION:
0N/A break;
0N/A
0N/A default:
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Marks given handle as finished, meaning that no new dependencies
0N/A * will be marked for handle. Calls to the assign and finish methods
0N/A * must occur in LIFO order.
0N/A */
0N/A void finish(int handle) {
0N/A int end;
0N/A if (lowDep < 0) {
0N/A // no pending unknowns, only resolve current handle
0N/A end = handle + 1;
0N/A } else if (lowDep >= handle) {
0N/A // pending unknowns now clearable, resolve all upward handles
0N/A end = size;
0N/A lowDep = -1;
0N/A } else {
0N/A // unresolved backrefs present, can't resolve anything yet
0N/A return;
0N/A }
0N/A
0N/A // change STATUS_UNKNOWN -> STATUS_OK in selected span of handles
0N/A for (int i = handle; i < end; i++) {
0N/A switch (status[i]) {
0N/A case STATUS_UNKNOWN:
0N/A status[i] = STATUS_OK;
0N/A deps[i] = null;
0N/A break;
0N/A
0N/A case STATUS_OK:
0N/A case STATUS_EXCEPTION:
0N/A break;
0N/A
0N/A default:
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Assigns a new object to the given handle. The object previously
0N/A * associated with the handle is forgotten. This method has no effect
0N/A * if the given handle already has an exception associated with it.
0N/A * This method may be called at any time after the handle is assigned.
0N/A */
0N/A void setObject(int handle, Object obj) {
0N/A switch (status[handle]) {
0N/A case STATUS_UNKNOWN:
0N/A case STATUS_OK:
0N/A entries[handle] = obj;
0N/A break;
0N/A
0N/A case STATUS_EXCEPTION:
0N/A break;
0N/A
0N/A default:
0N/A throw new InternalError();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Looks up and returns object associated with the given handle.
0N/A * Returns null if the given handle is NULL_HANDLE, or if it has an
0N/A * associated ClassNotFoundException.
0N/A */
0N/A Object lookupObject(int handle) {
0N/A return (handle != NULL_HANDLE &&
0N/A status[handle] != STATUS_EXCEPTION) ?
0N/A entries[handle] : null;
0N/A }
0N/A
0N/A /**
0N/A * Looks up and returns ClassNotFoundException associated with the
0N/A * given handle. Returns null if the given handle is NULL_HANDLE, or
0N/A * if there is no ClassNotFoundException associated with the handle.
0N/A */
0N/A ClassNotFoundException lookupException(int handle) {
0N/A return (handle != NULL_HANDLE &&
0N/A status[handle] == STATUS_EXCEPTION) ?
0N/A (ClassNotFoundException) entries[handle] : null;
0N/A }
0N/A
0N/A /**
0N/A * Resets table to its initial state.
0N/A */
0N/A void clear() {
0N/A Arrays.fill(status, 0, size, (byte) 0);
0N/A Arrays.fill(entries, 0, size, null);
0N/A Arrays.fill(deps, 0, size, null);
0N/A lowDep = -1;
0N/A size = 0;
0N/A }
0N/A
0N/A /**
0N/A * Returns number of handles registered in table.
0N/A */
0N/A int size() {
0N/A return size;
0N/A }
0N/A
0N/A /**
0N/A * Expands capacity of internal arrays.
0N/A */
0N/A private void grow() {
0N/A int newCapacity = (entries.length << 1) + 1;
0N/A
0N/A byte[] newStatus = new byte[newCapacity];
0N/A Object[] newEntries = new Object[newCapacity];
0N/A HandleList[] newDeps = new HandleList[newCapacity];
0N/A
0N/A System.arraycopy(status, 0, newStatus, 0, size);
0N/A System.arraycopy(entries, 0, newEntries, 0, size);
0N/A System.arraycopy(deps, 0, newDeps, 0, size);
0N/A
0N/A status = newStatus;
0N/A entries = newEntries;
0N/A deps = newDeps;
0N/A }
0N/A
0N/A /**
0N/A * Simple growable list of (integer) handles.
0N/A */
0N/A private static class HandleList {
0N/A private int[] list = new int[4];
0N/A private int size = 0;
0N/A
0N/A public HandleList() {
0N/A }
0N/A
0N/A public void add(int handle) {
0N/A if (size >= list.length) {
0N/A int[] newList = new int[list.length << 1];
0N/A System.arraycopy(list, 0, newList, 0, list.length);
0N/A list = newList;
0N/A }
0N/A list[size++] = handle;
0N/A }
0N/A
0N/A public int get(int index) {
0N/A if (index >= size) {
0N/A throw new ArrayIndexOutOfBoundsException();
0N/A }
0N/A return list[index];
0N/A }
0N/A
0N/A public int size() {
0N/A return size;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Method for cloning arrays in case of using unsharing reading
0N/A */
0N/A private static Object cloneArray(Object array) {
0N/A if (array instanceof Object[]) {
0N/A return ((Object[]) array).clone();
0N/A } else if (array instanceof boolean[]) {
0N/A return ((boolean[]) array).clone();
0N/A } else if (array instanceof byte[]) {
0N/A return ((byte[]) array).clone();
0N/A } else if (array instanceof char[]) {
0N/A return ((char[]) array).clone();
0N/A } else if (array instanceof double[]) {
0N/A return ((double[]) array).clone();
0N/A } else if (array instanceof float[]) {
0N/A return ((float[]) array).clone();
0N/A } else if (array instanceof int[]) {
0N/A return ((int[]) array).clone();
0N/A } else if (array instanceof long[]) {
0N/A return ((long[]) array).clone();
3220N/A } else if (array instanceof short[]) {
3220N/A return ((short[]) array).clone();
0N/A } else {
0N/A throw new AssertionError();
0N/A }
0N/A }
0N/A
0N/A}