How to serialize a stream

B

Brad

I have tried both the binary and the XML serialization; both give the
same results.

I have an object with a MemoryStream member. When I serialize and
deserialize the object, the MemoryStream member is created, but it's
actual stream blank. All primitive typed members of my object
deserialize fine.
Can anyone give me a clue about what I need to do to make this happen /
why it's possible / not possible?
 
N

Nick Malik

A memory stream is not a string of bytes. Granted, you can create a memory
stream from a stream of bytes, and in fact, you should be able to read the
string of bytes out of the memory stream and serialize that instead.
However, serializing a stream will not call out all of the data in it.

You may want to make a wrapper object that encapsulates the object in
question. When your wrapper is serialized, it will provide the byte array
for serialization. Deserialization will recreate the memory stream. I
haven't tried this, but, in theory, this may solve the problem.

Sorry I couldn't help more.
--- Nick
 
J

Joerg Jooss

Brad said:
I have tried both the binary and the XML serialization; both give the
same results.

I have an object with a MemoryStream member. When I serialize and
deserialize the object, the MemoryStream member is created, but it's
actual stream blank. All primitive typed members of my object
deserialize fine.
Can anyone give me a clue about what I need to do to make this happen
/ why it's possible / not possible?

Serializing a stream makes no sense. It is *not* serializable. A stream is
pretty much like the wire that connects your PC to the Internet. Imagine I
wanted a file that you've downloaded from the Internet to your PC. You
wouldn't want me to come to your place and hook my PC up to your wire to get
the file as well -- but that's pretty much what serializing a stream would
mean.

So what you need to do is serialize the actual data, not the medium that
carries it. Since MemoryStream is a simple buffer for bytes, the obvious
solution is to serialize it by calling MemoryStream.ToArray() and serialize
the resulting byte array.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top