/// <summary>
        /// Helper method to handle decompression of <see cref="WebResponse"/> streams. If the header contains a "Content-Encoding" that is supported by one of
        /// the <see cref="Compressors"/>, the returned stream will automatically be decompressed.
        /// </summary>
        /// <param name="response">Response to process.</param>
        /// <returns>Decompressed stream.</returns>
        public static Stream Decompress(WebResponse response)
        {
            IDeCompressor compressor     = CheckSupportedCompression(response.Headers.Get("CONTENT-ENCODING"));
            Stream        responseStream = response.GetResponseStream();

            return(compressor == null ? responseStream : Decompress(compressor, responseStream));
        }
        /// <summary>
        /// Helper method to write the contents of <paramref name="inputStream"/> to the <paramref name="response"/> body. Depending on the
        /// accepted encodings (<paramref name="acceptEncoding"/> argument), the result will be compressed or not.
        /// </summary>
        /// <param name="acceptEncoding">The Request's accepted encodings.</param>
        /// <param name="response">Response to be written.</param>
        /// <param name="inputStream">The input stream the will be written into the Response.</param>
        public static async Task WriteCompressedStream(string acceptEncoding, IOwinResponse response, MemoryStream inputStream)
        {
            IDeCompressor compressor = CheckSupportedCompression(acceptEncoding);

            byte[] buffer;
#if !DISABLE_COMPRESSION
            if (compressor == null)
            {
#endif
            buffer = inputStream.ToArray();
            response.ContentLength = buffer.Length;
            await response.Body.WriteAsync(buffer, 0, buffer.Length);

            return;

#if !DISABLE_COMPRESSION
        }
#endif

            using (MemoryStream compressedStream = (MemoryStream)Compress(compressor, inputStream))
                buffer = compressedStream.ToArray();

            response.Headers["Content-Encoding"] = compressor.EncodingName;
            // If there were multiple methods supported, we need to indicate the varying header.
            if (acceptEncoding != compressor.EncodingName)
            {
                response.Headers["Vary"] = "Accept-Encoding";
            }

            response.ContentLength = buffer.Length;
            await response.Body.WriteAsync(buffer, 0, buffer.Length);
        }
        /// <summary>
        /// Compresses the content of the given <paramref name="inputStream"/> and returns a compressed stream.
        /// </summary>
        /// <param name="compressor">The <see cref="IDeCompressor"/> to be used.</param>
        /// <param name="inputStream">Input Stream to read from. Reading will start from current <see cref="Stream.Position"/>.</param>
        /// <returns>Compressed stream.</returns>
        private static Stream Compress(IDeCompressor compressor, Stream inputStream)
        {
            MemoryStream compressed = new MemoryStream();
            Stream       compressedStream;

            using (compressedStream = compressor.CreateCompressionStream(compressed))
            {
                byte[] buffer = new byte[BUFFER_SIZE];
                int    nRead;
                while ((nRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    compressedStream.Write(buffer, 0, nRead);
                }
            }
            compressed.Position = 0;
            return(compressed);
        }
    /// <summary>
    /// Compresses the content of the given <paramref name="inputStream"/> and returns a compressed stream.
    /// </summary>
    /// <param name="compressor">The <see cref="IDeCompressor"/> to be used.</param>
    /// <param name="inputStream">Input Stream to read from. Reading will start from current <see cref="Stream.Position"/>.</param>
    /// <returns>Compressed stream.</returns>
    private static Stream Compress(IDeCompressor compressor, Stream inputStream)
    {
      MemoryStream compressed = new MemoryStream();
      Stream compressedStream;

      using (compressedStream = compressor.CreateCompressionStream(compressed))
      {
        byte[] buffer = new byte[BUFFER_SIZE];
        int nRead;
        while ((nRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
          compressedStream.Write(buffer, 0, nRead);
      }
      compressed.Position = 0;
      return compressed;
    }