java.io

Class ObjectInputStream

Implemented Interfaces:
AutoCloseable, Closeable, DataInput, ObjectInput, ObjectStreamConstants
Known Direct Subclasses:
ClassLoaderObjectInputStream, CorbaInput, RMIObjectInputStream

public class ObjectInputStream
extends InputStream
implements ObjectInput, ObjectStreamConstants

Nested Class Summary

static class
ObjectInputStream.GetField
This class allows a class to specify exactly which fields should be read, and what values should be read for these fields.

Fields inherited from interface java.io.ObjectStreamConstants

PROTOCOL_VERSION_1, PROTOCOL_VERSION_2, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, SUBCLASS_IMPLEMENTATION_PERMISSION, SUBSTITUTION_PERMISSION, TC_ARRAY, TC_BASE, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_MAX, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING, baseWireHandle

Constructor Summary

ObjectInputStream()
Protected constructor that allows subclasses to override deserialization.
ObjectInputStream(InputStream in)
Creates a new ObjectInputStream that will do all of its reading from in.

Method Summary

int
available()
This method returns the number of bytes that can be read without blocking.
void
close()
This method closes the input source
void
defaultReadObject()
Reads the current objects non-transient, non-static fields from the current class from the underlying output stream.
protected boolean
enableResolveObject(boolean enable)
If enable is true and this object is trusted, then resolveObject (Object) will be called in subsequent calls to readObject (Object).
int
read()
This method reading a byte of data from a stream.
int
read(byte[] data, int offset, int length)
This method reads raw bytes and stores them in a byte array buffer buf starting at position offset into the buffer.
boolean
readBoolean()
This method reads a Java boolean value from an input stream.
byte
readByte()
This method reads a Java byte value from an input stream.
char
readChar()
This method reads a Java char value from an input stream.
protected ObjectStreamClass
readClassDescriptor()
This method reads a class descriptor from the real input stream and use these data to create a new instance of ObjectStreamClass.
double
readDouble()
This method reads a Java double value from an input stream.
ObjectInputStream.GetField
readFields()
This method should be called by a method called 'readObject' in the deserializing class (if present).
float
readFloat()
This method reads a Java float value from an input stream.
void
readFully(data[] )
void
readFully(data[] , int offset, int size)
int
readInt()
This method reads a Java int value from an input stream It operates by reading four bytes from the stream and converting them to a single Java int.
String
readLine()
Deprecated.
long
readLong()
This method reads a Java long value from an input stream It operates by reading eight bytes from the stream and converting them to a single Java long.
Object
readObject()
Returns the next deserialized object read from the underlying stream.
protected Object
readObjectOverride()
This method allows subclasses to override the default de serialization mechanism provided by ObjectInputStream.
short
readShort()
This method reads a signed 16-bit value into a Java in from the stream.
protected void
readStreamHeader()
Reads stream magic and stream version information from the underlying stream.
String
readUTF()
This method reads a String from an input stream that is encoded in a modified UTF-8 format.
Object
readUnshared()
Returns the next deserialized object read from the underlying stream in an unshared manner.
int
readUnsignedByte()
This method reads 8 unsigned bits into a Java int value from the stream.
int
readUnsignedShort()
This method reads 16 unsigned bits into a Java int value from the stream.
void
registerValidation(ObjectInputValidation validator, int priority)
Registers a ObjectInputValidation to be carried out on the object graph currently being deserialized before it is returned to the original caller of readObject ().
protected Class
resolveClass(ObjectStreamClass osc)
Called when a class is being deserialized.
protected Object
resolveObject(Object obj)
Allows subclasses to resolve objects that are read from the stream with other objects to be returned in their place.
protected Class
resolveProxyClass(String[] intfs)
int
skipBytes(int len)
This method skips and discards the specified number of bytes in an input stream.

Methods inherited from class java.io.InputStream

available, close, mark, markSupported, read, read, read, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

ObjectInputStream

protected ObjectInputStream()
            throws IOException,
                   SecurityException
Protected constructor that allows subclasses to override deserialization. This constructor should be called by subclasses that wish to override readObject (Object). This method does a security check NOTE: currently not implemented, then sets a flag that informs readObject (Object) to call the subclasses readObjectOverride (Object) method.

ObjectInputStream

public ObjectInputStream(InputStream in)
            throws IOException,
                   StreamCorruptedException
Creates a new ObjectInputStream that will do all of its reading from in. This method also checks the stream by reading the header information (stream magic number and stream version).
Throws:
IOException - Reading stream header from underlying stream cannot be completed.
StreamCorruptedException - An invalid stream magic number or stream version was read from the stream.

Method Details

available

public int available()
            throws IOException
This method returns the number of bytes that can be read without blocking.
Specified by:
available in interface ObjectInput
Overrides:
available in interface InputStream
Returns:
The number of bytes available before blocking
Throws:
IOException - If an error occurs

