From a0e38c87b01af9c47101947580870f09513ed151 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 11 May 2022 14:59:50 +0200 Subject: [PATCH] Use memory stream for uncompressed data instead of a list --- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 16971b5b86..f46b5058a1 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -1223,26 +1223,26 @@ namespace SixLabors.ImageSharp.Formats.Png fixed (byte* compressedDataBase = compressedData) { using (IMemoryOwner destBuffer = this.memoryAllocator.Allocate(this.Configuration.StreamProcessingBufferSize)) - using (var memoryStream = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length)) - using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStream)) + using (var memoryStreamOutput = new MemoryStream(compressedData.Length)) + using (var memoryStreamInput = new UnmanagedMemoryStream(compressedDataBase, compressedData.Length)) + using (var bufferedStream = new BufferedReadStream(this.Configuration, memoryStreamInput)) using (var inflateStream = new ZlibInflateStream(bufferedStream)) { - Span dest = destBuffer.GetSpan(); + Span destUncompressedData = destBuffer.GetSpan(); if (!inflateStream.AllocateNewBytes(compressedData.Length, false)) { uncompressedBytesArray = Array.Empty(); return false; } - var uncompressedBytes = new List(compressedData.Length); - int bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length); + int bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); while (bytesRead != 0) { - uncompressedBytes.AddRange(dest.Slice(0, bytesRead).ToArray()); - bytesRead = inflateStream.CompressedStream.Read(dest, 0, dest.Length); + memoryStreamOutput.Write(destUncompressedData.Slice(0, bytesRead)); + bytesRead = inflateStream.CompressedStream.Read(destUncompressedData, 0, destUncompressedData.Length); } - uncompressedBytesArray = uncompressedBytes.ToArray(); + uncompressedBytesArray = memoryStreamOutput.ToArray(); return true; } }