public DecompressedData Decompress(byte[] data, int offset, int count, bool forceDecompress = false, bool dataCanBeLarger = false) { if (decompressorInputStream == null) { decompressorInputStream = new BufferPoolMemoryStream(count); } if (data != null) { decompressorInputStream.Write(data, offset, count); } if (!forceDecompress && decompressorInputStream.Length < MinLengthToDecompress) { return(new DecompressedData(null, 0)); } decompressorInputStream.Position = 0; if (decompressorGZipStream == null) { decompressorGZipStream = new Zlib.GZipStream(decompressorInputStream, Zlib.CompressionMode.Decompress, Zlib.CompressionLevel.Default, true); decompressorGZipStream.FlushMode = Zlib.FlushType.Sync; } if (decompressorOutputStream == null) { decompressorOutputStream = new BufferPoolMemoryStream(); } decompressorOutputStream.SetLength(0); byte[] copyBuffer = BufferPool.Get(1024, true); int readCount; int sumReadCount = 0; while ((readCount = decompressorGZipStream.Read(copyBuffer, 0, copyBuffer.Length)) != 0) { decompressorOutputStream.Write(copyBuffer, 0, readCount); sumReadCount += readCount; } BufferPool.Release(copyBuffer); // If no read is done (returned with any data) don't zero out the input stream, as it would delete any not yet used data. if (sumReadCount > 0) { decompressorGZipStream.SetLength(0); } byte[] result = decompressorOutputStream.ToArray(dataCanBeLarger); return(new DecompressedData(result, dataCanBeLarger ? (int)decompressorOutputStream.Length : result.Length)); }
private byte[] Decompress(byte[] data, int offset, int count) { if (decompressorInputStream == null) { decompressorInputStream = new MemoryStream(count); } decompressorInputStream.Write(data, offset, count); // http://tools.ietf.org/html/rfc7692#section-7.2.2 // Append 4 octets of 0x00 0x00 0xff 0xff to the tail end of the payload of the message. //decompressorInputStream.Write(PerMessageCompression.Trailer, 0, PerMessageCompression.Trailer.Length); decompressorInputStream.Position = 0; if (decompressorGZipStream == null) { decompressorGZipStream = new Decompression.Zlib.GZipStream(decompressorInputStream, Decompression.Zlib.CompressionMode.Decompress, Decompression.Zlib.CompressionLevel.Default, true); decompressorGZipStream.FlushMode = Decompression.Zlib.FlushType.Sync; } if (decompressorOutputStream == null) { decompressorOutputStream = new System.IO.MemoryStream(); } decompressorOutputStream.SetLength(0); if (copyBuffer == null) { copyBuffer = new byte[1024]; } int readCount; while ((readCount = decompressorGZipStream.Read(copyBuffer, 0, copyBuffer.Length)) != 0) { decompressorOutputStream.Write(copyBuffer, 0, readCount); } decompressorGZipStream.SetLength(0); byte[] result = decompressorOutputStream.ToArray(); /*if (this.ServerNoContextTakeover) * { * decompressorDeflateStream.Dispose(); * decompressorDeflateStream = null; * }*/ return(result); }
private byte[] Decompress(byte[] data, int offset, int count, bool forceDecompress = false) { if (decompressorInputStream == null) { decompressorInputStream = new MemoryStream(count); } if (data != null) { decompressorInputStream.Write(data, offset, count); } if (!forceDecompress && decompressorInputStream.Length < MinLengthToDecompress) { return(null); } decompressorInputStream.Position = 0; if (decompressorGZipStream == null) { decompressorGZipStream = new Decompression.Zlib.GZipStream(decompressorInputStream, Decompression.Zlib.CompressionMode.Decompress, Decompression.Zlib.CompressionLevel.Default, true); decompressorGZipStream.FlushMode = Decompression.Zlib.FlushType.Sync; } if (decompressorOutputStream == null) { decompressorOutputStream = new System.IO.MemoryStream(); } decompressorOutputStream.SetLength(0); if (copyBuffer == null) { copyBuffer = new byte[1024]; } int readCount; while ((readCount = decompressorGZipStream.Read(copyBuffer, 0, copyBuffer.Length)) != 0) { decompressorOutputStream.Write(copyBuffer, 0, readCount); } decompressorGZipStream.SetLength(0); byte[] result = decompressorOutputStream.ToArray(); return(result); }
private byte[] Decompress(byte[] data, int offset, int count, bool forceDecompress = false) { if (decompressorInputStream == null) { decompressorInputStream = new BufferPoolMemoryStream(count); } if (data != null) { decompressorInputStream.Write(data, offset, count); } if (!forceDecompress && decompressorInputStream.Length < MinLengthToDecompress) { return(null); } decompressorInputStream.Position = 0; if (decompressorGZipStream == null) { decompressorGZipStream = new Decompression.Zlib.GZipStream(decompressorInputStream, Decompression.Zlib.CompressionMode.Decompress, Decompression.Zlib.CompressionLevel.Default, true); decompressorGZipStream.FlushMode = Decompression.Zlib.FlushType.Sync; } if (decompressorOutputStream == null) { decompressorOutputStream = new BufferPoolMemoryStream(); } decompressorOutputStream.SetLength(0); byte[] copyBuffer = VariableSizedBufferPool.Get(1024, true); int readCount; while ((readCount = decompressorGZipStream.Read(copyBuffer, 0, copyBuffer.Length)) != 0) { decompressorOutputStream.Write(copyBuffer, 0, readCount); } VariableSizedBufferPool.Release(copyBuffer); decompressorGZipStream.SetLength(0); // TODO: be able to use a larger array byte[] result = decompressorOutputStream.ToArray(); return(result); }