Java.io.ObjectInputStream.resolveObject() Method



Description

The java.io.ObjectInputStream.resolveProxyClass(String[] interfaces) method Returns a proxy class that implements the interfaces named in a proxy class descriptor; subclasses may implement this method to read custom data from the stream along with the descriptors for dynamic proxy classes, allowing them to use an alternate loading mechanism for the interfaces and the proxy class.

Declaration

Following is the declaration for java.io.ObjectInputStream.resolveObject() method.

protected Class<?> resolveProxyClass(String[] interfaces)

Parameters

interfaces − The list of interface names that were deserialized in the proxy class descriptor

Return Value

This method returns a proxy class for the specified interfaces.

Exception

  • IOException − Any exception thrown by the underlying InputStream.

  • ClassNotFoundException − If the proxy class or any of the named interfaces could not be found.

Example

The following example shows the usage of java.io.ObjectInputStream.resolveProxyClass() method.

package com.tutorialspoint;

import java.io.*;

public class ObjectInputStreamDemo extends ObjectInputStream {

   public ObjectInputStreamDemo(InputStream in) throws IOException {
      super(in);
   }

   public static void main(String[] args) {
      String s = "Hello World";
      
      try {
         // create a new file with an ObjectOutputStream
         FileOutputStream out = new FileOutputStream("test.txt");
         ObjectOutputStream oout = new ObjectOutputStream(out);

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

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

         // create a list that will be used to resolve proxy class
         String[] list = {Serializable.class.getName()};

         // print the class proxy
         System.out.println("" + ois.resolveProxyClass(list));
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

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

class com.sun.proxy.$Proxy0
java_io_objectinputstream.htm
Advertisements