Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ObjectInputStream

public class ObjectInputStream
extends InputStream implements ObjectInput, ObjectStreamConstants

java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.ObjectInputStream


A specialized InputStream that is able to read (deserialize) Java objects as well as primitive data types (int, byte, char etc.). The data has typically been saved using an ObjectOutputStream.

See also:

Summary

Nested classes

class ObjectInputStream.GetField

GetField is an inner class that provides access to the persistent fields read from the source stream. 

Inherited constants

From interface java.io.ObjectStreamConstants

Inherited fields

From interface java.io.ObjectStreamConstants

Public constructors

ObjectInputStream(InputStream input)

Constructs a new ObjectInputStream that reads from the InputStream input.

Protected constructors

ObjectInputStream()

Constructs a new ObjectInputStream.

Public methods

int available()

Returns an estimated number of bytes that can be read or skipped without blocking for more input.

void close()

Closes this stream.

void defaultReadObject()

Default method to read objects from this stream.

int read(byte[] buffer, int byteOffset, int byteCount)

Reads up to byteCount bytes from this stream and stores them in the byte array buffer starting at byteOffset.

int read()

Reads a single byte from the source stream and returns it as an integer in the range from 0 to 255.

boolean readBoolean()

Reads a boolean from the source stream.

byte readByte()

Reads a byte (8 bit) from the source stream.

char readChar()

Reads a character (16 bit) from the source stream.

double readDouble()

Reads a double (64 bit) from the source stream.

ObjectInputStream.GetField readFields()

Reads the persistent fields of the object that is currently being read from the source stream.

float readFloat()

Reads a float (32 bit) from the source stream.

void readFully(byte[] dst)

Reads bytes from the source stream into the byte array dst.

void readFully(byte[] dst, int offset, int byteCount)

Reads byteCount bytes from the source stream into the byte array dst.

int readInt()

Reads an integer (32 bit) from the source stream.

String readLine()

This method was deprecated in API level 1. Use BufferedReader instead.

long readLong()

Reads a long (64 bit) from the source stream.

final Object readObject()

Reads the next object from the source stream.

short readShort()

Reads a short (16 bit) from the source stream.

String readUTF()

Reads a string encoded in modified UTF-8 from the source stream.

Object readUnshared()

Reads the next unshared object from the source stream.

int readUnsignedByte()

Reads an unsigned byte (8 bit) from the source stream.

int readUnsignedShort()

Reads an unsigned short (16 bit) from the source stream.

void registerValidation(ObjectInputValidation object, int priority)

Registers a callback for post-deserialization validation of objects.

int skipBytes(int length)

Skips length bytes on the source stream.

Protected methods

boolean enableResolveObject(boolean enable)

Enables object replacement for this stream.

ObjectStreamClass readClassDescriptor()

Reads a class descriptor from the source stream.

Object readObjectOverride()

Method to be overridden by subclasses to read the next object from the source stream.

void readStreamHeader()

Reads and validates the ObjectInputStream header from the source stream.

Class<?> resolveClass(ObjectStreamClass osClass)

Loads the Java class corresponding to the class descriptor osClass that has just been read from the source stream.

Object resolveObject(Object object)

Allows trusted subclasses to substitute the specified original object with a new object.

Class<?> resolveProxyClass(String[] interfaceNames)

Creates the proxy class that implements the interfaces specified in interfaceNames.

Inherited methods

From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.io.ObjectInput
From interface java.lang.AutoCloseable
From interface java.io.DataInput

Public constructors

ObjectInputStream

Added in API level 1
ObjectInputStream (InputStream input)

Constructs a new ObjectInputStream that reads from the InputStream input.

Parameters
input InputStream: the non-null source InputStream to filter reads on.
Throws
IOException if an error occurs while reading the stream header.
StreamCorruptedException if the source stream does not contain serialized objects that can be read.

Protected constructors

ObjectInputStream

Added in API level 1
ObjectInputStream ()

Constructs a new ObjectInputStream. This default constructor can be used by subclasses that do not want to use the public constructor if it allocates unneeded data.

Throws
IOException if an error occurs when creating this stream.

Public methods

available

Added in API level 1
int available ()

Returns an estimated number of bytes that can be read or skipped without blocking for more input.

Note that this method provides such a weak guarantee that it is not very useful in practice.

Firstly, the guarantee is "without blocking for more input" rather than "without blocking": a read may still block waiting for I/O to complete — the guarantee is merely that it won't have to wait indefinitely for data to be written. The result of this method should not be used as a license to do I/O on a thread that shouldn't be blocked.