close

public void close()
            throws IOException
This method closes the input source
Specified by:
close in interface ObjectInput
close in interface AutoCloseable
close in interface Closeable
Overrides:
close in interface InputStream
Throws:
IOException - If an error occurs

defaultReadObject

public void defaultReadObject()
            throws ClassNotFoundException,
                   IOException,
                   NotActiveException
Reads the current objects non-transient, non-static fields from the current class from the underlying output stream. This method is intended to be called from within a object's private void readObject (ObjectInputStream) method.
Throws:
ClassNotFoundException - The class that an object being read in belongs to cannot be found.
NotActiveException - This method was called from a context other than from the current object's and current class's private void readObject (ObjectInputStream) method.
IOException - Exception from underlying OutputStream.

enableResolveObject

protected boolean enableResolveObject(boolean enable)
            throws SecurityException
If enable is true and this object is trusted, then resolveObject (Object) will be called in subsequent calls to readObject (Object). Otherwise, resolveObject (Object) will not be called.
Throws:
SecurityException - This class is not trusted.

read

public int read()
            throws IOException
This method reading a byte of data from a stream. It returns that byte as an int. This method blocks if no data is available to be read.
Specified by:
read in interface ObjectInput
Overrides:
read in interface InputStream
Returns:
The byte of data read
Throws:
IOException - If an error occurs

read

public int read(byte[] data,
                int offset,
                int length)
            throws IOException
This method reads raw bytes and stores them in a byte array buffer buf starting at position offset into the buffer. A maximum of len bytes will be read. Note that this method blocks if no data is available, but will not necessarily block until it can read len bytes of data. That is, a "short count" is possible.
Specified by:
read in interface ObjectInput
Overrides:
read in interface InputStream
Parameters:
offset - The offset into buf to start storing data
Returns:
The actual number of bytes read or -1 if end of stream
Throws:
IOException - If an error occurs

readBoolean

public boolean readBoolean()
            throws IOException
This method reads a Java boolean value from an input stream. It does so by reading a single byte of data. If that byte is zero, then the value returned is false. If the byte is non-zero, then the value returned is true.

This method can read a boolean written by an object implementing the writeBoolean() method in the DataOutput interface.

Specified by:
readBoolean in interface DataInput
Returns:
The boolean value read
Throws:
IOException - If any other error occurs

readByte

public byte readByte()
            throws IOException
This method reads a Java byte value from an input stream. The value is in the range of -128 to 127.

This method can read a byte written by an object implementing the writeByte() method in the DataOutput interface.

Specified by:
readByte in interface DataInput
Returns:
The byte value read
Throws:
IOException - If any other error occurs

readChar

public char readChar()
            throws IOException
This method reads a Java char value from an input stream. It operates by reading two bytes from the stream and converting them to a single 16-bit Java char. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to a char in the following manner:

(char)((byte1 << 8) + byte2)

This method can read a char written by an object implementing the writeChar() method in the DataOutput interface.

Specified by:
readChar in interface DataInput
Returns:
The char value read
Throws:
IOException - If any other error occurs

readClassDescriptor

protected ObjectStreamClass readClassDescriptor()
            throws ClassNotFoundException,
                   IOException
This method reads a class descriptor from the real input stream and use these data to create a new instance of ObjectStreamClass. Fields are sorted and ordered for the real read which occurs for each instance of the described class. Be aware that if you call that method you must ensure that the stream is synchronized, in the other case it may be completely desynchronized.
Returns:
A new instance of ObjectStreamClass containing the freshly created descriptor.
Throws:
ClassNotFoundException - if the required class to build the descriptor has not been found in the system.
IOException - An input/output error occured.

readDouble

public double readDouble()
            throws IOException
This method reads a Java double value from an input stream. It operates by first reading a long value from the stream by calling the readLong() method in this interface, then converts that long to a double using the longBitsToDouble method in the class java.lang.Double.

This method can read a double written by an object implementing the writeDouble() method in the DataOutput interface.

Specified by:
readDouble in interface DataInput
Returns:
The double value read
Throws:
IOException - If any other error occurs

readFields

public ObjectInputStream.GetField readFields()
            throws IOException,
                   ClassNotFoundException,
                   NotActiveException
This method should be called by a method called 'readObject' in the deserializing class (if present). It cannot (and should not)be called outside of it. Its goal is to read all fields in the real input stream and keep them accessible through the ObjectInputStream.GetField class. Calling this method will not alter the deserializing object.
Returns:
A valid freshly created 'GetField' instance to get access to the deserialized stream.
Throws:
IOException - An input/output exception occured.
ClassNotFoundException -
NotActiveException -

readFloat

public float readFloat()
            throws IOException
