views:

53

answers:

3
+2  Q: 

java serialization

Hi all, I am trying to write message to a file using serialization.Message consists of two fields- date and TibrvMsg(TibrvMsg is a propriotory message by Tibco and this class is not serializable as per their documentation).So my custom message is: Message msg = new Message(TibrvMsg msg). Problem is though i m declaring Message Serializable, i m not able to serialize it as TibrvMsg is not serializable. So i get "java.io.NotSerializableException: com.tibco.tibrv.TibrvMsg" exception. How to oevrcome this problem?

+1  A: 

You need to find a way to represent your TibrvMsg as a serializable object (maybe something like this, which transforms it into a Map).

You can then override the following two methods to write this data to the output stream (or read it):

private void writeObject(java.io.ObjectOutputStream out)
     throws IOException{
    out.writeObject(date);
    out.writeObject(doSomethingWithTibrv(tibrv);
}

 private void readObject(java.io.ObjectInputStream in)
     throws IOException, ClassNotFoundException{
     date = (Date) in.readObject();
     tibrv = readTibrv(in.readObject());
 }
Thilo
Thanks.I am doing exactly the same.But program throws an exception.One option i see is to get various fields from TibrvMsg and attach to Message and then write Message to file.
What exception?
Thilo
java.io.NotSerializableException: com.tibco.tibrv.TibrvMsgTibrvMsg contains key value pairs.I will get them and then include as a part of Message before writing it to file.
you should not be getting this exception if your writeObject works properly.
Thilo
You should always add `defaultReadObject`/`getFields` (read?) and `defaultWriteObject`/`putFields` (write?) to the start of `readObject` and `writeObject`. (And add necessary `transient` or `serialPeristentFields` (spelt correctly).)
Tom Hawtin - tackline
A: 

What do you mean by "this class is not serializable as per their documentation"? Couldent you just extend their class and implement serializable? Its just a marker interface, so...

InsertNickHere
yes.I can do that.But I was wondering why they didn't privide that.Anyway, i have found that one method getAsBytes() in TibRvMsg class that returns byte array of message and documentation says that this can be used to archive the message.
If you extend a class and implement `Serializable`, the fields of the base class are not serialisable (for good reason) and the no-arg constructor is used for initialisation instead.
Tom Hawtin - tackline
+1  A: 

Another approach is to use a serialization proxy. Serialization proxy is a different class altogether than the object being serialized with the logical state of the object. The Object readResolve() method to write a proxy instead of this object and create an object by reading proxy.

Some semi-pseudo code:

class Message implements Serializable {

  private Date dt;
  private TibrvMsg msg;

  private Object writeReplace() {
     return new Proxy(this);
  }

  private static class Proxy implements Serializable {
     private Date dt;
     private Map msgData;

     Proxy(Message msg) {
        this.dt = msg.dt;
        this.msgData = doTransform(msg.msg, "UTF-16");
     }

     private Object readResolve() {
        Message msg = new Message();
        msg.dtd = dt;
        msg.msg = asTibrvMsg(msgData);
        return msg;
     }
  }
}

Additionally override readObject(ObjectInputStream) to throw an InvalidObjectException. The serialization proxy pattern also has certain security advantages over normal serialization. It also has a few disadvantages

naikus