java.io
Class ObjectInputStream

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.InputStream sample code for java.io.InputStream definition code for java.io.InputStream 
      extended by java.io.ObjectInputStream
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , DataInput sample code for java.io.DataInput definition code for java.io.DataInput , ObjectInput sample code for java.io.ObjectInput definition code for java.io.ObjectInput , ObjectStreamConstants sample code for java.io.ObjectStreamConstants definition code for java.io.ObjectStreamConstants

public class ObjectInputStream
extends InputStream sample code for java.io.InputStream definition code for java.io.InputStream
implements ObjectInput sample code for java.io.ObjectInput definition code for java.io.ObjectInput , ObjectStreamConstants sample code for java.io.ObjectStreamConstants definition code for java.io.ObjectStreamConstants

An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream.

ObjectOutputStream and ObjectInputStream can provide an application with persistent storage for graphs of objects when used with a FileOutputStream and FileInputStream respectively. ObjectInputStream is used to recover those objects previously serialized. Other uses include passing objects between hosts using a socket stream or for marshaling and unmarshaling arguments and parameters in a remote communication system.

ObjectInputStream ensures that the types of all objects in the graph created from the stream match the classes present in the Java Virtual Machine. Classes are loaded as required using the standard mechanisms.

Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams.

The method readObject is used to read an object from the stream. Java's safe casting should be used to get the desired type. In Java, strings and arrays are objects and are treated as objects during serialization. When read they need to be cast to the expected type.

Primitive data types can be read from the stream using the appropriate method on DataInput.

The default deserialization mechanism for objects restores the contents of each field to the value and type it had when it was written. Fields declared as transient or static are ignored by the deserialization process. References to other objects cause those objects to be read from the stream as necessary. Graphs of objects are restored correctly using a reference sharing mechanism. New objects are always allocated when deserializing, which prevents existing objects from being overwritten.

Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL). No-arg constructors are invoked for the non-serializable classes and then the fields of the serializable classes are restored from the stream starting with the serializable class closest to java.lang.object and finishing with the object's most specific class.

For example to read from a stream as written by the example in ObjectOutputStream:

        FileInputStream fis = new FileInputStream("t.tmp");
        ObjectInputStream ois = new ObjectInputStream(fis);

        int i = ois.readInt();
        String today = (String) ois.readObject();
        Date date = (Date) ois.readObject();

        ois.close();
 

Classes control how they are serialized by implementing either the java.io.Serializable or java.io.Externalizable interfaces.

Implementing the Serializable interface allows object serialization to save and restore the entire state of the object and it allows classes to evolve between the time the stream is written and the time it is read. It automatically traverses references between objects, saving and restoring entire graphs.

Serializable classes that require special handling during the serialization and deserialization process should implement the following methods:

 private void writeObject(java.io.ObjectOutputStream stream)
     throws IOException;
 private void readObject(java.io.ObjectInputStream stream)
     throws IOException, ClassNotFoundException; 
 private void readObjectNoData() 
     throws ObjectStreamException;
 

The readObject method is responsible for reading and restoring the state of the object for its particular class using data written to the stream by the corresponding writeObject method. The method does not need to concern itself with the state belonging to its superclasses or subclasses. State is restored by reading data from the ObjectInputStream for the individual fields and making assignments to the appropriate fields of the object. Reading primitive data types is supported by DataInput.

Any attempt to read object data which exceeds the boundaries of the custom data written by the corresponding writeObject method will cause an OptionalDataException to be thrown with an eof field value of true. Non-object reads which exceed the end of the allotted data will reflect the end of data in the same way that they would indicate the end of the stream: bytewise reads will return -1 as the byte read or number of bytes read, and primitive reads will throw EOFExceptions. If there is no corresponding writeObject method, then the end of default serialized data marks the end of the allotted data.