This method reads a Java float value from an input stream. It operates by first reading an int value from the stream by calling the readInt() method in this interface, then converts that int to a float using the intBitsToFloat method in the class java.lang.Float.

This method can read a float written by an object implementing the writeFloat() method in the DataOutput interface.

Specified by:
readFloat in interface DataInput
Returns:
The float value read
Throws:
IOException - If any other error occurs

readFully

public void readFully(data[] )
            throws IOException

readFully

public void readFully(data[] ,
                      int offset,
                      int size)
            throws IOException

readInt

public int readInt()
            throws IOException
This method reads a Java int value from an input stream It operates by reading four bytes from the stream and converting them to a single Java int. The bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 through byte4 represent the first four bytes read from the stream, they will be transformed to an int in the following manner:

(int)(((byte1 & 0xFF) << 24) + ((byte2 & 0xFF) << 16) + ((byte3 & 0xFF)<< 8) + (byte4 & 0xFF)))

The value returned is in the range of -2147483648 to 2147483647.

This method can read an int written by an object implementing the writeInt() method in the DataOutput interface.

Specified by:
readInt in interface DataInput
Returns:
The int value read
Throws:
IOException - If any other error occurs

readLine

public String readLine()
            throws IOException

Deprecated.

Specified by:
readLine in interface DataInput
See Also:
()

readLong

public long readLong()
            throws IOException
This method reads a Java long value from an input stream It operates by reading eight bytes from the stream and converting them to a single Java long. The bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 through byte8 represent the first eight bytes read from the stream, they will be transformed to an long in the following manner:

(long)(((byte1 & 0xFF) << 56) + ((byte2 & 0xFF) << 48) + ((byte3 & 0xFF) << 40) + ((byte4 & 0xFF) << 32) + ((byte5 & 0xFF) << 24) + ((byte6 & 0xFF) << 16) + ((byte7 & 0xFF) << 8) + (byte8 & 0xFF)))

The value returned is in the range of -9223372036854775808 to 9223372036854775807.

This method can read an long written by an object implementing the writeLong() method in the DataOutput interface.

Specified by:
readLong in interface DataInput
Returns:
The long value read
Throws:
IOException - If any other error occurs

readObject

public final Object readObject()
            throws ClassNotFoundException,
                   IOException
Returns the next deserialized object read from the underlying stream. This method can be overriden by a class by implementing private void readObject (ObjectInputStream). If an exception is thrown from this method, the stream is left in an undefined state. This method can also throw Errors and RuntimeExceptions if caused by existing readResolve() user code.
Specified by:
readObject in interface ObjectInput
Returns:
The object read from the underlying stream.
Throws:
ClassNotFoundException - The class that an object being read in belongs to cannot be found.
IOException - Exception from underlying InputStream.

readObjectOverride

protected Object readObjectOverride()
            throws ClassNotFoundException,
                   IOException,
                   OptionalDataException
This method allows subclasses to override the default de serialization mechanism provided by ObjectInputStream. To make this method be used for writing objects, subclasses must invoke the 0-argument constructor on this class from their constructor.

readShort

public short readShort()
            throws IOException
This method reads a signed 16-bit value into a Java in from the stream. It operates by reading two bytes from the stream and converting them to a single 16-bit Java short. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to a short in the following manner:

(short)(((byte1 & 0xFF) << 8) + (byte2 & 0xFF))

The value returned is in the range of -32768 to 32767.

This method can read a short written by an object implementing the writeShort() method in the DataOutput interface.

Specified by:
readShort in interface DataInput
Returns:
The short value read
Throws:
IOException - If any other error occurs

readStreamHeader

protected void readStreamHeader()
            throws IOException,
                   StreamCorruptedException
Reads stream magic and stream version information from the underlying stream.
Throws:
IOException - Exception from underlying stream.
StreamCorruptedException - An invalid stream magic number or stream version was read from the stream.

readUTF

public String readUTF()
            throws IOException
This method reads a String from an input stream that is encoded in a modified UTF-8 format. This format has a leading two byte sequence that contains the remaining number of bytes to read. This two byte sequence is read using the readUnsignedShort() method of this interface. After the number of remaining bytes have been determined, these bytes are read an transformed into char values. These char values are encoded in the stream using either a one, two, or three byte format. The particular format in use can be determined by examining the first byte read.

If the first byte has a high order bit of 0, then that character consists on only one byte. This character value consists of seven bits that are at positions 0 through 6 of the byte. As an example, if byte1 is the byte read from the stream, it would be converted to a char like so:

(char)byte1

If the first byte has 110 as its high order bits, then the character consists of two bytes. The bits that make up the character value are in positions 0 through 4 of the first byte and bit positions 0 through 5 of the second byte. (The second byte should have 10 as its high order bits). These values are in most significant byte first (i.e., "big endian") order.

