Ejemplo n.º 1
0
        internal static void DecompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat, long maxReceivedMessageSize)
        {
            MemoryStream memoryStream        = new MemoryStream(buffer.Array, buffer.Offset, buffer.Count);
            int          maxDecompressedSize = (int)Math.Min(maxReceivedMessageSize, int.MaxValue);

            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxReceivedMessageSizeExceeded, 1024, maxDecompressedSize, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                byte[] tempBuffer = bufferManager.TakeBuffer(DecompressBlockSize);
                try
                {
                    using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                                       (Stream) new GZipStream(memoryStream, CompressionMode.Decompress) :
                                       (Stream) new DeflateStream(memoryStream, CompressionMode.Decompress))
                    {
                        while (true)
                        {
                            int bytesRead = ds.Read(tempBuffer, 0, DecompressBlockSize);
                            if (bytesRead > 0)
                            {
                                bufferedOutStream.Write(tempBuffer, 0, bytesRead);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                finally
                {
                    bufferManager.ReturnBuffer(tempBuffer);
                }

                int    length            = 0;
                byte[] decompressedBytes = bufferedOutStream.ToArray(out length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(decompressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }
        internal static void CompressBuffer(ref ArraySegment <byte> buffer, BufferManager bufferManager, CompressionFormat compressionFormat)
        {
            using (BufferManagerOutputStream bufferedOutStream = new BufferManagerOutputStream(SR.MaxSentMessageSizeExceeded, 1024, int.MaxValue, bufferManager))
            {
                bufferedOutStream.Write(buffer.Array, 0, buffer.Offset);

                using (Stream ds = compressionFormat == CompressionFormat.GZip ?
                                   (Stream) new GZipStream(bufferedOutStream, CompressionMode.Compress, true) :
                                   (Stream) new DeflateStream(bufferedOutStream, CompressionMode.Compress, true))
                {
                    ds.Write(buffer.Array, buffer.Offset, buffer.Count);
                }

                byte[] compressedBytes = bufferedOutStream.ToArray(out int length);
                bufferManager.ReturnBuffer(buffer.Array);
                buffer = new ArraySegment <byte>(compressedBytes, buffer.Offset, length - buffer.Offset);
            }
        }