Primitive and object read calls issued from within a readExternal method behave in the same manner--if the stream is already positioned at the end of data written by the corresponding writeExternal method, object reads will throw OptionalDataExceptions with eof set to true, bytewise reads will return -1, and primitive reads will throw EOFExceptions. Note that this behavior does not hold for streams written with the old ObjectStreamConstants.PROTOCOL_VERSION_1 protocol, in which the end of data written by writeExternal methods is not demarcated, and hence cannot be detected.

The readObjectNoData method is responsible for initializing the state of the object for its particular class in the event that the serialization stream does not list the given class as a superclass of the object being deserialized. This may occur in cases where the receiving party uses a different version of the deserialized instance's class than the sending party, and the receiver's version extends classes that are not extended by the sender's version. This may also occur if the serialization stream has been tampered; hence, readObjectNoData is useful for initializing deserialized objects properly despite a "hostile" or incomplete source stream.

Serialization does not read or assign values to the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class. It is frequently the case that the fields of that class are accessible (public, package, or protected) or that there are get and set methods that can be used to restore the state.

Any exception that occurs while deserializing an object will be caught by the ObjectInputStream and abort the reading process.

Implementing the Externalizable interface allows the object to assume complete control over the contents and format of the object's serialized form. The methods of the Externalizable interface, writeExternal and readExternal, are called to save and restore the objects state. When implemented by a class they can write and read their own state using all of the methods of ObjectOutput and ObjectInput. It is the responsibility of the objects to handle any versioning that occurs.

Enum constants are deserialized differently than ordinary serializable or externalizable objects. The serialized form of an enum constant consists solely of its name; field values of the constant are not transmitted. To deserialize an enum constant, ObjectInputStream reads the constant name from the stream; the deserialized constant is then obtained by calling the static method Enum.valueOf(Class, String) with the enum constant's base type and the received constant name as arguments. Like other serializable or externalizable objects, enum constants can function as the targets of back references appearing subsequently in the serialization stream. The process by which enum constants are deserialized cannot be customized: any class-specific readObject, readObjectNoData, and readResolve methods defined by enum types are ignored during deserialization. Similarly, any serialPersistentFields or serialVersionUID field declarations are also ignored--all enum types have a fixed serialVersionUID of 0L.

Since:
JDK1.1
See Also:
DataInput sample code for java.io.DataInput definition code for java.io.DataInput , ObjectOutputStream sample code for java.io.ObjectOutputStream definition code for java.io.ObjectOutputStream , Serializable sample code for java.io.Serializable definition code for java.io.Serializable , Object Serialization Specification, Section 3, Object Input Classes

Nested Class Summary
static class ObjectInputStream.GetField sample code for java.io.ObjectInputStream.GetField definition code for java.io.ObjectInputStream.GetField
          Provide access to the persistent fields read from the input stream.
 
Field Summary
 
