I've got a question about navigating memory streams.

Memory stream properties aside (like making sure the stream can use seek etc...) if someone is storing variable or dynamic data in a stream, how can you retrieve that data? Like, for example if a user could store an image from their hard drive (for example) in a memory stream. and could add to that memory stream a caption to go with the image...how could someone retrieve only the image from the stream if we don't know how big the image is? It could be any number of bytes. I know you can use the Read method and seek to the start of the image data (which a programmer might know depending on how they organize the stream) but how do you know when to stop reading (the count parameter)? Do memory streams contain a "map" of some sort? or is it strictly a STREAM stream, and your SOL if you don't know the locations of the data in the stream?

I ask because I am encrypting (or trying to) a memory stream and then serializing it. But the memory stream contains a class instance as well as some string, integer and object data. The string, integer and object data I have a handle on size, the class instance not so much. I know where the class instance starts, but the size...not so sure of.

Recommended Answers

All 4 Replies

Are you trying to save the class information to reconstruct the class later? If so, serialize the class first, then encrypt it. If you wrote your decrypt/deserialize correctly, you'll be able to recover the class.

Yep. I have serialized it, and I can get the data to encrypt. Here's the issue:

MemoryStream A is encrypted and put into MemoryStream B.
MemoryStream B also has to hold other information, such as some strings, integers and an object.
Once the strings, integers and objects are added to the stream and the serialized and encrypted MemoryStream A is added to MemoryStream B. MemoryStream B is then serialized to a file.

I think I'm getting close to figuring this out. But I just cant seem to figure out, when I deserialize stream B, how do I deserialize stream A when I don't know where it ends. I know where it starts, but I don't know it's length.

Since stream A is a MemoryStream you know its size.
Add this as an integer to stream B before adding stream A.
Now you know the size of the stream when de-serializing it.

Alternatively, add stream A at the end of stream B.
What is left after reading all the other stuff must be stream A.

Oooh good idea nick!

I was kind of thinking the exact same thing actually. Basically creating my own memory map so I know the first 4 bytes represent where blah blah blah starts and the second 4 bytes represent where blah blah blah ends etc...and I can just use that as a reference.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.