title: Puzzling .Net C# DeflateStream problem
body: <p>I wonder if anyone can shed some light on an issue that is driving me nuts:</p>
  
  <p>I am writing a compression decompression test class.  To test it, I am serializing a dataset to a memory stream, compressing it, and uncompressing it and comparing the results. </p>
  
  <p>Compression is fine, but uncompression is where it hits the dirt. 
  This is the decompress function:</p>
  
  <pre><code>    public static Stream GetUncompressedStreamCopy(Stream inStream)
      {
        Stream outStream = new MemoryStream();
  
        inStream.Position = 0;
  
        DeflateStream uncompressStream = new DeflateStream(inStream, 
          CompressionMode.Decompress, true);
  
        byte[] buffer = new byte[65536];
  
        int totalread = 0;
        int bytesread = 0;
  
  
        do {
          bytesread = uncompressStream.Read(buffer, 0, buffer.Length);
          totalread += bytesread;
          outStream.Write(buffer, 0, bytesread);
          Console.WriteLine("bytesRead: [{0}]\t outStream.Length [{1}]",
          bytesread, outStream.Length);
        } while (bytesread &gt; 0);
  
  
        Console.WriteLine("total bytes read [{0}]", totalread);
        outStream.Flush();
        return outStream;
  }
  </code></pre>
  
  <p>With a buffer of size 65536 the decompressed stream always returns one byte less than it was uncompressed.</p>
  
  <p>Now this brings me to the second issue which I am battling with.  With some buffer sizes, uncompressStream.Read returns 0 even though there is still compressed data left to extract.</p>
  
  <p>For these cases, deflateStream.Read(s) only once in the do{} loop and then returns an uncompressed stream equal to buffersize, if you increase the buffersize by a single byte all is well (except for the missing byte).</p>
  
  <p>Output for buffersize of 65536:  (Original uncompressed data is 207833)</p>
  
  <pre><code>bytesRead: [65536]       outStream.Length [65536]
  bytesRead: [65536]       outStream.Length [131072]
  bytesRead: [58472]       outStream.Length [189544]
  bytesRead: [18288]       outStream.Length [207832]
  bytesRead: [0]           outStream.Length [207832]
  total bytes read [207832]
  </code></pre>
  
  <p>buffersize of 189544 (Some magic number where the code tanks)</p>
  
  <pre><code>bytesRead: [189544]      outStream.Length [189544]
  bytesRead: [0]           outStream.Length [189544]
  total bytes read [189544]
  Unompressed stream size 189544
  </code></pre>
  
  <p>Also note the 3rd read of buffersize 65536 ex: bytesRead: [58472]  Clearly this should also be 65536 as there still data left on the buffer?</p>
  
  <p>Any ideas will be <em>greatly</em> appreciated.</p>
  
  <p>tia</p>
  
  <ul>
  <li>Jaco</li>
  </ul>