Java ObjectInputStream

w‮.ww‬theitroad.com

The ObjectInputStream class in Java is a subclass of the InputStream class that provides the ability to read Java objects from an input stream. It can be used to read serialized objects that were previously written with an ObjectOutputStream instance.

Here are some of the commonly used methods of the ObjectInputStream class:

  1. readObject(): This method reads an 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. close(): This method closes the input stream and releases any system resources associated with it.

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.