// This is called by Dispose: private void PurgeBuffers(bool disposing) { if (!disposing) { return; } if (_stream == null) { return; } Flush(); if (_mode != CompressionMode.Compress) { return; } // Some deflaters (e.g. ZLib write more than zero bytes for zero bytes inpuits. // This round-trips and we should be ok with this, but our legacy managed deflater // always wrote zero output for zero input and upstack code (e.g. GZipStream) // took dependencies on it. Thus, make sure to only "flush" when we actually had // some input: if (wroteBytes) { // Compress any bytes left: WriteDeflaterOutput(false); // Pull out any bytes left inside deflater: bool finished; do { int compressedBytes; finished = deflater.Finish(buffer, out compressedBytes); if (compressedBytes > 0) { DoWrite(buffer, 0, compressedBytes, false); } } while (!finished); } // Write format footer: if (formatWriter != null && wroteHeader) { byte[] b = formatWriter.GetFooter(); _stream.Write(b, 0, b.Length); } }
// This is called by Dispose: private void PurgeBuffers(bool disposing) { if (!disposing) { return; } if (_stream == null) { return; } Flush(); if (_mode != CompressionMode.Compress) { return; } // Some deflaters (e.g. ZLib write more than zero bytes for zero bytes inpuits. // This round-trips and we should be ok with this, but our legacy managed deflater // always wrote zero output for zero input and upstack code (e.g. GZipStream) // took dependencies on it. Thus, make sure to only "flush" when we actually had // some input: if (_wroteBytes) { // Compress any bytes left: WriteDeflaterOutput(); // Pull out any bytes left inside deflater: bool finished; do { int compressedBytes; finished = _deflater.Finish(_buffer, out compressedBytes); if (compressedBytes > 0) { DoWrite(_buffer, 0, compressedBytes); } } while (!finished); } else { // In case of zero length buffer, we still need to clean up the native created stream before // the object get disposed because eventually ZLibNative.ReleaseHandle will get called during // the dispose operation and although it frees the stream but it return error code because the // stream state was still marked as in use. The symptoms of this problem will not be seen except // if running any diagnostic tools which check for disposing safe handle objects bool finished; do { int compressedBytes; finished = _deflater.Finish(_buffer, out compressedBytes); } while (!finished); } // Write format footer: if (_formatWriter != null && _wroteHeader) { byte[] b = _formatWriter.GetFooter(); _stream.Write(b, 0, b.Length); } }