Fields inherited from interface java.io.ObjectStreamConstants sample code for java.io.ObjectStreamConstants definition code for java.io.ObjectStreamConstants
baseWireHandle sample code for java.io.ObjectStreamConstants.baseWireHandle definition code for java.io.ObjectStreamConstants.baseWireHandle , PROTOCOL_VERSION_1 sample code for java.io.ObjectStreamConstants.PROTOCOL_VERSION_1 definition code for java.io.ObjectStreamConstants.PROTOCOL_VERSION_1 , PROTOCOL_VERSION_2 sample code for java.io.ObjectStreamConstants.PROTOCOL_VERSION_2 definition code for java.io.ObjectStreamConstants.PROTOCOL_VERSION_2 , SC_BLOCK_DATA sample code for java.io.ObjectStreamConstants.SC_BLOCK_DATA definition code for java.io.ObjectStreamConstants.SC_BLOCK_DATA , SC_ENUM sample code for java.io.ObjectStreamConstants.SC_ENUM definition code for java.io.ObjectStreamConstants.SC_ENUM , SC_EXTERNALIZABLE sample code for java.io.ObjectStreamConstants.SC_EXTERNALIZABLE definition code for java.io.ObjectStreamConstants.SC_EXTERNALIZABLE , SC_SERIALIZABLE sample code for java.io.ObjectStreamConstants.SC_SERIALIZABLE definition code for java.io.ObjectStreamConstants.SC_SERIALIZABLE , SC_WRITE_METHOD sample code for java.io.ObjectStreamConstants.SC_WRITE_METHOD definition code for java.io.ObjectStreamConstants.SC_WRITE_METHOD , STREAM_MAGIC sample code for java.io.ObjectStreamConstants.STREAM_MAGIC definition code for java.io.ObjectStreamConstants.STREAM_MAGIC , STREAM_VERSION sample code for java.io.ObjectStreamConstants.STREAM_VERSION definition code for java.io.ObjectStreamConstants.STREAM_VERSION , SUBCLASS_IMPLEMENTATION_PERMISSION sample code for java.io.ObjectStreamConstants.SUBCLASS_IMPLEMENTATION_PERMISSION definition code for java.io.ObjectStreamConstants.SUBCLASS_IMPLEMENTATION_PERMISSION , SUBSTITUTION_PERMISSION sample code for java.io.ObjectStreamConstants.SUBSTITUTION_PERMISSION definition code for java.io.ObjectStreamConstants.SUBSTITUTION_PERMISSION , TC_ARRAY sample code for java.io.ObjectStreamConstants.TC_ARRAY definition code for java.io.ObjectStreamConstants.TC_ARRAY , TC_BASE sample code for java.io.ObjectStreamConstants.TC_BASE definition code for java.io.ObjectStreamConstants.TC_BASE , TC_BLOCKDATA sample code for java.io.ObjectStreamConstants.TC_BLOCKDATA definition code for java.io.ObjectStreamConstants.TC_BLOCKDATA , TC_BLOCKDATALONG sample code for java.io.ObjectStreamConstants.TC_BLOCKDATALONG definition code for java.io.ObjectStreamConstants.TC_BLOCKDATALONG , TC_CLASS sample code for java.io.ObjectStreamConstants.TC_CLASS definition code for java.io.ObjectStreamConstants.TC_CLASS , TC_CLASSDESC sample code for java.io.ObjectStreamConstants.TC_CLASSDESC definition code for java.io.ObjectStreamConstants.TC_CLASSDESC , TC_ENDBLOCKDATA sample code for java.io.ObjectStreamConstants.TC_ENDBLOCKDATA definition code for java.io.ObjectStreamConstants.TC_ENDBLOCKDATA , TC_ENUM sample code for java.io.ObjectStreamConstants.TC_ENUM definition code for java.io.ObjectStreamConstants.TC_ENUM , TC_EXCEPTION sample code for java.io.ObjectStreamConstants.TC_EXCEPTION definition code for java.io.ObjectStreamConstants.TC_EXCEPTION , TC_LONGSTRING sample code for java.io.ObjectStreamConstants.TC_LONGSTRING definition code for java.io.ObjectStreamConstants.TC_LONGSTRING , TC_MAX sample code for java.io.ObjectStreamConstants.TC_MAX definition code for java.io.ObjectStreamConstants.TC_MAX , TC_NULL sample code for java.io.ObjectStreamConstants.TC_NULL definition code for java.io.ObjectStreamConstants.TC_NULL , TC_OBJECT sample code for java.io.ObjectStreamConstants.TC_OBJECT definition code for java.io.ObjectStreamConstants.TC_OBJECT , TC_PROXYCLASSDESC sample code for java.io.ObjectStreamConstants.TC_PROXYCLASSDESC definition code for java.io.ObjectStreamConstants.TC_PROXYCLASSDESC , TC_REFERENCE sample code for java.io.ObjectStreamConstants.TC_REFERENCE definition code for java.io.ObjectStreamConstants.TC_REFERENCE , TC_RESET sample code for java.io.ObjectStreamConstants.TC_RESET definition code for java.io.ObjectStreamConstants.TC_RESET , TC_STRING sample code for java.io.ObjectStreamConstants.TC_STRING definition code for java.io.ObjectStreamConstants.TC_STRING
 
