Class ObjectInputStream
- All Implemented Interfaces:
Closeable,DataInput,ObjectInput,ObjectStreamConstants,AutoCloseable
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.- See Also:
ObjectOutputStream,ObjectInput,Serializable,Externalizable
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classObjectInputStream.GetFieldGetField is an inner class that provides access to the persistent fields read from the source stream. -
Field Summary
Fields inherited from interface java.io.ObjectStreamConstants
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 -
Constructor Summary
Constructors Modifier Constructor Description protectedObjectInputStream()Constructs a new ObjectInputStream.ObjectInputStream(InputStream input)Constructs a new ObjectInputStream that reads from the InputStreaminput. -
Method Summary
Modifier and Type Method Description intavailable()Returns an estimated number of bytes that can be read or skipped without blocking for more input.voidclose()Closes this stream.voiddefaultReadObject()Default method to read objects from this stream.protected booleanenableResolveObject(boolean enable)Enables object replacement for this stream.intread()Reads a single byte from the source stream and returns it as an integer in the range from 0 to 255.intread(byte[] buffer, int byteOffset, int byteCount)Reads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset.booleanreadBoolean()Reads a boolean from the source stream.bytereadByte()Reads a byte (8 bit) from the source stream.charreadChar()Reads a character (16 bit) from the source stream.protected ObjectStreamClassreadClassDescriptor()Reads a class descriptor from the source stream.doublereadDouble()Reads a double (64 bit) from the source stream.ObjectInputStream.GetFieldreadFields()Reads the persistent fields of the object that is currently being read from the source stream.floatreadFloat()Reads a float (32 bit) from the source stream.voidreadFully(byte[] dst)Reads bytes from the source stream into the byte arraydst.voidreadFully(byte[] dst, int offset, int byteCount)ReadsbyteCountbytes from the source stream into the byte arraydst.intreadInt()Reads an integer (32 bit) from the source stream.StringreadLine()Deprecated.longreadLong()Reads a long (64 bit) from the source stream.ObjectreadObject()Reads the next object from the source stream.protected ObjectreadObjectOverride()Method to be overridden by subclasses to read the next object from the source stream.shortreadShort()Reads a short (16 bit) from the source stream.protected voidreadStreamHeader()Reads and validates the ObjectInputStream header from the source stream.ObjectreadUnshared()Reads the next unshared object from the source stream.intreadUnsignedByte()Reads an unsigned byte (8 bit) from the source stream.intreadUnsignedShort()Reads an unsigned short (16 bit) from the source stream.StringreadUTF()Reads a string encoded inmodified UTF-8from the source stream.voidregisterValidation(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 descriptorosClassthat has just been read from the source stream.protected ObjectresolveObject(Object object)Allows trusted subclasses to substitute the specified originalobjectwith a new object.protected Class<?>resolveProxyClass(String[] interfaceNames)Creates the proxy class that implements the interfaces specified ininterfaceNames.intskipBytes(int length)Skipslengthbytes on the source stream.Methods inherited from class java.io.InputStream
mark, markSupported, read, reset, skipMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.io.ObjectInput
read, skip
-
Constructor Details
-
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.
-
ObjectInputStream
Constructs a new ObjectInputStream that reads from the InputStreaminput.- Parameters:
input- 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.
-
-
Method Details
-
available
Description copied from class:InputStreamReturns 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
ByteArrayOutputStreamand 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
InputStreamalways returns 0. Subclasses should override this method if they are able to indicate the number of bytes available.- Specified by:
availablein interfaceObjectInput- Overrides:
availablein classInputStream- Returns:
- the estimated number of bytes available
- Throws:
IOException- if this stream is closed or an error occurs
-
close
Closes this stream. This implementation closes the source stream.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceObjectInput- Overrides:
closein classInputStream- Throws:
IOException- if an error occurs while closing this stream.
-
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 fromreadObject().- See Also:
ObjectOutputStream.defaultWriteObject()
-
enableResolveObject
protected 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-trueto enable object replacement;falseto disable it.- Returns:
- the previous setting.
- See Also:
resolveObject(java.lang.Object),ObjectOutputStream.enableReplaceObject(boolean)
-
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.- Specified by:
readin interfaceObjectInput- Specified by:
readin classInputStream- Returns:
- 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.
-
read
Description copied from class:InputStreamReads up tobyteCountbytes from this stream and stores them in the byte arraybufferstarting atbyteOffset. Returns the number of bytes actually read or -1 if the end of the stream has been reached.- Specified by:
readin interfaceObjectInput- Overrides:
readin classInputStream- Throws:
IOException- if the stream is closed or another IOException occurs.
-
readBoolean
Reads a boolean from the source stream.- Specified by:
readBooleanin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeBoolean(boolean)
-
readByte
Reads a byte (8 bit) from the source stream.- Specified by:
readBytein interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeByte(int)
-
readChar
Reads a character (16 bit) from the source stream.- Specified by:
readCharin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeChar(int)
-
readDouble
Reads a double (64 bit) from the source stream.- Specified by:
readDoublein interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeDouble(double)
-
readFields
public ObjectInputStream.GetField readFields() throws IOException, ClassNotFoundException, NotActiveExceptionReads 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:
- 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
Reads a float (32 bit) from the source stream.- Specified by:
readFloatin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeFloat(float)
-
readFully
Reads bytes from the source stream into the byte arraydst. This method will block untildst.lengthbytes have been read.- Specified by:
readFullyin interfaceDataInput- Parameters:
dst- 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
ReadsbyteCountbytes from the source stream into the byte arraydst.- Specified by:
readFullyin interfaceDataInput- Parameters:
dst- the byte array in which to store the bytes read.offset- the initial position indstto store the bytes read from the source stream.byteCount- 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
Reads an integer (32 bit) from the source stream.- Specified by:
readIntin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeInt(int)
-
readLine
Deprecated.UseBufferedReaderinstead.Reads the next line from the source stream. Lines are terminated by'\r','\n',"\r\n"or anEOF.- Specified by:
readLinein interfaceDataInput- Returns:
- the string read from the source stream.
- Throws:
IOException- if an error occurs while reading from the source stream.
-
readLong
Reads a long (64 bit) from the source stream.- Specified by:
readLongin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeLong(long)
-
readClassDescriptor
Reads a class descriptor from the source stream.- Returns:
- 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.
-
resolveProxyClass
protected Class<?> resolveProxyClass(String[] interfaceNames) throws IOException, ClassNotFoundExceptionCreates the proxy class that implements the interfaces specified ininterfaceNames.- Parameters:
interfaceNames- the interfaces used to create the proxy class.- Returns:
- 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:
ObjectOutputStream.annotateProxyClass(Class)
-
readObject
Reads the next object from the source stream.- Specified by:
readObjectin interfaceObjectInput- Returns:
- 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:
ObjectOutputStream.writeObject(Object)
-
readObjectOverride
protected Object readObjectOverride() throws OptionalDataException, ClassNotFoundException, IOExceptionMethod to be overridden by subclasses to read the next object from the source stream.- Returns:
- 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:
ObjectOutputStream.writeObjectOverride(java.lang.Object)
-
readShort
Reads a short (16 bit) from the source stream.- Specified by:
readShortin interfaceDataInput- Returns:
- the short value read from the source stream.
- Throws:
IOException- if an error occurs while reading from the source stream.- See Also:
DataOutput.writeShort(int)
-
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.
-
readUnsignedByte
Reads an unsigned byte (8 bit) from the source stream.- Specified by:
readUnsignedBytein interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeByte(int)
-
readUnsignedShort
Reads an unsigned short (16 bit) from the source stream.- Specified by:
readUnsignedShortin interfaceDataInput- Returns:
- 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.- See Also:
DataOutput.writeShort(int)
-
readUTF
Reads a string encoded inmodified UTF-8from the source stream.- Specified by:
readUTFin interfaceDataInput- Returns:
- the string encoded in
modified UTF-8read 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.- See Also:
DataOutput.writeUTF(java.lang.String)
-
registerValidation
public void registerValidation(ObjectInputValidation object, int priority) throws NotActiveException, InvalidObjectExceptionRegisters a callback for post-deserialization validation of objects. It allows to perform additional consistency checks before thereadObject()method of this class returns its result to the caller. This method can only be called from within thereadObject()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 bypriority.- Parameters:
object- an object that can validate itself by receiving a callback.priority- the validator's priority.- Throws:
InvalidObjectException- ifobjectisnull.NotActiveException- if this stream is currently not reading objects. In that case, calling this method is not allowed.- See Also:
ObjectInputValidation.validateObject()
-
resolveClass
protected Class<?> resolveClass(ObjectStreamClass osClass) throws IOException, ClassNotFoundExceptionLoads the Java class corresponding to the class descriptorosClassthat has just been read from the source stream.- Parameters:
osClass- an ObjectStreamClass read from the source stream.- Returns:
- 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:
ObjectOutputStream.annotateClass(Class)
-
resolveObject
Allows trusted subclasses to substitute the specified originalobjectwith a new object. Object substitution has to be activated first with callingenableResolveObject(true). This implementation just returnsobject.- Parameters:
object- the original object for which a replacement may be defined.- Returns:
- the replacement object for
object. - Throws:
IOException- if any I/O error occurs while creating the replacement object.- See Also:
enableResolveObject(boolean),ObjectOutputStream.enableReplaceObject(boolean),ObjectOutputStream.replaceObject(java.lang.Object)
-
skipBytes
Skipslengthbytes 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).- Specified by:
skipBytesin interfaceDataInput- Parameters:
length- the number of bytes to skip.- Returns:
- the number of bytes actually skipped.
- Throws:
IOException- if an error occurs while skipping bytes on the source stream.NullPointerException- if the source stream isnull.
-
BufferedReaderinstead.