deflateStream reading from stream and writing to a buffer
-
I'm having problems understanding why a loop is needed to read from a deflated stream. i've replaced the read all bytes in stream method with the following line, however the data in the output buffer is 1 byte smaller than it was before the data was compressed:
Return stream.Read(buffer, 0, buffer.Length)
used to be:Dim offset As Integer = 0 Dim totalCount As Integer = 0 While True Dim bytesRead As Integer = stream.Read(buffer, offset, 100) If bytesRead = 0 Then Exit While End If offset += bytesRead totalCount += bytesRead End While Return totalCount
the full code for this is taken from http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream(VS.80).aspx[^] -
I'm having problems understanding why a loop is needed to read from a deflated stream. i've replaced the read all bytes in stream method with the following line, however the data in the output buffer is 1 byte smaller than it was before the data was compressed:
Return stream.Read(buffer, 0, buffer.Length)
used to be:Dim offset As Integer = 0 Dim totalCount As Integer = 0 While True Dim bytesRead As Integer = stream.Read(buffer, offset, 100) If bytesRead = 0 Then Exit While End If offset += bytesRead totalCount += bytesRead End While Return totalCount
the full code for this is taken from http://msdn2.microsoft.com/en-us/library/system.io.compression.deflatestream(VS.80).aspx[^]The Read method doesn't have to read all data that you request. It returns the number of bytes actually read, so you have to loop until you have filled the buffer or until the method returns zero.
Dim offset As Integer = 0
Dim bytesRead As Integer
Do
bytesRead = stream.Read(buffer, offset, buffer.Length - offset)
offset += bytesRead
Loop Until bytesRead = 0 Or offset = buffer.Length
Return offsetExperience is the sum of all the mistakes you have done.