Java Methods of ObjectInputStream
The ObjectInputStream
class in Java provides a number of methods for reading serialized objects from an input stream. Some of the commonly used methods of the ObjectInputStream
class are:
readObject()
: This method reads the next object from the input stream and returns it as a genericObject
. The returned object must be cast to the appropriate type by the caller.readInt()
,readBoolean()
,readByte()
,readChar()
,readFloat()
,readDouble()
,readLong()
,readShort()
: These methods read the corresponding primitive data type from the input stream.readUTF()
: This method reads a string in UTF format from the input stream and returns it as aString
.readFully(byte[] buf)
: This method reads an entire byte array from the input stream and stores it in the specified byte array buffer.skip(long n)
: This method skips n bytes in the input stream.available()
: This method returns the number of bytes that can be read from the input stream without blocking.defaultReadObject()
: This method reads the default serialization for an object. It is typically used when the custom readObject method needs to read some or all of the data from the stream.readObjectOverride()
: This method is called to read the next object from the stream. It can be overridden by subclasses to provide a custom implementation.registerValidation(ObjectInputValidation obj, int prio)
: This method registers an object for validation after it is deserialized. The priority value determines the order in which registered objects are validated.resolveClass(ObjectStreamClass desc)
: This method resolves the class of an object based on its serialization descriptor. It can be overridden by subclasses to provide a custom implementation.
It's important to note that the ObjectInputStream
class should be used with caution, especially when reading data from untrusted sources, as it can be vulnerable to malicious attacks if the data is not properly validated.