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:

  1. readObject(): This method reads the next object from the input stream and returns it as a generic Object. The returned object must be cast to the appropriate type by the caller.

  2. readInt(), readBoolean(), readByte(), readChar(), readFloat(), readDouble(), readLong(), readShort(): These methods read the corresponding primitive data type from the input stream.

  3. readUTF(): This method reads a string in UTF format from the input stream and returns it as a String.

  4. readFully(byte[] buf): This method reads an entire byte array from the input stream and stores it in the specified byte array buffer.

  5. skip(long n): This method skips n bytes in the input stream.

  6. available(): This method returns the number of bytes that can be read from the input stream without blocking.

  7. 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.

  8. readObjectOverride(): This method is called to read the next object from the stream. It can be overridden by subclasses to provide a custom implementation.

  9. 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.

  10. 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.