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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ObjectInputStream.GetField | GetField is an inner class that provides access to the persistent fields read from the source stream. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From interface
java.io.ObjectStreamConstants
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new ObjectInputStream that reads from the InputStream
input . |
Protected Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Constructs a new ObjectInputStream.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns an estimated number of bytes that can be read or skipped without blocking for more
input.
| |||||||||||
Closes this stream.
| |||||||||||
Default method to read objects from this stream.
| |||||||||||
Reads at most
length bytes from the source stream and stores them
in byte array buffer starting at offset count . | |||||||||||
Reads a single byte from the source stream and returns it as an integer
in the range from 0 to 255.
| |||||||||||
Reads a boolean from the source stream.
| |||||||||||
Reads a byte (8 bit) from the source stream.
| |||||||||||
Reads a character (16 bit) from the source stream.
| |||||||||||
Reads a double (64 bit) from the source stream.
| |||||||||||
Reads the persistent fields of the object that is currently being read
from the source stream.
| |||||||||||
Reads a float (32 bit) from the source stream.
| |||||||||||
Reads bytes from the source stream into the byte array
buffer . | |||||||||||
Reads bytes from the source stream into the byte array
buffer . | |||||||||||
Reads an integer (32 bit) from the source stream.
| |||||||||||
This method is deprecated.
Use
BufferedReader
| |||||||||||
Reads a long (64 bit) from the source stream.
| |||||||||||
Reads the next object from the source stream.
| |||||||||||
Reads a short (16 bit) from the source stream.
| |||||||||||
Reads a string encoded in
modified UTF-8 from the
source stream. | |||||||||||
Reads the next unshared object from the source stream.
| |||||||||||
Reads an unsigned byte (8 bit) from the source stream.
| |||||||||||
Reads an unsigned short (16 bit) from the source stream.
| |||||||||||
Registers a callback for post-deserialization validation of objects.
| |||||||||||
Skips
length bytes on the source stream. |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Enables object replacement for this stream.
| |||||||||||
Reads a class descriptor from the source stream.
| |||||||||||
Method to be overriden by subclasses to read the next object from the
source stream.
| |||||||||||
Reads and validates the ObjectInputStream header from the source stream.
| |||||||||||
Loads the Java class corresponding to the class descriptor
osClass that has just been read from the source stream. | |||||||||||
Allows trusted subclasses to substitute the specified original
object with a new object. | |||||||||||
Creates the proxy class that implements the interfaces specified in
interfaceNames . |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.io.InputStream
| |||||||||||
From class java.lang.Object
| |||||||||||
From interface java.io.Closeable
| |||||||||||
From interface java.io.DataInput
| |||||||||||
From interface java.io.ObjectInput
|
Constructs a new ObjectInputStream that reads from the InputStream
input
.
input | the non-null source InputStream to filter reads on. |
---|
IOException | if an error occurs while reading the stream header. |
---|---|
StreamCorruptedException | if the source stream does not contain serialized objects that can be read. |
SecurityException | if a security manager is installed and it denies subclassing this class. |
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.
IOException | if an error occurs when creating this stream. |
---|---|
SecurityException | if a security manager is installed and it denies subclassing this class. |
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.
IOException |
---|
Closes this stream. This implementation closes the source stream.
IOException | if an error occurs while closing this stream. |
---|
Default method to read objects from this stream. Serializable fields defined in the object's class and superclasses are read from the source stream.
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() . |
Reads at most length
bytes from the source stream and stores them
in byte array buffer
starting at offset count
. Blocks
until count
bytes have been read, the end of the source stream is
detected or an exception is thrown.
buffer | the array in which to store the bytes read. |
---|---|
offset | the initial position in buffer to store the bytes
read from the source stream. |
length | the maximum number of bytes to store in buffer . |
IndexOutOfBoundsException | if offset < 0 or length < 0 , or if
offset + length is greater than the length of
buffer . |
---|---|
IOException | if an error occurs while reading from this stream. |
NullPointerException | if buffer is null .
|
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.
IOException | if an error occurs while reading from this stream. |
---|
Reads a boolean from the source stream.
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. |
Reads a byte (8 bit) from the source stream.
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. |
Reads a character (16 bit) from the source stream.
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. |
Reads a double (64 bit) from the source stream.
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. |
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.
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. |
Reads a float (32 bit) from the source stream.
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. |
Reads bytes from the source stream into the byte array buffer
.
This method will block until length
number of bytes have been
read.
buffer | the byte array in which to store the bytes read. |
---|---|
offset | the initial position in buffer to store the bytes
read from the source stream. |
length | the maximum number of bytes to store in buffer . |
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. |
Reads bytes from the source stream into the byte array buffer
.
This method will block until buffer.length
bytes have been read.
buffer | the array in which to store the bytes read. |
---|
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. |
Reads an integer (32 bit) from the source stream.
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. |
This method is deprecated.
Use BufferedReader
Reads the next line from the source stream. Lines are terminated by
'\r'
, '\n'
, "\r\n"
or an EOF
.
IOException | if an error occurs while reading from the source stream. |
---|
Reads a long (64 bit) from the source stream.
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. |
Reads the next object from the source stream.
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. |
Reads a short (16 bit) from the source stream.
IOException | if an error occurs while reading from the source stream. |
---|
Reads a string encoded in modified UTF-8
from the
source stream.
modified UTF-8
read from
the source stream.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. |
Reads the next unshared object from the source stream.
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. |
Reads an unsigned byte (8 bit) from the source stream.
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. |
Reads an unsigned short (16 bit) from the source stream.
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. |
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
.
object | an object that can validate itself by receiving a callback. |
---|---|
priority | the validator's priority. |
InvalidObjectException | if object is null . |
---|---|
NotActiveException | if this stream is currently not reading objects. In that case, calling this method is not allowed. |
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).
length | the number of bytes to skip. |
---|
IOException | if an error occurs while skipping bytes on the source stream. |
---|---|
NullPointerException | if the source stream is null .
|
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.
enable | true to enable object replacement; false to
disable it. |
---|
SecurityException | if a security manager is installed and it denies enabling object replacement for this stream. |
---|
Reads a class descriptor from the source stream.
ClassNotFoundException | if a class for one of the objects cannot be found. |
---|---|
IOException | if an error occurs while reading from the source stream. |
Method to be overriden by subclasses to read the next object from the source stream.
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. |
Reads and validates the ObjectInputStream header from the source stream.
IOException | if an error occurs while reading from the source stream. |
---|---|
StreamCorruptedException | if the source stream does not contain readable serialized objects. |
Loads the Java class corresponding to the class descriptor osClass
that has just been read from the source stream.
osClass | an ObjectStreamClass read from the source stream. |
---|
osClass
.ClassNotFoundException | if the class for an object cannot be found. |
---|---|
IOException | if an I/O error occurs while creating the class. |
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
.
object | the original object for which a replacement may be defined. |
---|
object
.IOException | if any I/O error occurs while creating the replacement object. |
---|
Creates the proxy class that implements the interfaces specified in
interfaceNames
.
interfaceNames | the interfaces used to create the proxy class. |
---|
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. |