Пример #1
0
        public void RoundTrip_Chunks()
        {
            int           chunkSize = 100;
            int           totalSize = 20000;
            BrotliEncoder encoder   = default;
            BrotliDecoder decoder   = default;

            for (int i = 0; i < totalSize; i += chunkSize)
            {
                byte[] uncompressed = new byte[chunkSize];
                new Random().NextBytes(uncompressed);
                byte[] compressed       = new byte[BrotliEncoder.GetMaxCompressedLength(chunkSize)];
                byte[] deompressed      = new byte[chunkSize];
                var    uncompressedSpan = new ReadOnlySpan <byte>(uncompressed);
                var    compressedSpan   = new Span <byte>(compressed);
                var    decompressedSpan = new Span <byte>(deompressed);

                int totalWrittenThisIteration = 0;
                var compress = encoder.Compress(uncompressedSpan, compressedSpan, out int bytesConsumed, out int bytesWritten, isFinalBlock: false);
                totalWrittenThisIteration += bytesWritten;
                compress = encoder.Flush(compressedSpan.Slice(bytesWritten), out bytesWritten);
                totalWrittenThisIteration += bytesWritten;

                var res = decoder.Decompress(compressedSpan.Slice(0, totalWrittenThisIteration), decompressedSpan, out int decompressbytesConsumed, out int decompressbytesWritten);
                Assert.Equal(totalWrittenThisIteration, decompressbytesConsumed);
                Assert.Equal(bytesConsumed, decompressbytesWritten);
                for (int j = 0; j < bytesConsumed; j++)
                {
                    Assert.Equal(uncompressed[j], decompressedSpan[j]);
                }
            }
        }