5

With this code:

using (var stream = new MemoryStream())
{
    thumbnail.Save(stream); // you get the idea
    stream.Position = 0; // <- is this needed?
    WriteStreamToDisk(stream);
}

If I have a method writing to a memory stream, and then I want to write that stream to disk, do I need to set the position to 0?

Or, do streams have different read / write pointers?

Michael Liu
  • 52,147
  • 13
  • 117
  • 150
Thomas
  • 10,933
  • 14
  • 65
  • 136

1 Answers1

3

A stream has only a single position which is used for both reading and writing. So, assuming that...

  1. Thumbnail.Save(O); doesn't rewind the stream after it's done writing to the stream, and
  2. WriteStreamToDisk(O); doesn't rewind the stream before it starts reading from the stream,

then yes, you will need to rewind the stream yourself.

Michael Liu
  • 52,147
  • 13
  • 117
  • 150