Constructor Summary
protected ObjectInputStream sample code for java.io.ObjectInputStream.ObjectInputStream() definition code for java.io.ObjectInputStream.ObjectInputStream() ()
          Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream.
  ObjectInputStream sample code for java.io.ObjectInputStream.ObjectInputStream(java.io.InputStream) definition code for java.io.ObjectInputStream.ObjectInputStream(java.io.InputStream) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
          Creates an ObjectInputStream that reads from the specified InputStream.
 
Method Summary
 int available sample code for java.io.ObjectInputStream.available() definition code for java.io.ObjectInputStream.available() ()
          Returns the number of bytes that can be read without blocking.
 void close sample code for java.io.ObjectInputStream.close() definition code for java.io.ObjectInputStream.close() ()
          Closes the input stream.
 void defaultReadObject sample code for java.io.ObjectInputStream.defaultReadObject() definition code for java.io.ObjectInputStream.defaultReadObject() ()
          Read the non-static and non-transient fields of the current class from this stream.
protected  boolean enableResolveObject sample code for java.io.ObjectInputStream.enableResolveObject(boolean) definition code for java.io.ObjectInputStream.enableResolveObject(boolean) (boolean enable)
          Enable the stream to allow objects read from the stream to be replaced.
 int read sample code for java.io.ObjectInputStream.read() definition code for java.io.ObjectInputStream.read() ()
          Reads a byte of data.
 int read sample code for java.io.ObjectInputStream.read(byte[], int, int) definition code for java.io.ObjectInputStream.read(byte[], int, int) (byte[] buf, int off, int len)
          Reads into an array of bytes.
 boolean readBoolean sample code for java.io.ObjectInputStream.readBoolean() definition code for java.io.ObjectInputStream.readBoolean() ()
          Reads in a boolean.
 byte readByte sample code for java.io.ObjectInputStream.readByte() definition code for java.io.ObjectInputStream.readByte() ()
          Reads an 8 bit byte.
 char readChar sample code for java.io.ObjectInputStream.readChar() definition code for java.io.ObjectInputStream.readChar() ()
          Reads a 16 bit char.
protected  ObjectStreamClass sample code for java.io.ObjectStreamClass definition code for java.io.ObjectStreamClass readClassDescriptor sample code for java.io.ObjectInputStream.readClassDescriptor() definition code for java.io.ObjectInputStream.readClassDescriptor() ()
          Read a class descriptor from the serialization stream.
 double readDouble sample code for java.io.ObjectInputStream.readDouble() definition code for java.io.ObjectInputStream.readDouble() ()
          Reads a 64 bit double.
 ObjectInputStream.GetField sample code for java.io.ObjectInputStream.GetField definition code for java.io.ObjectInputStream.GetField readFields sample code for java.io.ObjectInputStream.readFields() definition code for java.io.ObjectInputStream.readFields() ()
          Reads the persistent fields from the stream and makes them available by name.
 float readFloat sample code for java.io.ObjectInputStream.readFloat() definition code for java.io.ObjectInputStream.readFloat() ()
          Reads a 32 bit float.
 void readFully sample code for java.io.ObjectInputStream.readFully(byte[]) definition code for java.io.ObjectInputStream.readFully(byte[]) (byte[] buf)
          Reads bytes, blocking until all bytes are read.
 void readFully sample code for java.io.ObjectInputStream.readFully(byte[], int, int) definition code for java.io.ObjectInputStream.readFully(byte[], int, int) (byte[] buf, int off, int len)
          Reads bytes, blocking until all bytes are read.
 int readInt sample code for java.io.ObjectInputStream.readInt() definition code for java.io.ObjectInputStream.readInt() ()
          Reads a 32 bit int.
 String sample code for java.lang.String definition code for java.lang.String readLine sample code for java.io.ObjectInputStream.readLine() definition code for java.io.ObjectInputStream.readLine() ()
          Deprecated. This method does not properly convert bytes to characters. see DataInputStream for the details and alternatives.
 long readLong sample code for java.io.ObjectInputStream.readLong() definition code for java.io.ObjectInputStream.readLong() ()
          Reads a 64 bit long.
 Object sample code for java.lang.Object definition code for java.lang.Object readObject sample code for java.io.ObjectInputStream.readObject() definition code for java.io.ObjectInputStream.readObject() ()
          Read an object from the ObjectInputStream.
