This project is read-only.

Incorrect Value Returned by DeflateStream.Read()

Sep 28, 2009 at 7:32 PM
Edited Sep 28, 2009 at 8:14 PM


I am attempting to compress and then decompress some byte arrays using the newest (beta) version of libraries.  I am using the DeflateStream.  I had this working in version 1.8 of the library, but upgraded to 1.9 (had some weird issues, possibly unrelated to this library, but I figured it couldn't hurt to try something new).  There seems to be a bug that occurs when using the Read() function of the DeflateStream when I am decompressing.  I have the following code:

                int compressedLength;
                byte[] bufferedBytes;

                MemoryStream memoryStream = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
                DeflateStream deflateStream = new DeflateStream(memoryStream, CompressionMode.Decompress, true);
                MemoryStream uncompressedStream = new MemoryStream();
                int readBytes = 0;
                bufferedBytes = new byte[1024];
                while ((readBytes = deflateStream.Read(bufferedBytes, 0, bufferedBytes.Length)) > 0)
                    uncompressedStream.Write(bufferedBytes, 0, readBytes);

As you can see, I am attempting to loop by 1024 byte increments and decompress an array of bytes (buffer.Array is a byte[]).  The bug seems to manifest when the decompressed size of the byte array is smaller than 1024.  So, if the input is maybe 293 bytes compressed, and would be maybe 417 bytes uncompressed, the Read() function returns 0 as the number of bytes read.  But not always the case, ie, many times it works correctly and returns the number of bytes read even if it is less than 1024.  But I can consistently reproduce this with the 293 bytes compressed to 417 bytes uncompressed.  The interesting thing is that the DeflateStream correctly decompresses it, that is to say, after calling Read(), I can see that the DeflateStream.TotalIn is 293 and the DeflateStream.TotalOut is 417...  But since the Read() returned 0, nothing ends up being written to the "uncompressedStream" variable, even though "bufferedBytes" actually contains the correct data.

I am pretty sure that this is not the desired behavior, and has caused me some issues.  I have found a workaround, which is to make my "bufferedBytes" array be of size Math.Min(1024, buffer.Count) instead of 1024.  This basically eliminates cases where the size of "bufferedBytes" is greater than the decompressed byte array returned by Read().  But I shouldn't have to do this.  The updated line of code is:         

               bufferedBytes = new byte[Math.Min(1024, buffer.Count)];

Any help would be appreciated.


Sep 29, 2009 at 7:39 AM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Sep 29, 2009 at 7:41 AM

Sounds like a bug.

Can you upload the deflated (293-byte) file that causes the problem?  Attach it to the workitem.  (See the post on this discussion forum thread for the link).

When you post the file I'll get it fixed.

Sep 29, 2009 at 1:45 PM

I tried to reproduce this problem but couldn't. 

If you can send me a file that causes the problem, I'll be able to fix it.