Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary (De)Serialization of a stream of objects to 1 file

I am faced with the following problem. I need to (de)serialize (binary) a stream of objects to a single file on disk. Serialization part is not an issue, just open a stream in append mode and use .Net's BinaryFormatter Serialize method and you are done. The problem with this approach is that I can't just give this stream to BinaryFormatter's deserialize function, what it contains is not a single instance of the object I've serialized.

Does a common solution to this problem exists? All objects serialized to a given stream are of the same type, so at least we don't need to figure out what is to be deserialized, that's a given, but it doesn't seem to suggest a way out of this to me.

Clarification based on replies: The number of objects sent in is expected to be large and it is therefore infeasible to hold them all in a wrapper collection (as flushing to disk would require to load them all into memory -> add the new ones -> flush to disk).

  • Normally when you serialize a single object you get a file that contains:

[Object]

  • What I am creating is a file that contains:

[Object][Object][Object][Object]...[Object]

And I need to deserialize the individual Object instances.

Thanks in advance!

Answer: Since the answer is alluded to in this thread (with sufficient clarity), but never explicitly stated, I thought I'll state it here:

while (fileStream.Position < fileStream.Length)
    messages.Add((Message)formatter.Deserialize(fileStream));

The BinaryFormatter will deserialize one object at a time as desired :) You might want to cache the fileStream.Length property, since the length appears to be re-computed every time you call the property, slowing things down. I've got no clue why that didn't work the first time I tried it before posting this question, but it does work flawlessly now.

like image 831
United Avatar asked Nov 15 '22 09:11

United


1 Answers

Try putting your objects into a serializable collection (I believe List is serializable), then (de)serializing that object

EDIT in response to clarification: I just realized that this question has the same answer as this question. Rather than try and reinvent an answer, I'd just take a look at Mark Gravell's answer, or to this one

like image 146
Josh E Avatar answered Dec 14 '22 22:12

Josh E