How can I update a serialized HashMap contained in a file?

Saffie :

I have a file that contains a serialized HashMap containing an element of type MyObject:

�� sr java.util.HashMap���`� F 
loadFactorI     thresholdxp?@     w      t (a54d88e06612d820bc3be72877c74f257b561b19sr  com.myproject.MyObject C�m�I�/ I  partitionL hashcodet Ljava/lang/String;L idt Ljava/lang/Long;L offsetq ~ L    timestampq ~ L topicq ~ xp    q ~ ppppx

Now, I also have some other MyObject objects that I would like to add to that map. However, I dont want to first read and deserialize the map back into memory, then update it and then write the whole updated map back to file. How would one update the serialization in the file in a more efficient way?

GhostCat salutes Monica C. :

How would one update the serialization in the file in a more efficient way?

Basically by reverse engineering the binary protocol that Java uses when serializing objects into their binary representation. That would enable you to understand which elements in that binary blob would need to be updated in which way.

Other people have already done that, see here for example.

Anything else is just work. You sitting down and writing code.

Or you write the few lines of code that read in the existing files, and write out a new file with that map plus the other object you need in there.

You see, efficiency depends on the point of view:

  • do you think the single update of a file with binary serialized objects is so time critical that it needs to be done by manually "patching" that binary file
  • do you think it is more efficient to spend hours and hours to learn the underlying binary format, to correctly update its content?

The only valid reason (I can think of) why to do that: to learn exactly such things: binary data formats, and how to patch content. But even then there might be "better" assignments that give you more insights (of real value in the real world) than ... spending your time re-implementing Java binary serialization.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324935&siteId=1