Secondly, the result is a conservative estimate and may be significantly smaller than the actual number of bytes available. In particular, an implementation that always returns 0 would be correct. In general, callers should only use this method if they'd be satisfied with treating the result as a boolean yes or no answer to the question "is there definitely data ready?".

Thirdly, the fact that a given number of bytes is "available" does not guarantee that a read or skip will actually read or skip that many bytes: they may read or skip fewer.

It is particularly important to realize that you must not use this method to size a container and assume that you can read the entirety of the stream without needing to resize the container. Such callers should probably write everything they read to a ByteArrayOutputStream and convert that to a byte array. Alternatively, if you're reading from a file, length() returns the current length of the file (though assuming the file's length can't change may be incorrect, reading a file is inherently racy).

The default implementation of this method in InputStream always returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.

Returns
int the estimated number of bytes available
Throws
IOException

close

Added in API level 1
void close ()

Closes this stream. This implementation closes the source stream.

Throws
IOException if an error occurs while closing this stream.

defaultReadObject

Added in API level 1
void defaultReadObject ()

Default method to read objects from this stream. Serializable fields defined in the object's class and superclasses are read from the source stream.

Throws
ClassNotFoundException if the object's class cannot be found.
IOException if an I/O error occurs while reading the object data.
NotActiveException if this method is not called from readObject().

See also:

read

Added in API level 1
int read (byte[] buffer, 
                int byteOffset, 
                int byteCount)

Reads up to byteCount bytes from this stream and stores them in the byte array buffer starting at byteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.

Parameters
buffer byte
byteOffset int
byteCount int
Returns
int
Throws
IOException

read

Added in API level 1
int read ()

Reads a single byte from the source stream and returns it as an integer in the range from 0 to 255. Returns -1 if the end of the source stream has been reached. Blocks if no input is available.

Returns
int the byte read or -1 if the end of the source stream has been reached.
Throws
IOException if an error occurs while reading from this stream.

readBoolean

Added in API level 1
boolean readBoolean ()

Reads a boolean from the source stream.

Returns
boolean the boolean value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readByte

Added in API level 1
byte readByte ()

Reads a byte (8 bit) from the source stream.

Returns
byte the byte value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readChar

Added in API level 1
char readChar ()

Reads a character (16 bit) from the source stream.

Returns
char the char value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readDouble

Added in API level 1
double readDouble ()

Reads a double (64 bit) from the source stream.

Returns
double the double value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readFields

Added in API level 1
ObjectInputStream.GetField readFields ()

Reads the persistent fields of the object that is currently being read from the source stream. The values read are stored in a GetField object that provides access to the persistent fields. This GetField object is then returned.

Returns
ObjectInputStream.GetField the GetField object from which persistent fields can be accessed by name.
Throws
ClassNotFoundException if the class of an object being deserialized can not be found.
IOException if an error occurs while reading from this stream.
NotActiveException if this stream is currently not reading an object.

readFloat

Added in API level 1
float readFloat ()

Reads a float (32 bit) from the source stream.

Returns
float the float value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readFully

Added in API level 1
void readFully (byte[] dst)

Reads bytes from the source stream into the byte array dst. This method will block until dst.length bytes have been read.

Parameters
dst byte: the array in which to store the bytes read.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readFully

Added in API level 1
void readFully (byte[] dst, 
                int offset, 
                int byteCount)

Reads byteCount bytes from the source stream into the byte array dst.

Parameters
dst byte: the byte array in which to store the bytes read.
offset int: the initial position in dst to store the bytes read from the source stream.
byteCount int: the number of bytes to read.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readInt

Added in API level 1
int readInt ()

Reads an integer (32 bit) from the source stream.

Returns
int the integer value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readLine

Added in API level 1
String readLine ()

This method was deprecated in API level 1.
Use BufferedReader instead.

Reads the next line from the source stream. Lines are terminated by '\r', '\n', "\r\n" or an EOF.

Returns
String the string read from the source stream.
Throws
IOException if an error occurs while reading from the source stream.

readLong

Added in API level 1
long readLong ()

Reads a long (64 bit) from the source stream.

Returns
long the long value read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readObject

Added in API level 1
Object readObject ()

Reads the next object from the source stream.

Returns
Object the object read from the source stream.
Throws
ClassNotFoundException if the class of one of the objects in the object graph cannot be found.
IOException if an error occurs while reading from the source stream.
OptionalDataException if primitive data types were found instead of an object.

See also:

readShort

Added in API level 1
short readShort ()

Reads a short (16 bit) from the source stream.

Returns
short the short value read from the source stream.
Throws
IOException if an error occurs while reading from the source stream.

