locked
Write file data directly to a property in my class RRS feed

  • Question

  • User1824003892 posted

    I have a class that I use to send file data to and from my database, My question is. How do I write file data directly to the "fileData" property in my class without first saving the data to the "ms" memoryStream. Snother question I am asking. Is wrighting the data to a memory stream the most effecient way to do this. Would there be any advantage to writing the data to the property directly.?

     

     

            Class Rif
                Public Property fileData As Stream
            End Class
    
            Dim rif = New Rif
            Using stream As Stream = myResp.GetResponseStream()
                Using ms As New MemoryStream()
                    Dim count As Integer
                    Do
                        Dim buf As Byte() = New Byte(1023) {}
                        count = stream.Read(buf, 0, 1024)
                        ms.Write(buf, 0, count)
                    Loop While stream.CanRead AndAlso count > 0
    
    
                    rif.fileData = stream

    Tuesday, May 21, 2013 3:41 PM

Answers

  • User-1469158370 posted

    You should never keep a property as a stream. This will lead to memory leaks. If the class is not disposed fairly quickly by the garbage collector, then the memory stream will be open.

    why can't you just read the response as byte[] and assign that to your property. This way you are not dealing with streams but with data directly

    You can use System.Text.Encoding.GetBytes method for this.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 21, 2013 11:50 PM