protected  Object sample code for java.lang.Object definition code for java.lang.Object readObjectOverride sample code for java.io.ObjectInputStream.readObjectOverride() definition code for java.io.ObjectInputStream.readObjectOverride() ()
          This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor.
 short readShort sample code for java.io.ObjectInputStream.readShort() definition code for java.io.ObjectInputStream.readShort() ()
          Reads a 16 bit short.
protected  void readStreamHeader sample code for java.io.ObjectInputStream.readStreamHeader() definition code for java.io.ObjectInputStream.readStreamHeader() ()
          The readStreamHeader method is provided to allow subclasses to read and verify their own stream headers.
 Object sample code for java.lang.Object definition code for java.lang.Object readUnshared sample code for java.io.ObjectInputStream.readUnshared() definition code for java.io.ObjectInputStream.readUnshared() ()
          Reads an "unshared" object from the ObjectInputStream.
 int readUnsignedByte sample code for java.io.ObjectInputStream.readUnsignedByte() definition code for java.io.ObjectInputStream.readUnsignedByte() ()
          Reads an unsigned 8 bit byte.
 int readUnsignedShort sample code for java.io.ObjectInputStream.readUnsignedShort() definition code for java.io.ObjectInputStream.readUnsignedShort() ()
          Reads an unsigned 16 bit short.
 String sample code for java.lang.String definition code for java.lang.String readUTF sample code for java.io.ObjectInputStream.readUTF() definition code for java.io.ObjectInputStream.readUTF() ()
          Reads a String in modified UTF-8 format.
 void registerValidation sample code for java.io.ObjectInputStream.registerValidation(java.io.ObjectInputValidation, int) definition code for java.io.ObjectInputStream.registerValidation(java.io.ObjectInputValidation, int) (ObjectInputValidation sample code for java.io.ObjectInputValidation definition code for java.io.ObjectInputValidation  obj, int prio)
          Register an object to be validated before the graph is returned.
protected  Class sample code for java.lang.Class definition code for java.lang.Class <?> resolveClass sample code for java.io.ObjectInputStream.resolveClass(java.io.ObjectStreamClass) definition code for java.io.ObjectInputStream.resolveClass(java.io.ObjectStreamClass) (ObjectStreamClass sample code for java.io.ObjectStreamClass definition code for java.io.ObjectStreamClass  desc)
          Load the local class equivalent of the specified stream class description.
protected  Object sample code for java.lang.Object definition code for java.lang.Object resolveObject sample code for java.io.ObjectInputStream.resolveObject(java.lang.Object) definition code for java.io.ObjectInputStream.resolveObject(java.lang.Object) (Object sample code for java.lang.Object definition code for java.lang.Object  obj)
          This method will allow trusted subclasses of ObjectInputStream to substitute one object for another during deserialization.
protected  Class sample code for java.lang.Class definition code for java.lang.Class <?> resolveProxyClass sample code for java.io.ObjectInputStream.resolveProxyClass(java.lang.String[]) definition code for java.io.ObjectInputStream.resolveProxyClass(java.lang.String[]) (String sample code for java.lang.String definition code for java.lang.String [] interfaces)
          Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.
 int skipBytes sample code for java.io.ObjectInputStream.skipBytes(int) definition code for java.io.ObjectInputStream.skipBytes(int) (int len)
          Skips bytes, block until all bytes are skipped.
 
