serialization - How do I serialize and deserialize in Java when I have a black box to convert to/from a byte array? -


i have class following:

public class foo {   static byte[] converttoarray(foo f);   static foo converttofoo(byte[] ba); } 

how can use converttoarray , converttofoo allow foo implement java.io.serializable? default serialization procedure doesn't seem solution class because require changing lot of other classes implement serializable. however, have way go , bytes, there should easy way foo implement serializable without having declare dependent members serializable. suspect overriding readobject , writeobject way go, correct way this, since these instance methods return type void?

check out externalizable, subinterface of serializable. readexternal , writeexternal methods delegate serialization details programmer, sounds appropriate in case.

during deserialization (in implementation of readexternal), need use foo.converttofoo convert bytes objectinput foo object, , copy of state of foo object this.

a snippet javadoc describes semantics of externalizable:

only identity of class of externalizable instance written in serialization stream , responsibility of class save , restore contents of instances. writeexternal , readexternal methods of externalizable interface implemented class give class complete control on format , contents of stream object , supertypes. these methods must explicitly coordinate supertype save state. these methods supersede customized implementations of writeobject , readobject methods.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -