locked
Why is my MemoryStream apparently full of zeroes?

    Question

  • 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 | www.wieser-software.com

    Tuesday, November 25, 2014 6:27 AM

All replies

  • What exactly was ms before it gets copied? And what is the readfromstream function?  Can you give a sample of this code?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Tuesday, November 25, 2014 9:09 PM
    Moderator
  • ms was declared in the class as

    MemoryStream ms;


    ReadFromStream was a placeholder for the example. Note that the second version of the code works, and the first doesn't so it's kind of irrelevant what the function actually does, as it's the same function. It is in fact calling a member of another class declared thus:

    That implementation does this:

    public async Task<bool> ReadFromStream(IInputStream iis)
    {
       byte [] retb = new byte[len];
    
       int read = await s.ReadAsync(retb, len, CancellationToken.None);
    
       foreach (byte b in retb)
       {
          if (b !=0) return true;
       }
    
       return false;
    }

    This reads a buffer full of zeroes, and always returns false.  ReadAsync is my extension method that is implmemented thus (like my other question hints at here: https://social.msdn.microsoft.com/Forums/windowsapps/en-US/c32f6a9a-da50-4212-8ef0-a12530755492/when-does-iinputstreamreadasync-return-a-different-buffer?forum=winappswithcsharp )

    public static async Task<int> ReadAsync(this IInputStream s, byte[] b, uint len, CancellationToken ct,
     InputStreamOptions opt=InputStreamOptions.None, bool bThrowIfShort = true)
    {
    	var buf = b.AsBuffer();
    	IBuffer bRet = await s.ReadAsync(buf, len, opt).AsTask(ct);
    	int read = (int) bRet.Length;
    
    	if (bThrowIfShort && (len != read))
    	{
    		throw new EndOfStreamException(string.Format("Short stream: {0} != {1}", len, read));
    	}
    
    	if (bRet != buf)
    	{
    		bRet.CopyTo(0, b, 0, read);
    	}
    	return read;
    }
    




    Anthony Wieser | Wieser Software Ltd | www.wieser-software.com

    Wednesday, November 26, 2014 7:13 AM