开发者

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.


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


A file is a serialization, so I would think sending your stream directly to a file would do what you seem to want. A more specific explanation of your situation would help, to provide a more useful answer. (I wish I could enter this as a 'comment', but somehow the comment button is not available to me.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