locked
Stream.CopyToAsync to InMemoryRandomAccessStream fails without throwing exception

    Question

  • The following code works about 75% of the time. It's not random in the sense the same buffer data will either copy or fail. There is no exception thrown, but, when it fails, ras.Length == 0 after the call to CopyToAsync. What are the conditions that CopyToAsync will fail? Also, I know for a fact that the data in the buffer is fine when it fails, it is a jpg image and the image data is fine.

    ms.Position = 0;
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    try
    {
    	await ms.CopyToAsync(ras.AsStreamForWrite(), (int)ms.Length);
    }
    catch (Exception ex)
    {
    	System.Diagnostics.Debug.WriteLine(ex.Message);
    }
    ras.Seek(0);


    • Edited by jonpb1 Sunday, November 23, 2014 12:39 AM
    Sunday, November 23, 2014 12:38 AM

Answers

  • The above code does not work, it is obviously a bug, there are certain bytes sets that CopyToAsync both fail to copy and permanently corrupt the the MemoryStream. Should I report it? I can give the byte array for testing.

    The following code does work, but, requires an additional byte[] buffer. Btw, I don't actually understand why I can't just use a MemoryStream to create a BitmaImage in the first place so that all this additional buffering would not be needed.

    ms.Position = 0;
    byte[] buf = new byte[ms.Length];
    await ms.ReadAsync(buf, 0, (int)ms.Length);
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    var dw = new Windows.Storage.Streams.DataWriter(ras);
    dw.WriteBytes(buf);
    await dw.StoreAsync();
    ras.Seek(0);
    

    Sunday, November 23, 2014 6:01 PM

All replies

  • The above code does not work, it is obviously a bug, there are certain bytes sets that CopyToAsync both fail to copy and permanently corrupt the the MemoryStream. Should I report it? I can give the byte array for testing.

    The following code does work, but, requires an additional byte[] buffer. Btw, I don't actually understand why I can't just use a MemoryStream to create a BitmaImage in the first place so that all this additional buffering would not be needed.

    ms.Position = 0;
    byte[] buf = new byte[ms.Length];
    await ms.ReadAsync(buf, 0, (int)ms.Length);
    InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
    var dw = new Windows.Storage.Streams.DataWriter(ras);
    dw.WriteBytes(buf);
    await dw.StoreAsync();
    ras.Seek(0);
    

    Sunday, November 23, 2014 6:01 PM
  • Hi jonpb1,

    Thanks for sharing your solution with us.

    Ok, for the CopyToAsync(), can you confirm the size of the buffer? As I can see from the documentation: AsStreamForWrite Method (IOutputStream), there is some size limitation.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, November 24, 2014 9:34 AM
    Moderator