Java Methods of ObjectOutputStream

www.i‮fig‬tidea.com

The ObjectOutputStream class in Java provides a number of methods for writing serialized objects to an output stream. Some of the commonly used methods of the ObjectOutputStream class are:

  1. writeObject(Object obj): This method writes an object to the output stream. The object must be serializable, otherwise a NotSerializableException will be thrown.

  2. writeInt(int val), writeBoolean(boolean val), writeByte(byte val), writeChar(char val), writeFloat(float val), writeDouble(double val), writeLong(long val), writeShort(short val): These methods write the corresponding primitive data type to the output stream.

  3. writeUTF(String str): This method writes a string in UTF format to the output stream.

  4. write(byte[] buf): This method writes an entire byte array to the output stream.

  5. defaultWriteObject(): This method writes the default serialization for an object. It is typically used when the custom writeObject method needs to write some or all of the data to the stream.

  6. writeObjectOverride(Object obj): This method is called to write the specified object to the stream. It can be overridden by subclasses to provide a custom implementation.

  7. useProtocolVersion(int version): This method sets the serialization protocol version to be used by the output stream.

  8. writeUnshared(Object obj): This method writes an object to the stream, but ensures that it is not shared with any other object previously written to the stream.

  9. reset(): This method clears the internal state of the output stream, which is useful if you want to reuse the stream to write a new set of objects.

  10. close(): This method closes the output stream and releases any system resources associated with it.

It's important to note that the ObjectOutputStream class should be used with caution, especially when writing data that will be read by an ObjectInputStream from an untrusted source, as it can be vulnerable to malicious attacks if the data is not properly validated.