java.io.ObjectInputStream
An ObjectInputStream can be used to load Java objects from a stream where the
objects were saved using an ObjectOutputStream. Primitive data (ints, bytes,
chars, etc) can also be loaded if the data was saved as primitive types as
well. It is invalid to attempt to read an object as primitive data.
Nested Classes
Summary
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
Public Constructors
Protected Constructors
Public Methods
Protected Methods
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait
Details
Public Constructors
public
ObjectInputStream(InputStream input)
Constructs a new ObjectInputStream on the InputStream
input
.
All reads are now filtered through this stream.
Parameters
input
| The non-null InputStream to filter reads on. |
Throws
IOException
| If an IO exception happened when reading the stream header. |
StreamCorruptedException
| If the underlying stream does not contain serialized objects
that can be read.
|
Protected Constructors
protected
ObjectInputStream()
Constructs a new ObjectInputStream. The representation and proper
initialization is on the hands of subclasses.
Public Methods
public
int
available()
Returns the number of bytes of primitive data available from the
receiver. It should not be used at any arbitrary position; just when
reading primitive data types (ints, chars, etc).
Returns
- the number of available primitive data bytes
Throws
IOException
| If any IO problem occurred when trying to compute the bytes
available.
|
public
void
close()
Close this ObjectInputStream. This implementation closes the target
stream.
Throws
IOException
| If an error occurs attempting to close this stream.
|
public
void
defaultReadObject()
Default method to read objects from the receiver. Fields defined in the
object's class and super classes (which are Serializable) will be read.
public
int
read(byte[] buffer, int offset, int length)
Reads at most
length
bytes from the receiver and stores
them in byte array
buffer
starting at offset
offset
. Answer the number of bytes actually read or -1 if
no bytes were read and end of stream was encountered.
Parameters
buffer
| the byte array in which to store the read bytes. |
offset
| the offset in buffer to store the read bytes. |
length
| the maximum number of bytes to store in buffer . |
Returns
- The number of bytes actually read or -1 if end of stream.
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
int
read()
Reads a single byte from the receiver and returns the result as an int.
The low-order byte is returned or -1 of the end of stream was
encountered.
Returns
- The byte read or -1 if end of stream.
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
boolean
readBoolean()
Reads and returns primitive data of type boolean read from the receiver
Returns
- A boolean saved as primitive data using
ObjectOutputStream.writeBoolean()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
byte
readByte()
Reads and returns primitive data of type byte read from the receiver
Returns
- A byte saved as primitive data using
ObjectOutputStream.writeByte()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
char
readChar()
Reads and returns primitive data of type char read from the receiver
Returns
- A char saved as primitive data using
ObjectOutputStream.writeChar()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
double
readDouble()
Reads and returns primitive data of type double read from the receiver
Returns
- A double saved as primitive data using
ObjectOutputStream.writeDouble()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
Reads the fields of the object being read from the stream. The stream
will use the currently active
getField
object, allowing
users to load emulated fields, for cross-loading compatibility when a
class definition changes.
public
float
readFloat()
Reads and returns primitive data of type float read from the receiver
Returns
- A float saved as primitive data using
ObjectOutputStream.writeFloat()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
void
readFully(byte[] buffer, int offset, int length)
Reads bytes from the receiver into the byte array
buffer
.
This method will block until
length
number of bytes have
been read.
Parameters
buffer
| the byte array in which to store the read bytes. |
offset
| the offset in buffer to store the read bytes. |
length
| the maximum number of bytes to store in buffer . |
Throws
IOException
| if a problem occurs reading from this stream.
|
public
void
readFully(byte[] buffer)
Reads bytes from the receiver into the byte array
buffer
.
This method will block until
buffer.length
number of bytes
have been read.
Parameters
buffer
| the buffer to read bytes into |
Throws
IOException
| if a problem occurs reading from this stream.
|
public
int
readInt()
Reads and returns primitive data of type int read from the receiver
Returns
- an int saved as primitive data using
ObjectOutputStream.writeInt()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
String
readLine()
This method is deprecated.
Use BufferedReader
Reads and returns the next line (primitive data of type String) read from
the receiver
Returns
- a String saved as primitive data using
ObjectOutputStream.writeLine()
Throws
IOException
| If an IO exception happened when reading the primitive data. |
public
long
readLong()
Reads and returns primitive data of type long read from the receiver
Returns
- a long saved as primitive data using
ObjectOutputStream.writeLong()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
final
Object
readObject()
Read the next object from the receiver's underlying stream.
public
short
readShort()
Reads and returns primitive data of type short from the receiver
Returns
- a short saved as primitive data using
ObjectOutputStream.writeShort()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
String
readUTF()
Reads and returns primitive data of type String read in UTF format from
the receiver
Returns
- a String saved as primitive data using
ObjectOutputStream.writeUTF()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
Object
readUnshared()
Read the next unshared object from the receiver's underlying stream.
public
int
readUnsignedByte()
Reads and returns primitive data of type byte (unsigned) from the
receiver
Returns
- a byte saved as primitive data using
ObjectOutputStream.writeUnsignedByte()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
int
readUnsignedShort()
Reads and returns primitive data of type short (unsigned) from the
receiver
Returns
- a short saved as primitive data using
ObjectOutputStream.writeUnsignedShort()
Throws
IOException
| If an IO exception happened when reading the primitive data.
|
public
synchronized
void
registerValidation(ObjectInputValidation object, int priority)
Register object validator
object
to be executed to perform
validation of objects loaded from the receiver. Validations will be run
in order of decreasing priority, defined by
priority
.
Parameters
object
| An ObjectInputValidation to validate objects loaded. |
priority
| validator priority |
public
int
skipBytes(int length)
Skips
length
bytes of primitive data from the receiver. It
should not be used to skip bytes at any arbitrary position; just when
reading primitive data types (ints, chars, etc).
Parameters
length
| How many bytes to skip |
Throws
IOException
| If any IO problem occurred when trying to skip the bytes.
|
Protected Methods
protected
boolean
enableResolveObject(boolean enable)
Enables/disables object replacement for the receiver. By default this is
not enabled. Only trusted subclasses (loaded with system class loader)
can override this behavior.
Parameters
enable
| if true, enables replacement. If false, disables replacement. |
Returns
- the previous configuration (if it was enabled or disabled)
Reads a new class descriptor from the receiver. Return the class
descriptor read.
Returns
- The
ObjectStreamClass
read from the stream.
protected
Object
readObjectOverride()
Method to be overriden by subclasses to read the next object from the
receiver's underlying stream.
protected
void
readStreamHeader()
Reads and validates the ObjectInputStream header from the receiver
Throws
IOException
| If an IO exception happened when reading the stream header. |
StreamCorruptedException
| If the underlying stream does not contain serialized objects
that can be read.
|
Loads the Java class corresponding to the class descriptor
osClass
(ObjectStreamClass) just read from the receiver.
Parameters
osClass
| An ObjectStreamClass read from the receiver. |
Returns
- a Class corresponding to the descriptor loaded.
protected
Object
resolveObject(Object object)
If
enableResolveObject()
was activated, computes the
replacement object for the original object
object
and
returns the replacement. Otherwise returns
object
.
Parameters
object
| Original object for which a replacement may be defined |
Returns
- a possibly new, replacement object for
object
Throws
IOException
| If any IO problem occurred when trying to resolve the object. |
protected
Class<?>
resolveProxyClass(String[] interfaceNames)
Retrieves the proxy class corresponding to the interface names.
Parameters
interfaceNames
| The interfaces used to create the proxy class |