public void WriteFully(string testFile)
        {
            byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
            byte[] compressedBytes          = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];
            byte[] actualUncompressedBytes  = new byte[BrotliEncoder.GetMaxCompressedLength(correctUncompressedBytes.Length)];

            Span <byte> destination = new Span <byte>(compressedBytes);

            Assert.True(BrotliEncoder.TryCompress(correctUncompressedBytes, destination, out int bytesWritten));
            Assert.True(BrotliDecoder.TryDecompress(destination, actualUncompressedBytes, out bytesWritten));
            Assert.Equal(correctUncompressedBytes.Length, bytesWritten);

            Assert.Equal <byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray());
        }
Beispiel #2
0
        public void Compress_Canterbury_WithoutState(string uncompressedFileName, CompressionLevel compressLevel)
        {
            byte[] bytes = File.ReadAllBytes(uncompressedFileName);
            ReadOnlySpan <byte> uncompressedData = new ReadOnlySpan <byte>(bytes);
            int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength(bytes.Length);

            byte[] compressedDataArray = new byte[maxCompressedSize];
            int    compressLevelBrotli = compressLevel == CompressionLevel.Optimal ? 11 : compressLevel == CompressionLevel.Fastest ? 1 : 0;

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    Assert.True(BrotliEncoder.TryCompress(uncompressedData, compressedDataArray, out int bytesWritten, compressLevelBrotli, 22));
                }
            }
        }
Beispiel #3
0
        public Span <byte> Compress_WithState()
        {
            using (BrotliEncoder encoder = new BrotliEncoder(GetQuality(level), Window))
            {
                Span <byte>         output = new Span <byte>(CompressedFile.UncompressedData);
                ReadOnlySpan <byte> input  = CompressedFile.CompressedData;
                while (!input.IsEmpty && !output.IsEmpty)
                {
                    encoder.Compress(input, output, out int bytesConsumed, out int written, isFinalBlock: false);
                    input  = input.Slice(bytesConsumed);
                    output = output.Slice(written);
                }
                encoder.Compress(input, output, out int bytesConsumed2, out int written2, isFinalBlock: true);

                return(output);
            }
        }
Beispiel #4
0
        public void Compress_WithEmptySource()
        {
            byte[] sourceBytes              = new byte[0];
            byte[] destinationBytes         = new byte[100000];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(sourceBytes);
            Span <byte>         destination = new Span <byte>(destinationBytes);

            Assert.True(BrotliEncoder.TryCompress(source, destination, out int bytesWritten));
            // The only byte written should be the Brotli end of stream byte which varies based on the window/quality
            Assert.Equal(1, bytesWritten);

            BrotliEncoder encoder = default;
            var           result  = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(1, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.Done, result);
        }
Beispiel #5
0
        public void Compress_WithEmptyDestination()
        {
            string testFile = UncompressedTestFile();

            byte[] correctUncompressedBytes = File.ReadAllBytes(testFile);
            byte[] empty = new byte[0];
            ReadOnlySpan <byte> source      = new ReadOnlySpan <byte>(correctUncompressedBytes);
            Span <byte>         destination = new Span <byte>(empty);

            Assert.False(BrotliEncoder.TryCompress(source, destination, out int bytesWritten), "TryCompress completed successfully but should have failed due to too short of a destination array");
            Assert.Equal(0, bytesWritten);

            BrotliEncoder encoder = default;
            var           result  = encoder.Compress(source, destination, out int bytesConsumed, out bytesWritten, false);

            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);

            result = encoder.Compress(source, destination, out bytesConsumed, out bytesWritten, isFinalBlock: true);
            Assert.Equal(0, bytesWritten);
            Assert.Equal(0, bytesConsumed);
            Assert.Equal(OperationStatus.DestinationTooSmall, result);
        }
Beispiel #6
0
        public void Compress_Canterbury_WithState(string uncompressedFileName, CompressionLevel compressLevel)
        {
            byte[] bytes = File.ReadAllBytes(uncompressedFileName);
            ReadOnlySpan <byte> uncompressedData = new ReadOnlySpan <byte>(bytes);
            int maxCompressedSize = BrotliEncoder.GetMaxCompressedLength(bytes.Length);

            byte[] compressedDataArray = new byte[maxCompressedSize];
            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                    using (BrotliEncoder encoder = new BrotliEncoder())
                    {
                        Span <byte>         output = new Span <byte>(compressedDataArray);
                        ReadOnlySpan <byte> input  = uncompressedData;
                        while (!input.IsEmpty && !output.IsEmpty)
                        {
                            encoder.Compress(input, output, out int bytesConsumed, out int written, isFinalBlock: false);
                            input  = input.Slice(bytesConsumed);
                            output = output.Slice(written);
                        }
                        encoder.Compress(input, output, out int bytesConsumed2, out int written2, isFinalBlock: true);
                    }
            }
        }
Beispiel #7
0
 private static void Compress_WithoutState(ReadOnlySpan <byte> input, Span <byte> output)
 {
     BrotliEncoder.TryCompress(input, output, out int bytesWritten);
 }
Beispiel #8
0
 public bool Compress_WithoutState()
 => BrotliEncoder.TryCompress(CompressedFile.UncompressedData, CompressedFile.UncompressedData, out int bytesWritten, GetQuality(level), Window);