As an example, if byte1 and byte2 are the first two bytes read respectively, and the high order bits of them match the patterns which indicate a two byte character encoding, then they would be converted to a Java char like so:

(char)(((byte1 & 0x1F) << 6) + (byte2 & 0x3F))

If the first byte has a 1110 as its high order bits, then the character consists of three bytes. The bits that make up the character value are in positions 0 through 3 of the first byte and bit positions 0 through 5 of the other two bytes. (The second and third bytes should have 10 as their high order bits). These values are in most significant byte first (i.e., "big endian") order.

As an example, if byte1, byte2, and byte3 are the three bytes read, and the high order bits of them match the patterns which indicate a three byte character encoding, then they would be converted to a Java char like so: (char)(((byte1 & 0x0F) << 12) + ((byte2 & 0x3F) + (byte3 & 0x3F)) Note that all characters are encoded in the method that requires the fewest number of bytes with the exception of the character with the value of \<llll>u0000 which is encoded as two bytes. This is a modification of the UTF standard used to prevent C language style NUL values from appearing in the byte stream.

This method can read data that was written by an object implementing the writeUTF() method in DataOutput.

Specified by:
readUTF in interface DataInput
Returns:
The String read
Throws:
IOException - If any other error occurs

readUnshared

public Object readUnshared()
            throws IOException,
                   ClassNotFoundException
Returns the next deserialized object read from the underlying stream in an unshared manner. Any object returned by this method will not be returned by subsequent calls to either this method or readObject().

This behaviour is achieved by:

Returns:
a reference to the deserialized object.
Throws:
ClassNotFoundException - if the class of the object being deserialized can not be found.
IOException - if an I/O error occurs from the stream.
Since:
1.4
See Also:
readObject()

readUnsignedByte

public int readUnsignedByte()
            throws IOException
This method reads 8 unsigned bits into a Java int value from the stream. The value returned is in the range of 0 to 255.

This method can read an unsigned byte written by an object implementing the writeByte() method in the DataOutput interface.

Specified by:
readUnsignedByte in interface DataInput
Returns:
The unsigned bytes value read as a Java int.
Throws:
IOException - If any other error occurs

readUnsignedShort

public int readUnsignedShort()
            throws IOException
This method reads 16 unsigned bits into a Java int value from the stream. It operates by reading two bytes from the stream and converting them to a single Java int. The two bytes are stored most significant byte first (i.e., "big endian") regardless of the native host byte ordering.

As an example, if byte1 and byte2 represent the first and second byte read from the stream respectively, they will be transformed to an int in the following manner:

(int)(((byte1 0xFF) << 8) + (byte2 & 0xFF))

The value returned is in the range of 0 to 65535.

This method can read an unsigned short written by an object implementing the writeShort() method in the DataOutput interface.

Specified by:
readUnsignedShort in interface DataInput
Returns:
The unsigned short value read as a Java int.
Throws:
IOException - If any other error occurs

registerValidation

public void registerValidation(ObjectInputValidation validator,
                               int priority)
            throws InvalidObjectException,
                   NotActiveException
Registers a ObjectInputValidation to be carried out on the object graph currently being deserialized before it is returned to the original caller of readObject (). The order of validation for multiple ObjectInputValidations can be controled using priority. Validators with higher priorities are called first.
Throws:
InvalidObjectException - validator is null
NotActiveException - an attempt was made to add a validator outside of the readObject method of the object currently being deserialized

resolveClass

protected Class resolveClass(ObjectStreamClass osc)
            throws ClassNotFoundException,
                   IOException
Called when a class is being deserialized. This is a hook to allow subclasses to read in information written by the annotateClass (Class) method of an ObjectOutputStream. This implementation looks up the active call stack for a ClassLoader; if a ClassLoader is found, it is used to load the class associated with osc, otherwise, the default system ClassLoader is used.
Throws:
IOException - Exception from underlying OutputStream.

resolveObject

protected Object resolveObject(Object obj)
            throws IOException
Allows subclasses to resolve objects that are read from the stream with other objects to be returned in their place. This method is called the first time each object is encountered. This method must be enabled before it will be called in the serialization process.
Throws:
IOException - Exception from underlying OutputStream.

resolveProxyClass

protected Class resolveProxyClass(String[] intfs)
            throws IOException,
                   ClassNotFoundException

skipBytes

public int skipBytes(int len)
            throws IOException
This method skips and discards the specified number of bytes in an input stream. Note that this method may skip less than the requested number of bytes. The actual number of bytes skipped is returned. No bytes are skipped if a negative number is passed to this method.
Specified by:
skipBytes in interface DataInput
Parameters:
Returns:
The number of bytes actually skipped, which will always be numBytes
Throws:
IOException - If any other error occurs

ObjectInputStream.java -- Class used to read serialized objects Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.