java - Kryo read and write generically -


i found 2 ways how read , write using kryo generically , thinking if 1 of them better or worse or if same.

option 1 - using kryo function writeclassandobject (readclassandobject)

public <t> void writek(t obj, string filename) {     checkdir(path);     kryo kryo = new kryo();     output output = null;     try {         output = new output(new fileoutputstream(homepath + path + "/" + "kryo_" + filename + ".bin"));         kryo.writeclassandobject(output, obj);     } catch (filenotfoundexception e) {         e.printstacktrace();     } {         output.close();     } 

}

option 2 - using writeobject (readobject) , class information

public <t> void writekryo(class<t> cl, t obj, string filename) {     checkdir(path);     kryo kryo = new kryo();     kryo.register(cl);      output output = null;     try {         output = new output(new fileoutputstream(homepath + path + "/" + "kryo_" + filename + ".bin"));         kryo.writeobject(output, obj);     } catch (filenotfoundexception e) {         e.printstacktrace();     } {         output.close();     } } 

it seem second option better because when calling function specify class java doesn't need figure out itself. not sure if that's true or not. according speed seem comparable. thanks

this depends on whether can know context instance of class serialized data represents @ time deserialize data. when using writeclassandobject, not need specify class when reading object. can deserialize data , call getclass on instance.

in contrast, when using writeobject, need know class of stored object when reading it. otherwise, cannot deserialize data information not stored in serialization data.

when (de-)serializing 1 object, not matter approach choose, given can choose both. however, imagine scenario serialize instances of same class again , again. serialization data can reduced in size when not storing class every instance. instead, example serialize class name @ beginning of file or hard-code deserializer.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -