public class ObjectInputStream extends InputStream implements ObjectInput, ObjectStreamConstants
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.ObjectOutputStream,
ObjectInput,
Serializable,
Externalizable| Modifier and Type | Class and Description |
|---|---|
static class |
ObjectInputStream.GetField
GetField is an inner class that provides access to the persistent fields
read from the source stream.
|
baseWireHandle, 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| Modifier | Constructor and Description |
|---|---|
protected |
ObjectInputStream()
Constructs a new ObjectInputStream.
|
|
ObjectInputStream(InputStream input)
Constructs a new ObjectInputStream that reads from the InputStream
input. |
| Modifier and Type | Method and Description |
|---|---|
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.
|
protected boolean |
enableResolveObject(boolean enable)
Enables object replacement for this stream.
|
int |
read()
Reads a single byte from the source stream and returns it as an integer
in the range from 0 to 255.
|
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. |
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.
|
protected ObjectStreamClass |
readClassDescriptor()
Reads a class descriptor 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()
Deprecated.
Use
BufferedReader instead. |
long |
readLong()
Reads a long (64 bit) from the source stream.
|
Object |
readObject()
Reads the next object from the source stream.
|
protected Object |
readObjectOverride()
Method to be overridden by subclasses to read the next object from the
source stream.
|
short |
readShort()
Reads a short (16 bit) from the source stream.
|
protected void |
readStreamHeader()
Reads and validates the ObjectInputStream header 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.
|
String |
readUTF()
Reads a string encoded in
modified UTF-8 from the
source stream. |
void |
registerValidation(ObjectInputValidation object,
int priority)
Registers a callback for post-deserialization validation of objects.
|
protected Class<?> |
resolveClass(ObjectStreamClass osClass)
Loads the Java class corresponding to the class descriptor
osClass that has just been read from the source stream. |
protected Object |
resolveObject(Object object)
Allows trusted subclasses to substitute the specified original
object with a new object. |
protected Class<?> |
resolveProxyClass(String[] interfaceNames)
Creates the proxy class that implements the interfaces specified in
interfaceNames. |
int |
skipBytes(int length)
Skips
length bytes on the source stream. |
mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, transferToclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitread, skipprotected ObjectInputStream()
throws IOException
IOException - if an error occurs when creating this stream.public ObjectInputStream(InputStream input) throws StreamCorruptedException, IOException
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.public int available()
throws IOException
InputStreamNote 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, 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.
available in interface ObjectInputavailable in class InputStreamIOException - if this stream is closed or an error occurspublic void close()
throws IOException
close in interface Closeableclose in interface ObjectInputclose in interface AutoCloseableclose in class InputStreamIOException - if an error occurs while closing this stream.public void defaultReadObject()
throws IOException,
ClassNotFoundException,
NotActiveException
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().ObjectOutputStream.defaultWriteObject()protected boolean enableResolveObject(boolean enable)
enable - true to enable object replacement; false to
disable it.resolveObject(java.lang.Object),
ObjectOutputStream.enableReplaceObject(boolean)public int read()
throws IOException
read in interface ObjectInputread in class InputStreamIOException - if an error occurs while reading from this stream.public int read(byte[] buffer,
int byteOffset,
int byteCount)
throws IOException
InputStreambyteCount 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.read in interface ObjectInputread in class InputStreamIOException - if the stream is closed or another IOException occurs.public boolean readBoolean()
throws IOException
readBoolean in interface DataInputEOFException - 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.DataOutput.writeBoolean(boolean)public byte readByte()
throws IOException
readByte in interface DataInputEOFException - 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.DataOutput.writeByte(int)public char readChar()
throws IOException
readChar in interface DataInputEOFException - 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.DataOutput.writeChar(int)public double readDouble()
throws IOException
readDouble in interface DataInputEOFException - 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.DataOutput.writeDouble(double)public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException, NotActiveException
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.public float readFloat()
throws IOException
readFloat in interface DataInputEOFException - 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.DataOutput.writeFloat(float)public void readFully(byte[] dst)
throws IOException
dst.
This method will block until dst.length bytes have been read.readFully in interface DataInputdst - 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.public void readFully(byte[] dst,
int offset,
int byteCount)
throws IOException
byteCount bytes from the source stream into the byte array dst.readFully in interface DataInputdst - the byte array in which to store the bytes read.offset - the initial position in dst to store the bytes
read from the source stream.byteCount - the number of bytes to 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.public int readInt()
throws IOException
readInt in interface DataInputEOFException - 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.DataOutput.writeInt(int)@Deprecated public String readLine() throws IOException
BufferedReader instead.'\r', '\n', "\r\n" or an EOF.readLine in interface DataInputIOException - if an error occurs while reading from the source stream.public long readLong()
throws IOException
readLong in interface DataInputEOFException - 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.DataOutput.writeLong(long)protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException
ClassNotFoundException - if a class for one of the objects cannot be found.IOException - if an error occurs while reading from the source stream.protected Class<?> resolveProxyClass(String[] interfaceNames) throws IOException, ClassNotFoundException
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.ObjectOutputStream.annotateProxyClass(Class)public final Object readObject() throws OptionalDataException, ClassNotFoundException, IOException
readObject in interface ObjectInputClassNotFoundException - 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.ObjectOutputStream.writeObject(Object)public Object readUnshared() throws IOException, ClassNotFoundException
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.ObjectOutputStream.writeUnshared(java.lang.Object)protected Object readObjectOverride() throws OptionalDataException, ClassNotFoundException, IOException
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.ObjectOutputStream.writeObjectOverride(java.lang.Object)public short readShort()
throws IOException
readShort in interface DataInputIOException - if an error occurs while reading from the source stream.DataOutput.writeShort(int)protected void readStreamHeader()
throws IOException,
StreamCorruptedException
IOException - if an error occurs while reading from the source stream.StreamCorruptedException - if the source stream does not contain readable serialized
objects.public int readUnsignedByte()
throws IOException
readUnsignedByte in interface DataInputEOFException - 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.DataOutput.writeByte(int)public int readUnsignedShort()
throws IOException
readUnsignedShort in interface DataInputEOFException - 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.DataOutput.writeShort(int)public String readUTF() throws IOException
modified UTF-8 from the
source stream.readUTF in interface DataInputmodified 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.DataOutput.writeUTF(java.lang.String)public void registerValidation(ObjectInputValidation object, int priority) throws NotActiveException, InvalidObjectException
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.ObjectInputValidation.validateObject()protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundException
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.ObjectOutputStream.annotateClass(Class)protected Object resolveObject(Object object) throws IOException
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.enableResolveObject(boolean),
ObjectOutputStream.enableReplaceObject(boolean),
ObjectOutputStream.replaceObject(java.lang.Object)public int skipBytes(int length)
throws IOException
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).skipBytes in interface DataInputlength - 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.