예제 #1
0
        public void CanCompressAndDecompressData(bool highCompression, bool randomData)
        {
            var codec = new LZ4Codec(LZ4Codec.DefaultBlockSize, highCompression, new RecyclableMemoryStreamManager(),
                                     LZ4Codec.CodecProvider.LZ4Net);
            for (var bufferLength = 10; bufferLength < 10000000; bufferLength *= 10)
            {
                var sourceBuffer = randomData
                                       ? GenerateRandomDataBuffer(bufferLength)
                                       : GenerateNonrandomDataBuffer(bufferLength);
                using (var compressedStream = new MemoryStream())
                {
                    using (var compressionStream = codec.GetStream(compressedStream, CompressionMode.Compress, true))
                    {
                        compressionStream.Write(sourceBuffer, 0, sourceBuffer.Length);
                    }

                    compressedStream.Position = 0;
                    var destinationBuffer = new byte[sourceBuffer.Length];
                    using (var decompressionStream = codec.GetStream(compressedStream, CompressionMode.Decompress, true))
                    {
                        decompressionStream.Read(destinationBuffer, 0, destinationBuffer.Length);
                    }

                    for (var i = 0; i < bufferLength; ++i)
                    {
                        Assert.AreEqual(sourceBuffer[i], destinationBuffer[i]);
                    }
                }
            }
        }
예제 #2
0
        internal LZ4NetStream(Stream innerStream, CompressionMode compressionMode, bool leaveInnerStreamOpen,
                              LZ4Codec codec)
        {
            if (innerStream == null)
            {
                throw new ArgumentNullException(nameof(innerStream));
            }
            switch (compressionMode)
            {
            case CompressionMode.Compress:
            case CompressionMode.Decompress:
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(compressionMode));
            }
            if (codec == null)
            {
                throw new ArgumentNullException(nameof(codec));
            }

            this.innerStream = innerStream;
            this.compressionMode = compressionMode;
            this.leaveInnerStreamOpen = leaveInnerStreamOpen;
            this.codec = codec;
        }