Java.io.ObjectOutputStream.replaceObject() Method

Advertisements


Description

The java.io.ObjectOutputStream.replaceObject(Object obj) method will allow trusted subclasses of ObjectOutputStream to substitute one object for another during serialization. Replacing objects is disabled until enableReplaceObject is called. The enableReplaceObject method checks that the stream requesting to do replacement can be trusted. The first occurrence of each object written into the serialization stream is passed to replaceObject. Subsequent references to the object are replaced by the object returned by the original call to replaceObject. To ensure that the private state of objects is not unintentionally exposed, only trusted streams may use replaceObject.

The ObjectOutputStream.writeObject method takes a parameter of type Object (as opposed to type Serializable) to allow for cases where non-serializable objects are replaced by serializable ones.

When a subclass is replacing objects it must insure that either a complementary substitution must be made during deserialization or that the substituted object is compatible with every field where the reference will be stored. Objects whose type is not a subclass of the type of the field or array element abort the serialization by raising an exception and the object is not be stored.

This method is called only once when each object is first encountered. All subsequent references to the object will be redirected to the new object. This method should return the object to be substituted or the original object. Null can be returned as the object to be substituted, but may cause NullReferenceException in classes that contain references to the original object since they may be expecting an object instead of null.

Declaration

Following is the declaration for java.io.ObjectOutputStream.replaceObject() method

protected Object replaceObject(Object obj)

Parameters

  • obj -- the object to be replaced

Return Value

This method returns the alternate object that replaced the specified one

Exception

  • IOException -- Any exception thrown by the underlying OutputStream.

Example

The following example shows the usage of java.io.ObjectOutputStream.replaceObject() method.

package com.tutorialspoint;

import java.io.*;

public class ObjectOutputStreamDemo extends ObjectOutputStream {

   public ObjectOutputStreamDemo(OutputStream out) throws IOException {
      super(out);
   }

   public static void main(String[] args) {

      Object s = "Hello World!";
      Object s2 = "Bye World!";
      try {

         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStreamDemo oout = new ObjectOutputStreamDemo(out);

         // write something in the file
         oout.writeObject(s);
         oout.flush();

         // enable object replacing
         oout.enableReplaceObject(true);

         // replace object
         System.out.println("" + oout.replaceObject(s2));

         // close the stream
         oout.close();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois =
                 new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print an int
         System.out.println("" + (String) ois.readObject());

      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}

Let us compile and run the above program, this will produce the following result:

Bye World!
Hello World!



Advertisements
Advertisements