locked
decompress stream using memorystream?!?!? RRS feed

  • Question

  • User1080785583 posted

    What is the purpose of using Stream and why is this an unsafe method?

    /// <summary>
            /// WARNING! This is unsafe method. MemoryStream used inside. should not be used if possible.
            /// </summary>
            /// <param name="inputStream"></param>
            /// <returns></returns>
            public static byte[] Decompress(Stream inputStream)
            {
                using (inputStream)
                using (MemoryStream output = new MemoryStream())
                {
                    Decompress(inputStream, output);
                    return output.ToArray();
                }
            }

    Thursday, April 23, 2015 2:25 PM

Answers

  • User753101303 posted

    Hi,

    A Stream is just an "abstraction" for a series of byte (and it could be a network, a file, a memory Stream etc... allowing to use them all the same way).

    In my opinion, the comment is just wrong. This is not unsafe with the C# meaning see https://msdn.microsoft.com/en-us/library/t2yzs44b.aspx and not even otherwise. Likely some confusion (or was afraid about OutOfMemory exceptions etc...)

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 24, 2015 4:34 AM

All replies

  • User753101303 posted

    Hi,

    A Stream is just an "abstraction" for a series of byte (and it could be a network, a file, a memory Stream etc... allowing to use them all the same way).

    In my opinion, the comment is just wrong. This is not unsafe with the C# meaning see https://msdn.microsoft.com/en-us/library/t2yzs44b.aspx and not even otherwise. Likely some confusion (or was afraid about OutOfMemory exceptions etc...)

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 24, 2015 4:34 AM
  • User-821857111 posted

    why is this an unsafe method?
    According to whom?

    Friday, April 24, 2015 4:38 AM
  • User1080785583 posted

    I inherited some code and I am optimizing it and someone put in a startling comment...

    Is  it bad practice to pass around a stream instead of something else??

    Friday, April 24, 2015 11:59 AM
  • User753101303 posted

    No as I said the comment is IMO just wrong. Ignore it. Only the author could tell us why he wrote this....

    Friday, April 24, 2015 12:04 PM
  • User1080785583 posted

    CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(binaryStorageId);

    using (var memoryStream = new MemoryStream())
    {
    blockBlob2.DownloadToStream(memoryStream);
    //var text = Encoding.UTF8.GetString(memoryStream.ToArray());
    //return text;
    var base64string = Convert.ToBase64String(memoryStream.ToArray(), 0, memoryStream.ToArray().Length);
    return base64string;
    }

    I am using another memory stream, can you tell me if this is bad practice to return inside of a using of memory stream?

    Friday, April 24, 2015 12:34 PM
  • User753101303 posted

    It's best to start a new thread rather than to ask a new question in a thread which is already closed.

    It's fine. The point is precisely to guarantee the Dispose call even if you have a return Inside the using block (behind the scene it is  a try/finally construct).

    Monday, April 27, 2015 12:42 PM