Tuesday, November 25, 2014

Why is my MemoryStream apparently full of zeroes?

I have a memory stream as a member of a class, call it ms


After I've written to ms, I try to do this:



ms.Flush();
ms.SetLength(ms.Position);
ms.Seek(0, SeekOrigin.Begin);

// now pass it to a reader function
ReadFromStream(ms.AsRandomAccessStream());

However, the function only reads zeroes from the passed stream.


If on the other hand I do this:



ms.Flush();
ms.SetLength(ms.Position);
ms.Seek(0, SeekOrigin.Begin);

byte[] bytes = ms.ToArray();
MemoryStream ms2 = new MemoryStream(bytes);

// now pass it to a reader function
ReadFromStream(ms2.AsRandomAccessStream);

Everything works fine, though I've incurred an extra copy of all those bytes.


Any idea why the former method doesn't work on 8.1?




Anthony Wieser | Wieser Software Ltd | http://ift.tt/1cEdngW


No comments:

Post a Comment