Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - ObjectOutputStream annotateClass(Class<?> cl) method



Description

The Java ObjectOutputStream annotateClass(Class<?> cl) can be implemented by subclasses to allow class data to be stored in the stream. By default this method does nothing. The corresponding method in ObjectInputStream is resolveClass. This method is called exactly once for each unique class in the stream. The class name and signature will have already been written to the stream. This method may make free use of the ObjectOutputStream to save any representation of the class it deems suitable (for example, the bytes of the class file). The resolveClass method in the corresponding subclass of ObjectInputStream must read and use any data or objects written by annotateClass.

Important Notes

  • You don't call annotateClass() directly.

  • You override it in a custom ObjectOutputStream subclass.

  • It's rarely needed in typical Java apps but can be useful in advanced use cases.

Declaration

Following is the declaration for java.io.ObjectOutputStream.annotateClass(Class<?> cl) method.

protected void annotateClass(Class<?> cl)

Parameters

cl − The class to annotate custom data for.

Return Value

This method does not return a value.

Exception

NA

Example - Usage of ObjectOutputStream annotateClass(Class<?> cl) method

The following example shows the usage of ObjectOutputStream annotateClass(Class<?> cl) method.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

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 annotateClass but it does nothing
         oout.annotateClass(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();
      }
   }
}

Output

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

319874
1653984

Example - Logging Class Names During Serialization

The following example shows the usage of ObjectOutputStream annotateClass(Class<?> cl) method. We're printing the class name being serialized.

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      try (FileOutputStream fos = new FileOutputStream("test1.ser");
         ObjectOutputStream oos = new CustomObjectOutputStream(fos)) {
         oos.writeObject(new Person("Alice", 30));
      }
   }

   static class CustomObjectOutputStream extends ObjectOutputStream {
      public CustomObjectOutputStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void annotateClass(Class<?> cl) throws IOException {
         System.out.println("Serializing class: " + cl.getName());
      }
   }

   static class Person implements Serializable {
      private static final long serialVersionUID = 1L;
      String name;
      int age;

      Person(String name, int age) {
         this.name = name;
         this.age = age;
      }
   }
}

Output

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

Serializing class: com.tutorialspoint.ObjectOutputStreamDemo$Person

Explanation

  • Overrides annotateClass() to print the class name when it's being serialized.

Example - Writing Custom Annotation Info to the Stream

The following example shows the usage of ObjectOutputStream annotateClass(Class<?> cl) method. We'll write extra info to the stream (e.g. version info).

ObjectOutputStreamDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class ObjectOutputStreamDemo {
   public static void main(String[] args) throws Exception {
      try (FileOutputStream fos = new FileOutputStream("test2.ser");
         ObjectOutputStream oos = new CustomAnnotatingStream(fos)) {
         oos.writeObject(new Data("Sample"));
      }
   }

   static class CustomAnnotatingStream extends ObjectOutputStream {
      public CustomAnnotatingStream(OutputStream out) throws IOException {
         super(out);
      }

      @Override
      protected void annotateClass(Class<?> cl) throws IOException {
         System.out.println(" Inside annotateClass ");
         // Add custom data (could be version info or other metadata)
         writeUTF("CustomVersion:1.0");
      }
   }

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      String info;

      Data(String info) {
         this.info = info;
      }
   }
}

Output

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

Inside annotateClass

Explanation

  • When Data class is serialized, "CustomVersion:1.0" is written alongside the class descriptor.

  • This could be used by a custom ObjectInputStream that overrides resolveClass() to interpret it.

java_io_objectoutputstream.htm
Advertisements