Methods inherited from class java.io.InputStream sample code for java.io.InputStream definition code for java.io.InputStream
mark sample code for java.io.InputStream.mark(int) definition code for java.io.InputStream.mark(int) , markSupported sample code for java.io.InputStream.markSupported() definition code for java.io.InputStream.markSupported() , read sample code for java.io.InputStream.read(byte[]) definition code for java.io.InputStream.read(byte[]) , reset sample code for java.io.InputStream.reset() definition code for java.io.InputStream.reset() , skip sample code for java.io.InputStream.skip(long) definition code for java.io.InputStream.skip(long)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 
Methods inherited from interface java.io.ObjectInput sample code for java.io.ObjectInput definition code for java.io.ObjectInput
read sample code for java.io.ObjectInput.read(byte[]) definition code for java.io.ObjectInput.read(byte[]) , skip sample code for java.io.ObjectInput.skip(long) definition code for java.io.ObjectInput.skip(long)
 

Constructor Detail

ObjectInputStream sample code for java.io.ObjectInputStream(java.io.InputStream) definition code for java.io.ObjectInputStream(java.io.InputStream)

public ObjectInputStream(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
                  throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header.

If a security manager is installed, this constructor will check for the "enableSubclassImplementation" SerializablePermission when invoked directly or indirectly by the constructor of a subclass which overrides the ObjectInputStream.readFields or ObjectInputStream.readUnshared methods.

Parameters:
in - input stream to read from
Throws:
StreamCorruptedException sample code for java.io.StreamCorruptedException definition code for java.io.StreamCorruptedException - if the stream header is incorrect
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O error occurs while reading stream header
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if untrusted subclass illegally overrides security-sensitive methods
NullPointerException sample code for java.lang.NullPointerException definition code for java.lang.NullPointerException - if in is null
See Also:
ObjectInputStream() sample code for java.io.ObjectInputStream.ObjectInputStream() definition code for java.io.ObjectInputStream.ObjectInputStream() , readFields() sample code for java.io.ObjectInputStream.readFields() definition code for java.io.ObjectInputStream.readFields() , ObjectOutputStream.ObjectOutputStream(OutputStream) sample code for java.io.ObjectOutputStream.ObjectOutputStream(java.io.OutputStream) definition code for java.io.ObjectOutputStream.ObjectOutputStream(java.io.OutputStream)

ObjectInputStream sample code for java.io.ObjectInputStream() definition code for java.io.ObjectInputStream()

protected ObjectInputStream()
                     throws IOException sample code for java.io.IOException definition code for java.io.IOException ,
                            SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException 
Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream.

If there is a security manager installed, this method first calls the security manager's checkPermission method with the SerializablePermission("enableSubclassImplementation") permission to ensure it's ok to enable subclassing.

Throws:
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if a security manager exists and its checkPermission method denies enabling subclassing.
IOException sample code for java.io.IOException definition code for java.io.IOException
See Also:
SecurityManager.checkPermission(java.security.Permission) sample code for java.lang.SecurityManager.checkPermission(java.security.Permission) definition code for java.lang.SecurityManager.checkPermission(java.security.Permission) , SerializablePermission sample code for java.io.SerializablePermission definition code for java.io.SerializablePermission
Method Detail

readObject sample code for java.io.ObjectInputStream.readObject() definition code for java.io.ObjectInputStream.readObject()

public final Object sample code for java.lang.Object definition code for java.lang.Object  readObject()
                        throws IOException sample code for java.io.IOException definition code for java.io.IOException ,
                               ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException 
Read an object from the ObjectInputStream. The class of the object, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes are read. Default deserializing for a class can be overriden using the writeObject and readObject methods. Objects referenced by this object are read transitively so that a complete equivalent graph of objects is reconstructed by readObject.

The root object is completely restored when all of its fields and the objects it references are completely restored. At this point the object validation callbacks are executed in order based on their registered priorities. The callbacks are registered by objects (in the readObject special methods) as they are individually restored.

Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

Specified by:
readObject sample code for java.io.ObjectInput.readObject() definition code for java.io.ObjectInput.readObject() in interface ObjectInput sample code for java.io.ObjectInput definition code for java.io.ObjectInput
Returns:
the object read from the stream
Throws:
ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException - Class of a serialized object cannot be found.
InvalidClassException sample code for java.io.InvalidClassException definition code for java.io.InvalidClassException - Something is wrong with a class used by serialization.
StreamCorruptedException sample code for java.io.StreamCorruptedException definition code for java.io.StreamCorruptedException - Control information in the stream is inconsistent.
OptionalDataException sample code for java.io.OptionalDataException definition code for java.io.OptionalDataException - Primitive data was found in the stream instead of objects.
IOException sample code for java.io.IOException definition code for java.io.IOException - Any of the usual Input/Output related exceptions.

readObjectOverride sample code for java.io.ObjectInputStream.readObjectOverride() definition code for java.io.ObjectInputStream.readObjectOverride()

protected Object sample code for java.lang.Object definition code for java.lang.Object  readObjectOverride()
                             throws IOException sample code for java.io.IOException definition code for java.io.IOException ,
                                    ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException 
This method is called by trusted subclasses of ObjectOutputStream that constructed ObjectOutputStream using the protected no-arg constructor. The subclass is expected to provide an override method with the modifier "final".

Returns:
the Object read from the stream.
Throws:
ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException - Class definition of a serialized object cannot be found.
OptionalDataException sample code for java.io.OptionalDataException definition code for java.io.OptionalDataException - Primitive data was found in the stream instead of objects.
IOException sample code for java.io.IOException definition code for java.io.IOException - if I/O errors occurred while reading from the underlying stream
Since:
1.2
See Also:
ObjectInputStream() sample code for java.io.ObjectInputStream.ObjectInputStream() definition code for java.io.ObjectInputStream.ObjectInputStream() , readObject() sample code for java.io.ObjectInputStream.readObject() definition code for java.io.ObjectInputStream.readObject()

readUnshared sample code for java.io.ObjectInputStream.readUnshared() definition code for java.io.ObjectInputStream.readUnshared()

public Object sample code for java.lang.Object definition code for java.lang.Object  readUnshared()
                    throws IOException sample code for java.io.IOException definition code for java.io.IOException ,
                           ClassNotFoundException sample code for java.lang.ClassNotFoundException definition code for java.lang.ClassNotFoundException 
Reads an "unshared" object from the ObjectInputStream. This method is identical to readObject, except that it prevents subsequent calls to readObject and readUnshared from returning additional references to the deserialized instance obtained via this call. Specifically: Deserializing an object via readUnshared invalidates the stream handle associated with the returned object. Note that this in itself does not always guarantee that the reference returned by readUnshared is unique; the deserialized object may define a readResolve method which returns an object visible to other parties, or readUnshared may return a Class object or enum constant obtainable elsewhere in the stream or through external means.

However, for objects which are not enum constants or instances of java.lang.Class and do not define readResolve methods, readUnshared guarantees that the returned object reference is unique and cannot be obtained a second time from the ObjectInputStream that created it, even if the underlying data stream has been manipulated. This guarantee applies only to the base-level object returned by readUnshared, and not to any transitively referenced sub-objects in the returned object graph.

ObjectInputStream subclasses which override this method can only be constructed in security contexts possessing the "enableSubclassImplementation" SerializablePermission; any attempt to instantiate such a subclass without this permission will cause a SecurityException to be thrown.

Returns:
reference to deserialized object
Throws:
ClassNotFoundException