ObjectInputStream.java revision 28
3909N/A * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 2362N/A * by Sun in the LICENSE file that accompanied this code. 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 * 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. 2362N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 2362N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * An ObjectInputStream deserializes primitive data and objects previously 0N/A * written using an ObjectOutputStream. 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 1489N/A * arguments and parameters in a remote communication system. 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 * <p>Only objects that support the java.io.Serializable or 0N/A * java.io.Externalizable interface can be read from streams. 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 * <p>Primitive data types can be read from the stream using the appropriate 0N/A * method on DataInput. 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 28N/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 * <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). 28N/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 1489N/A * the object's most specific class. 0N/A * <p>For example to read from a stream as written by the example in 0N/A * ObjectOutputStream: 848N/A * FileInputStream fis = new FileInputStream("t.tmp"); 848N/A * ObjectInputStream ois = new ObjectInputStream(fis); 0N/A * int i = ois.readInt(); 0N/A * String today = (String) ois.readObject(); 0N/A * Date date = (Date) ois.readObject(); 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 * <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 * <p>Serializable classes that require special handling during the 0N/A * serialization and deserialization process should implement the following 0N/A * private void writeObject(java.io.ObjectOutputStream stream) 0N/A * throws IOException; 848N/A * private void readObject(java.io.ObjectInputStream stream) 848N/A * throws IOException, ClassNotFoundException; 848N/A * private void readObjectNoData() 848N/A * throws ObjectStreamException; 848N/A * <p>The readObject method is responsible for reading and restoring the state 848N/A * of the object for its particular class using data written to the stream by 848N/A * the corresponding writeObject method. The method does not need to concern 848N/A * itself with the state belonging to its superclasses or subclasses. State is 848N/A * restored by reading data from the ObjectInputStream for the individual 848N/A * fields and making assignments to the appropriate fields of the object. 848N/A * Reading primitive data types is supported by DataInput. 848N/A * <p>Any attempt to read object data which exceeds the boundaries of the 848N/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. 1327N/A * <p>Primitive and object read calls issued from within a readExternal method 1327N/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 * <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 28N/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 * <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 * <p>Any exception that occurs while deserializing an object will be caught by 0N/A * the ObjectInputStream and abort the reading process. 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 * <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 * @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 /** handle value representing null */ 0N/A /** marker for unshared objects in internal handle table */ 0N/A /** table mapping primitive type names to corresponding class objects */ 0N/A /** cache of subclass security audit results */ 0N/A /** queue for WeakReferences to audited subclasses */ 0N/A /** filter stream for handling block data conversion */ 0N/A /** validation callback list */ 0N/A /** recursion depth */ 0N/A /** whether stream is closed */ 0N/A /** scratch field for passing handle values up/down call stack */ 0N/A /** flag set when at end of field value block with no TC_ENDBLOCKDATA */ 0N/A /** buffer for reading primitive field values */ 0N/A /** if true, invoke readObjectOverride() instead of readObject() */ 0N/A /** if true, invoke resolveObject() */ 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 * 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 * <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 28N/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) 848N/A * Provide a way for subclasses that are completely reimplementing 848N/A * ObjectInputStream to not have to allocate private data just used by this 848N/A * implementation of ObjectInputStream. 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 * @throws SecurityException if a security manager exists and its 0N/A * <code>checkPermission</code> method denies enabling 28N/A * @see SecurityManager#checkPermission 0N/A * @see java.io.SerializablePermission 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 * <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 1489N/A * priorities. The callbacks are registered by objects (in the readObject 0N/A * special methods) as they are individually restored. 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 * @throws ClassNotFoundException Class of a serialized object cannot be 28N/A * @throws InvalidClassException Something is wrong with a class used by 28N/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 // if nested read, passHandle contains handle of enclosing object 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 * @return the Object read from the stream. 0N/A * @throws ClassNotFoundException Class definition of a serialized object 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 * @see #ObjectInputStream() 0N/A * @see #readObject() 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 * <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 * <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 * 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 * <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 * @return reference to deserialized object 0N/A * @throws ClassNotFoundException if class of an object to deserialize 0N/A * @throws StreamCorruptedException if control information in the stream 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 // if nested read, passHandle contains handle of enclosing object 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 * @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 * 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 * Reads the persistent fields from the stream and makes them available by 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 * 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 * 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 * @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 * 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 * <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 * <p>The default implementation of this method in 0N/A * <code>ObjectInputStream</code> returns the result of calling 1489N/A * Class.forName(desc.getName(), false, loader) 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 28N/A * executing frame; otherwise, <code>loader</code> is 1489N/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 * @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 ClassNotFoundException if class of a serialized object cannot 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 848N/A * classes, allowing them to use an alternate loading mechanism for the 848N/A * interfaces and the proxy class. 848N/A * <p>This method is called exactly once for each unique proxy class 848N/A * descriptor in the stream. 848N/A * <p>The corresponding method in <code>ObjectOutputStream</code> is 848N/A * <code>annotateProxyClass</code>. For a given subclass of 848N/A * <code>ObjectInputStream</code> that overrides this method, the 848N/A * <code>annotateProxyClass</code> method in the corresponding subclass of 848N/A * <code>ObjectOutputStream</code> must write any data or objects read by 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 * Class.forName(i, false, loader) 0N/A * where <code>loader</code> is that of the first non-<code>null</code> 28N/A * class loader up the execution stack, or <code>null</code> if no 28N/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 * @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 1489N/A * named interfaces could not be found 1489N/A * @see ObjectOutputStream#annotateProxyClass(Class) 0N/A // define proxy in class loader of non-public interface(s), if any 0N/A "conflicting non-public interface class loaders");
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 * <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 * <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 * <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 * @param obj object to be substituted 0N/A * @return the substituted object 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 * <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 * @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 * 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 * @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 * 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 * @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 * Reads a byte of data. This method will block if no input is available. 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 * 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 * @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) 28N/A * Returns the number of bytes that can be read without blocking. 28N/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 * Closes the input stream. Must be called to release any resources 0N/A * associated with the stream. 0N/A * @throws IOException If an I/O error has occurred. 0N/A * Even if stream already closed, propagate redundant close to 0N/A * underlying stream to stay consistent with previous implementations. 0N/A * Reads in a boolean. 0N/A * @return the boolean read. 1489N/A * @throws EOFException If end of file is reached. 0N/A * @throws IOException If other I/O error has occurred. 0N/A * Reads an 8 bit byte. 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 * Reads an unsigned 8 bit byte. 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 * Reads a 16 bit char. 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 * Reads a 16 bit short. 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 * Reads an unsigned 16 bit short. 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 * Reads a 32 bit int. 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 * Reads a 64 bit long. 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 * Reads a 32 bit float. 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 * Reads a 64 bit double. 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 * Reads bytes, blocking until all bytes are read. 272N/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 * Reads bytes, blocking until all bytes are read. 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 * @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. 272N/A * Reads in a line that has been terminated by a \n, \r, \r\n or EOF. 272N/A * @return a String copy of the line. 272N/A * @throws IOException if there are I/O errors while reading from the 272N/A * underlying <code>InputStream</code> 272N/A * @deprecated This method does not properly convert bytes to characters. 272N/A * see DataInputStream for the details and alternatives. 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 * Provide access to the persistent fields read from the input stream. * Get the ObjectStreamClass that describes the fields in the stream. * @return the descriptor class that describes the serializable fields * Return true if the named field is defaulted and has no value in this * @param name the name of the field * @return true, if and only if the named field is defaulted * @throws IOException if there are I/O errors while reading from * the underlying <code>InputStream</code> * @throws IllegalArgumentException if <code>name</code> does not * correspond to a serializable field * Get the value of the named boolean field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>boolean</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named byte field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>byte</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named char field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>char</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named short field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>short</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named int field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>int</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named long field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>long</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named float field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>float</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named double field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>double</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Get the value of the named Object field from the persistent field. * @param name the name of the field * @param val the default value to use if <code>name</code> does not * @return the value of the named <code>Object</code> field * @throws IOException if there are I/O errors while reading from the * underlying <code>InputStream</code> * @throws IllegalArgumentException if type of <code>name</code> is * not serializable or if the field type is incorrect * Verifies that this (possibly subclass) instance can be constructed * without violating security constraints: the subclass must not override * security-sensitive non-final methods, or else the * "enableSubclassImplementation" SerializablePermission is checked. * Performs reflective checks on given subclass to verify that it doesn't * override security-sensitive non-final methods. Returns true if subclass * is "safe", false otherwise. * Clears internal data structures. * Underlying readObject implementation. * Fix for 4360508: stream is currently at the end of a field * value block written via default serialization; since there * is no terminating TC_ENDBLOCKDATA tag, simulate * end-of-custom-data behavior explicitly. "unexpected block data");
"unexpected end of block data");
* If resolveObject has been enabled and given object does not have an * exception associated with it, calls resolveObject to determine * replacement for object, and updates handle table accordingly. Returns * replacement object, or echoes provided object if no replacement * occurred. Expects that passHandle is set to given object's handle prior * to calling this method. * Reads string without allowing it to be replaced in stream. Called from * within ObjectStreamClass.read(). * Reads in null code, sets passHandle to NULL_HANDLE and returns null. * Reads in object handle, sets passHandle to the read handle, and returns * object associated with the handle. // REMIND: what type of exception to throw here? "cannot read back reference as unshared");
// REMIND: what type of exception to throw here? "cannot read back reference to unshared object");
* Reads in and returns class object. Sets passHandle to class object's * assigned handle. Returns null if class is unresolvable (in which case a * ClassNotFoundException will be associated with the class' handle in the * Reads in and returns (possibly null) class descriptor. Sets passHandle * to class descriptor's assigned handle. If class descriptor cannot be * resolved to a class in the local VM, a ClassNotFoundException is * associated with the class descriptor's handle. * Reads in and returns class descriptor for a dynamic proxy class. Sets * passHandle to proxy class descriptor's assigned handle. If proxy class * descriptor cannot be resolved to a class in the local VM, a * ClassNotFoundException is associated with the descriptor's handle. * Reads in and returns class descriptor for a class that is not a dynamic * proxy class. Sets passHandle to class descriptor's assigned handle. If * class descriptor cannot be resolved to a class in the local VM, a * ClassNotFoundException is associated with the descriptor's handle. * Reads in and returns new string. Sets passHandle to new string's * Reads in and returns array object, or null if array class is * unresolvable. Sets passHandle to array's assigned handle. for (
int i =
0; i <
len; i++) {
for (
int i =
0; i <
len; i++) {
* Reads in and returns enum constant, or null if enum type is * unresolvable. Sets passHandle to enum constant's assigned handle. "enum constant " +
name +
" does not exist in " +
* Reads and returns "ordinary" (i.e., not a String, Class, * ObjectStreamClass, array, or enum constant) object, or null if object's * class is unresolvable (in which case a ClassNotFoundException will be * associated with object's handle). Sets passHandle to object's assigned * If obj is non-null, reads externalizable data by invoking readExternal() * method of obj; otherwise, attempts to skip over externalizable data. * Expects that passHandle is set to obj's handle before this method is * In most cases, the handle table has already propagated * a CNFException to passHandle at this point; this mark * call is included to address cases where the readExternal * method has cons'ed and thrown a new CNFException of its * At this point, if the externalizable data was not written in * block-data form and either the externalizable class doesn't exist * locally (i.e., obj == null) or readExternal() just threw a * CNFException, then the stream is probably in an inconsistent state, * since some (or all) of the externalizable data may not have been * consumed. Since there's no "correct" action to take in this case, * we mimic the behavior of past serialization implementations and * blindly hope that the stream is in sync; if it isn't and additional * externalizable data remains in the stream, a subsequent read will * most likely throw a StreamCorruptedException. * Reads (or attempts to skip, if obj is null or is tagged with a * ClassNotFoundException) instance data for each serializable class of * object in stream, from superclass to subclass. Expects that passHandle * is set to obj's handle before this method is called. * In most cases, the handle table has already * propagated a CNFException to passHandle at this * point; this mark call is included to address cases * where the custom readObject method has cons'ed and * thrown a new CNFException of its own. * defaultDataEnd may have been set indirectly by custom * readObject() method when calling defaultReadObject() or * readFields(); clear it to restore normal read behavior. * Skips over all block data and objects until TC_ENDBLOCKDATA is * Reads in values of serializable fields declared by given class * descriptor. If obj is non-null, sets field values in obj. Expects that * passHandle is set to obj's handle before this method is called. // REMIND: is isInstance check necessary? * Reads in and returns IOException that caused serialization to abort. * All stream state is discarded prior to reading in fatal exception. Sets * passHandle to fatal exception's handle. * If recursion depth is 0, clears internal data structures; otherwise, * throws a StreamCorruptedException. This method is called when a * TC_RESET typecode is encountered. "unexpected reset; recursion depth: " +
depth);
* Converts specified span of bytes into float values. // REMIND: remove once hotspot inlines Float.intBitsToFloat * Converts specified span of bytes into double values. // REMIND: remove once hotspot inlines Double.longBitsToDouble * Returns the first non-null class loader (not counting class loaders of * generated reflection implementation classes) up the execution stack, or * null if only code from the null class loader is on the stack. This * method is also called via reflection by the following RMI-IIOP class: * com.sun.corba.se.internal.util.JDKClassLoader * This method should not be removed or its signature changed without * corresponding modifications to the above class. // REMIND: change name to something more accurate? * Default GetField implementation. /** class descriptor describing serializable fields */ /** primitive field values */ /** object field values */ /** object field value handles */ * Creates GetFieldImpl object for reading fields defined in given * Reads primitive and object field values from stream. * Returns offset of field with given name and type. A specified type * of null matches all types, Object.class matches all non-primitive * types, and any other non-null type matches assignable types only. * If no matching field is found in the (incoming) class * descriptor but a matching field is present in the associated local * class descriptor, returns -1. Throws IllegalArgumentException if * neither incoming nor local class descriptor contains a match. * Prioritized list of callbacks to be performed once object graph has been * completely deserialized. /** linked list of callbacks */ * Creates new (empty) ValidationList. * Registers callback. Throws InvalidObjectException if callback * Invokes all registered callbacks and clears the callback list. * Callbacks with higher priorities are called first; those with equal * priorities may be called in any order. If any of the callbacks * throws an InvalidObjectException, the callback process is terminated * and the exception propagated upwards. * Resets the callback list to its initial (empty) state. * Input stream supporting single-byte peek operations. * Creates new PeekInputStream on top of given underlying stream. * Peeks at next byte value in stream. Similar to read(), except * that it does not consume the read value. return (n >=
0) ? (n +
1) :
1;
* Input stream with two modes: in default mode, inputs data written in the * same format as DataOutputStream; in "block data" mode, inputs data * bracketed by block data markers (see object serialization specification * for details). Buffering depends on block data mode: when in default * mode, no data is buffered in advance; when in block data mode, all data * for the current data block is read in at once (and buffered). /** maximum data block length */ /** maximum data block header length */ /** (tunable) length of char buffer (for reading strings) */ /** readBlockHeader() return value indicating header read may block */ /** buffer for reading block data headers */ /** char buffer for fast string reads */ // block data state fields; values meaningful only when blkmode true /** current offset into buf */ /** end offset of valid data in buf, or -1 if no more block data */ /** number of bytes in current block yet to be read from stream */ /** underlying stream (wrapped in peekable filter stream) */ /** loopback stream (for data reads that span data blocks) */ * Creates new BlockDataInputStream on top of given underlying stream. * Block data mode is turned off by default. * Sets block data mode to the given mode (true == on, false == off) * and returns the previous mode value. If the new mode is the same as * the old mode, no action is taken. Throws IllegalStateException if * block data mode is being switched from on to off while unconsumed * block data is still present in the stream. * Returns true if the stream is currently in block data mode, false * If in block data mode, skips to the end of the current group of data * blocks (but does not unset block data mode). If not in block data * mode, throws an IllegalStateException. * Attempts to read in the next block data header (if any). If * canBlock is false and a full header cannot be read without possibly * blocking, returns HEADER_BLOCKED, else if the next element in the * stream is a block data header, returns the block data length * specified by the header, else returns -1. * Fix for 4360508: stream is currently at the end of a field * value block written via default serialization; since there * is no terminating TC_ENDBLOCKDATA tag, simulate * end-of-custom-data behavior explicitly. "illegal block data header length: " +
* TC_RESETs may occur in between data blocks. * Unfortunately, this case must be parsed at a lower * level than other typecodes, since primitive data * reads may span data blocks separated by a TC_RESET. "unexpected EOF while reading block data header");
* Refills internal buffer buf with block data. Any data in buf at the * time of the call is considered consumed. Sets the pos, end, and * unread fields to reflect the new amount of available block data; if * the next element in the stream is not a data block, sets pos and * unread to 0 and end to -1. "unexpected EOF in middle of data block");
* If in block data mode, returns the number of unconsumed bytes * remaining in the current data block. If not in block data mode, * throws an IllegalStateException. * Peeks at (but does not consume) and returns the next byte value in * the stream, or -1 if the end of the stream/block data (if in block * data mode) has been reached. * Peeks at (but does not consume) and returns the next byte value in * the stream, or throws EOFException if end of stream/block data has /* ----------------- generic input stream methods ------------------ */ * The following methods are equivalent to their counterparts in * InputStream, except that they interpret data block boundaries and * read the requested data from within data blocks when in block data return (
end >=
0) ? (
buf[
pos++] &
0xFF) : -
1;
// avoid unnecessary call to in.available() if possible * Attempts to read len bytes into byte array b at offset off. Returns * the number of bytes read, or -1 if the end of stream/block data has * been reached. If copy is true, reads values into an intermediate * buffer before copying them to b (to avoid exposing a reference to /* ----------------- primitive data input methods ------------------ */ * The following methods are equivalent to their counterparts in * DataInputStream, except that they interpret data block boundaries * and read the requested data from within data blocks when in block return din.
readLine();
// deprecated, not worth optimizing /* -------------- primitive data array input methods --------------- */ * The following methods read in spans of primitive data values. * Though equivalent to calling the corresponding primitive read * methods repeatedly, these methods are optimized for reading groups * of primitive data values more efficiently. * Reads in string written in "long" UTF format. "Long" UTF format is * identical to standard UTF, except that it uses an 8 byte header * (instead of the standard 2 bytes) to convey the UTF encoding length. * Reads in the "body" (i.e., the UTF representation minus the 2-byte * or 8-byte length header) of a UTF encoding, which occupies the next // near block boundary, read one byte at a time // shift and refill buffer manually * Reads span of UTF-encoded characters out of internal buffer * (starting at offset pos and ending at or before offset end), * consuming no more than utflen bytes. Appends read characters to * sbuf. Returns the number of bytes consumed. // stop short of last char unless all of utf bytes in buffer case 7:
// 1 byte format: 0xxxxxxx case 13:
// 2 byte format: 110xxxxx 10xxxxxx if ((
b2 &
0xC0) !=
0x80) {
case 14:
// 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx if ((
b2 &
0xC0) !=
0x80 || (
b3 &
0xC0) !=
0x80) {
default:
// 10xx xxxx, 1111 xxxx * Fix for 4450867: if a malformed utf char causes the * conversion loop to scan past the expected end of the utf * string, only consume the expected number of utf bytes. * Reads in single UTF-encoded character one byte at a time, appends * the character to sbuf, and returns the number of bytes consumed. * This method is used when reading in UTF strings written in block * data mode to handle UTF-encoded characters which (potentially) * straddle block-data boundaries. case 7:
// 1 byte format: 0xxxxxxx case 13:
// 2 byte format: 110xxxxx 10xxxxxx if ((
b2 &
0xC0) !=
0x80) {
case 14:
// 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx if ((
b2 &
0xC0) !=
0x80 || (
b3 &
0xC0) !=
0x80) {
default:
// 10xx xxxx, 1111 xxxx * Unsynchronized table which tracks wire handle to object mappings, as * well as ClassNotFoundExceptions associated with deserialized objects. * This class implements an exception-propagation algorithm for * determining which objects should have ClassNotFoundExceptions associated * with them, taking into account cycles and discontinuities (e.g., skipped * fields) in the object graph. * <p>General use of the table is as follows: during deserialization, a * given object is first assigned a handle by calling the assign method. * This method leaves the assigned handle in an "open" state, wherein * dependencies on the exception status of other handles can be registered * by calling the markDependency method, or an exception can be directly * associated with the handle by calling markException. When a handle is * tagged with an exception, the HandleTable assumes responsibility for * propagating the exception to any other objects which depend * (transitively) on the exception-tagged object. * registered, the handle should be "closed" by calling the finish method * on it. The act of finishing a handle allows the exception propagation * algorithm to aggressively prune dependency links, lessening the * <p>Note that the exception propagation algorithm used depends on handles * as memory conservation, it does not enforce this constraint. // REMIND: add full description of exception propagation algorithm? /* status codes indicating whether object has associated exception */ /** array mapping handle -> object status */ /** array mapping handle -> list of dependent handles (if any) */ /** lowest unresolved dependency */ /** number of handles in table */ * Creates handle table with the given initial capacity. * Assigns next available handle to given object, and returns assigned * handle. Once object has been completely deserialized (and all * dependencies on other objects identified), the handle should be * "closed" by passing it to finish(). * Registers a dependency (in exception status) of one handle on * another. The dependent handle must be "open" (i.e., assigned, but * not finished yet). No action is taken if either dependent or target // ignore dependencies on objs with no exception // eagerly propagate exception // add to dependency list of target // remember lowest unresolved target seen * Associates a ClassNotFoundException (if one not already associated) * with the currently active handle and propagates it to other * referencing objects as appropriate. The specified handle must be * "open" (i.e., assigned, but not finished yet). // propagate exception to dependents for (
int i =
0; i <
ndeps; i++) {
* Marks given handle as finished, meaning that no new dependencies * will be marked for handle. Calls to the assign and finish methods * must occur in LIFO order. // no pending unknowns, only resolve current handle // pending unknowns now clearable, resolve all upward handles // unresolved backrefs present, can't resolve anything yet // change STATUS_UNKNOWN -> STATUS_OK in selected span of handles * Assigns a new object to the given handle. The object previously * associated with the handle is forgotten. This method has no effect * if the given handle already has an exception associated with it. * This method may be called at any time after the handle is assigned. * Looks up and returns object associated with the given handle. * Returns null if the given handle is NULL_HANDLE, or if it has an * associated ClassNotFoundException. * Looks up and returns ClassNotFoundException associated with the * given handle. Returns null if the given handle is NULL_HANDLE, or * if there is no ClassNotFoundException associated with the handle. * Resets table to its initial state. * Returns number of handles registered in table. * Expands capacity of internal arrays. * Simple growable list of (integer) handles. private int[]
list =
new int[
4];
* Method for cloning arrays in case of using unsharing reading }
else if (
array instanceof boolean[]) {
}
else if (
array instanceof byte[]) {
}
else if (
array instanceof char[]) {
}
else if (
array instanceof double[]) {
}
else if (
array instanceof float[]) {
}
else if (
array instanceof int[]) {
}
else if (
array instanceof long[]) {
}
else if (
array instanceof double[]) {
* Context that during upcalls to class-defined readObject methods; holds * object currently being deserialized and descriptor for current class. * This context keeps a boolean state to indicate that defaultReadObject * or readFields has already been invoked with this context or the class's * readObject method has returned; if true, the getObj method throws "not in readObject invocation or fields already read");