Do I need to 'rewind' a stream before reading what was written to it?
Asked Answered
A

1

6

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?

Abshire answered 8/5, 2018 at 17:33 Comment(0)
P
4

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.

Pathfinder answered 8/5, 2018 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.