Java.io.ObjectOutputStream.annotateProxyClass() Method

Advertisements


Description

The java.io.ObjectOutputStream.annotateProxyClass(Class<?> cl) can be implemented by subclasses to store custom data in the stream along with descriptors for dynamic proxy classes. This method is called exactly once for each unique proxy class descriptor in the stream. The default implementation of this method in ObjectOutputStream does nothing. The corresponding method in ObjectInputStream is resolveProxyClass. For a given subclass of ObjectOutputStream that overrides this method, the resolveProxyClass method in the corresponding subclass of ObjectInputStream must read any data or objects written by annotateProxyClass.

Declaration

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

protected void annotateProxyClass(Class<?> cl)

Parameters

  • cl -- the proxy class to annotate custom data for

Return Value

This method does not return a value

Exception

  • IOException -- any exception thrown by the underlying OutputStream

Example

The following example shows the usage of java.io.ObjectOutputStream.annotateProxyClass() 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) {

      int i = 319874;
      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.writeInt(i);
         oout.writeInt(1653984);
         oout.flush();

         // call annotateProxyClass but it does nothing
         oout.annotateProxyClass(Integer.class);

         // 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("" + ois.readInt());

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

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

   }
}

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

319874
1653984



Advertisements
Advertisements