private static void Decompress_WithState(ReadOnlySpan <byte> input, Span <byte> output) { BrotliDecoder decoder = default; while (!input.IsEmpty && !output.IsEmpty) { decoder.Decompress(input, output, out int bytesConsumed, out int written); input = input.Slice(bytesConsumed); output = output.Slice(written); } }
public void ReadFully(string testFile) { byte[] correctUncompressedBytes = File.ReadAllBytes(testFile); byte[] compressedBytes = File.ReadAllBytes(CompressedTestFile(testFile)); byte[] actualUncompressedBytes = new byte[correctUncompressedBytes.Length + 10000]; ReadOnlySpan <byte> source = new ReadOnlySpan <byte>(compressedBytes); Span <byte> destination = new Span <byte>(actualUncompressedBytes); Assert.True(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress did not complete successfully"); Assert.Equal(correctUncompressedBytes.Length, bytesWritten); Assert.Equal <byte>(correctUncompressedBytes, actualUncompressedBytes.AsSpan(0, correctUncompressedBytes.Length).ToArray()); }
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()); }
public Span <byte> Decompress_WithState() // the level argument is not used here, but it describes how the data was compressed (in the benchmark id) { using (BrotliDecoder decoder = new BrotliDecoder()) { Span <byte> output = new Span <byte>(CompressedFile.UncompressedData); ReadOnlySpan <byte> input = CompressedFile.CompressedData; while (!input.IsEmpty && !output.IsEmpty) { decoder.Decompress(input, output, out int bytesConsumed, out int written); input = input.Slice(bytesConsumed); output = output.Slice(written); } return(output); } }
public void Decompress_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.False(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress completed successfully but should have failed due to too short of a source array"); Assert.Equal(0, bytesWritten); BrotliDecoder decoder = default; var result = decoder.Decompress(source, destination, out int bytesConsumed, out bytesWritten); Assert.Equal(0, bytesWritten); Assert.Equal(0, bytesConsumed); Assert.Equal(OperationStatus.NeedMoreData, result); }
public void Decompress_WithEmptyDestination() { string testFile = UncompressedTestFile(); byte[] sourceBytes = File.ReadAllBytes(CompressedTestFile(testFile)); byte[] destinationBytes = new byte[0]; ReadOnlySpan <byte> source = new ReadOnlySpan <byte>(sourceBytes); Span <byte> destination = new Span <byte>(destinationBytes); Assert.False(BrotliDecoder.TryDecompress(source, destination, out int bytesWritten), "TryDecompress completed successfully but should have failed due to too short of a destination array"); Assert.Equal(0, bytesWritten); BrotliDecoder decoder = default; var result = decoder.Decompress(source, destination, out int bytesConsumed, out bytesWritten); Assert.Equal(0, bytesWritten); Assert.Equal(0, bytesConsumed); Assert.Equal(OperationStatus.DestinationTooSmall, result); }
public void Decompress_Canterbury_WithoutState(int innerIterations, string uncompressedFileName) { byte[] compressedBytes = File.ReadAllBytes(CompressedTestFile(uncompressedFileName)); ReadOnlySpan <byte> compressedData = new ReadOnlySpan <byte>(compressedBytes); int uncompressedSize = (int)new FileInfo(uncompressedFileName).Length; List <byte[]> uncompressedDataArrays = new List <byte[]>(innerIterations); foreach (var iteration in Benchmark.Iterations) { for (int i = 0; i < innerIterations; i++) { uncompressedDataArrays.Add(new byte[uncompressedSize]); } using (iteration.StartMeasurement()) { for (int i = 0; i < innerIterations; i++) { Assert.True(BrotliDecoder.TryDecompress(compressedData, uncompressedDataArrays[i], out int bytesWritten)); } } } }
private static void Decompress_WithoutState(ReadOnlySpan <byte> input, Span <byte> output) { BrotliDecoder.TryDecompress(input, output, out int bytesWritten); }