Is there a way to include the class code in a seri...
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nuno_Santos
Posted On:   Tuesday, January 9, 2001 05:11 PM

Is there a way to include the class code in a serialized object so that it can be deserialized without having to
have its class available?

Re: Is there a way to include the class code in a ...

Posted By:   Anonymous  
Posted On:   Friday, March 16, 2001 07:00 AM

Definitely exists.
You need to make your subclasses of ObjectXXXStream's, say MyObjectInputStream and MyObjectOutputStream so that
  • MyObjectOutputStream overrides a method
    annotateClass(Class), here it writes your class data to itself using DataOutput's methods;

  • MyObjectInputStream overrides a method
    resolveClass(ObjectStreamClass), here it reads your class data from itself using methods of DataInput,
    and resolves the class using methods of its own classloader.


  • This assumes that you have MyObjectOutputStream at one side, and MyObjectInputStream at another.


    samokhodkin[at]chat.ru

    Re: Is there a way to include the class code in a ...

    Posted By:   Luigi_Viggiano  
    Posted On:   Thursday, February 22, 2001 07:15 AM

    You can serialize the class, check the example on serializing and deserializing a Class object and instantiating after deserialized.


    package test;
    import java.io.*;

    public class Test {

    public static void main(String[] args) throws Exception {
    Class c = Class.forName("test.Test");
    ByteArrayOutputStream baos = new ByteArrayOutputStream(10);
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(c);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ObjectInputStream ois = new ObjectInputStream(bais);
    Class serializedClass = (Class)ois.readObject();
    Object o = serializedClass.newInstance();
    System.out.println(o);
    }

    public String toString() {
    return "Hello! I've been serialized! : " + super.toString();
    }
    }

    Best regards,

    Luigi

    About | Sitemap | Contact