readUTF

Added in API level 1
String readUTF ()

Reads a string encoded in modified UTF-8 from the source stream.

Returns
String the string encoded in modified UTF-8 read from the source stream.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readUnshared

Added in API level 1
Object readUnshared ()

Reads the next unshared object from the source stream.

Returns
Object the new object read.
Throws
ClassNotFoundException if the class of one of the objects in the object graph cannot be found.
IOException if an error occurs while reading from the source stream.

See also:

readUnsignedByte

Added in API level 1
int readUnsignedByte ()

Reads an unsigned byte (8 bit) from the source stream.

Returns
int the unsigned byte value read from the source stream packaged in an integer.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

readUnsignedShort

Added in API level 1
int readUnsignedShort ()

Reads an unsigned short (16 bit) from the source stream.

Returns
int the unsigned short value read from the source stream packaged in an integer.
Throws
EOFException if the end of the input is reached before the read request can be satisfied.
IOException if an error occurs while reading from the source stream.

registerValidation

Added in API level 1
void registerValidation (ObjectInputValidation object, 
                int priority)

Registers a callback for post-deserialization validation of objects. It allows to perform additional consistency checks before the readObject() method of this class returns its result to the caller. This method can only be called from within the readObject() method of a class that implements "special" deserialization rules. It can be called multiple times. Validation callbacks are then done in order of decreasing priority, defined by priority.

Parameters
object ObjectInputValidation: an object that can validate itself by receiving a callback.
priority int: the validator's priority.
Throws
InvalidObjectException if object is null.
NotActiveException if this stream is currently not reading objects. In that case, calling this method is not allowed.

See also:

skipBytes

Added in API level 1
int skipBytes (int length)

Skips length bytes on the source stream. This method should not be used to skip bytes at any arbitrary position, just when reading primitive data types (int, char etc).

Parameters
length int: the number of bytes to skip.
Returns
int the number of bytes actually skipped.
Throws
IOException if an error occurs while skipping bytes on the source stream.
NullPointerException if the source stream is null.

Protected methods

enableResolveObject

Added in API level 1
boolean enableResolveObject (boolean enable)

Enables object replacement for this stream. By default this is not enabled. Only trusted subclasses (loaded with system class loader) are allowed to change this status.

Parameters
enable boolean: true to enable object replacement; false to disable it.
Returns
boolean the previous setting.

See also:

readClassDescriptor

Added in API level 1
ObjectStreamClass readClassDescriptor ()

Reads a class descriptor from the source stream.

Returns
ObjectStreamClass the class descriptor read from the source stream.
Throws
ClassNotFoundException if a class for one of the objects cannot be found.
IOException if an error occurs while reading from the source stream.

readObjectOverride

Added in API level 1
Object readObjectOverride ()

Method to be overridden by subclasses to read the next object from the source stream.

Returns
Object the object read from the source stream.
Throws
ClassNotFoundException if the class of one of the objects in the object graph cannot be found.
IOException if an error occurs while reading from the source stream.
OptionalDataException if primitive data types were found instead of an object.

See also:

readStreamHeader

Added in API level 1
void readStreamHeader ()

Reads and validates the ObjectInputStream header from the source stream.

Throws
IOException if an error occurs while reading from the source stream.
StreamCorruptedException if the source stream does not contain readable serialized objects.

resolveClass

Added in API level 1
Class<?> resolveClass (ObjectStreamClass osClass)

Loads the Java class corresponding to the class descriptor osClass that has just been read from the source stream.

Parameters
osClass ObjectStreamClass: an ObjectStreamClass read from the source stream.
Returns
Class<?> a Class corresponding to the descriptor osClass.
Throws
ClassNotFoundException if the class for an object cannot be found.
IOException if an I/O error occurs while creating the class.

See also:

resolveObject

Added in API level 1
Object resolveObject (Object object)

Allows trusted subclasses to substitute the specified original object with a new object. Object substitution has to be activated first with calling enableResolveObject(true). This implementation just returns object.

Parameters
object Object: the original object for which a replacement may be defined.
Returns
Object the replacement object for object.
Throws
IOException if any I/O error occurs while creating the replacement object.

See also:

resolveProxyClass

Added in API level 1
Class<?> resolveProxyClass (String[] interfaceNames)

Creates the proxy class that implements the interfaces specified in interfaceNames.

Parameters
interfaceNames String: the interfaces used to create the proxy class.
Returns
Class<?> the proxy class.
Throws
ClassNotFoundException if the proxy class or any of the specified interfaces cannot be created.
IOException if an error occurs while reading from the source stream.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.