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)); }
protected byte[] DecodeStream(BufferPoolMemoryStream streamToDecode) { streamToDecode.Seek(0, SeekOrigin.Begin); // The cache stores the decoded data var encoding = #if !BESTHTTP_DISABLE_CACHING IsFromCache ? null : #endif GetHeaderValues("content-encoding"); #if !UNITY_WEBGL || UNITY_EDITOR Stream decoderStream = null; #endif // Return early if there are no encoding used. if (encoding == null) { return(streamToDecode.ToArray()); } else { switch (encoding[0]) { #if !UNITY_WEBGL || UNITY_EDITOR case "gzip": decoderStream = new Decompression.Zlib.GZipStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; case "deflate": decoderStream = new Decompression.Zlib.DeflateStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; #endif //identity, utf-8, etc. default: // Do not copy from one stream to an other, just return with the raw bytes return(streamToDecode.ToArray()); } } #if !UNITY_WEBGL || UNITY_EDITOR using (var ms = new BufferPoolMemoryStream((int)streamToDecode.Length)) { var buf = BufferPool.Get(1024, true); int byteCount = 0; while ((byteCount = decoderStream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, byteCount); } BufferPool.Release(buf); decoderStream.Dispose(); return(ms.ToArray()); } #endif }
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); }
protected byte[] DecodeStream(Stream streamToDecode) { streamToDecode.Seek(0, SeekOrigin.Begin); // The cache stores the decoded data var encoding = #if !BESTHTTP_DISABLE_CACHING && (!UNITY_WEBGL || UNITY_EDITOR) IsFromCache ? null : #endif GetHeaderValues("content-encoding"); Stream decoderStream = null; if (encoding == null) { decoderStream = streamToDecode; } else { switch (encoding[0]) { #if !UNITY_WEBGL || UNITY_EDITOR case "gzip": decoderStream = new Decompression.Zlib.GZipStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; case "deflate": decoderStream = new Decompression.Zlib.DeflateStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; #endif default: //identity, utf-8, etc. decoderStream = streamToDecode; break; } } using (var ms = new MemoryStream((int)streamToDecode.Length)) { var buf = new byte[1024]; int byteCount = 0; while ((byteCount = decoderStream.Read(buf, 0, buf.Length)) > 0) { ms.Write(buf, 0, byteCount); } return(ms.ToArray()); } }
private void CloseDecompressors() { if (decompressorGZipStream != null) { decompressorGZipStream.Dispose(); } decompressorGZipStream = null; if (decompressorInputStream != null) { decompressorInputStream.Dispose(); } decompressorInputStream = null; if (decompressorOutputStream != null) { decompressorOutputStream.Dispose(); } decompressorOutputStream = null; }
/// <summary> /// Uncompress a GZip'ed byte array into a single string. /// </summary> /// /// <seealso cref="GZipStream.CompressString(String)"/> /// <seealso cref="GZipStream.UncompressBuffer(byte[])"/> /// /// <param name="compressed"> /// A buffer containing GZIP-compressed data. /// </param> /// /// <returns>The uncompressed string</returns> public static String UncompressString(byte[] compressed) { using (var input = new MemoryStream(compressed)) { Stream decompressor = new GZipStream(input, CompressionMode.Decompress); return ZlibBaseStream.UncompressString(compressed, decompressor); } }
/// <summary> /// Compress a byte array into a new byte array using GZip. /// </summary> /// /// <remarks> /// Uncompress it with <see cref="GZipStream.UncompressBuffer(byte[])"/>. /// </remarks> /// /// <seealso cref="GZipStream.CompressString(string)"/> /// <seealso cref="GZipStream.UncompressBuffer(byte[])"/> /// /// <param name="b"> /// A buffer to compress. /// </param> /// /// <returns>The data in compressed form</returns> public static byte[] CompressBuffer(byte[] b) { using (var ms = new MemoryStream()) { System.IO.Stream compressor = new GZipStream( ms, CompressionMode.Compress, CompressionLevel.BestCompression ); ZlibBaseStream.CompressBuffer(b, compressor); return ms.ToArray(); } }
/// <summary> /// Uncompress a GZip'ed byte array into a byte array. /// </summary> /// /// <seealso cref="GZipStream.CompressBuffer(byte[])"/> /// <seealso cref="GZipStream.UncompressString(byte[])"/> /// /// <param name="compressed"> /// A buffer containing data that has been compressed with GZip. /// </param> /// /// <returns>The data in uncompressed form</returns> public static byte[] UncompressBuffer(byte[] compressed) { using (var input = new System.IO.MemoryStream(compressed)) { System.IO.Stream decompressor = new GZipStream( input, CompressionMode.Decompress ); return ZlibBaseStream.UncompressBuffer(compressed, decompressor); } }
protected byte[] DecodeStream(Stream streamToDecode) { streamToDecode.Seek(0, SeekOrigin.Begin); // The cache stores the decoded data var encoding = IsFromCache ? null : GetHeaderValues("content-encoding"); Stream decoderStream = null; if (encoding == null) decoderStream = streamToDecode; else { switch (encoding[0]) { case "gzip": decoderStream = new Decompression.Zlib.GZipStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; case "deflate": decoderStream = new Decompression.Zlib.DeflateStream(streamToDecode, Decompression.Zlib.CompressionMode.Decompress); break; default: //identity, utf-8, etc. decoderStream = streamToDecode; break; } } using (var ms = new MemoryStream((int)streamToDecode.Length)) { var buf = new byte[1024]; int byteCount = 0; while ((byteCount = decoderStream.Read(buf, 0, buf.Length)) > 0) ms.Write(buf, 0, byteCount); return ms.ToArray(); } }