Skip to content

Commit

Permalink
Use memory stream for uncompressed data instead of a list
Browse files Browse the repository at this point in the history
  • Loading branch information
brianpopow committed May 11, 2022
1 parent d43ec49 commit a0e38c8
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/ImageSharp/Formats/Png/PngDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,26 +1223,26 @@ private unsafe bool TryUncompressZlibData(ReadOnlySpan<byte> compressedData, out
fixed (byte* compressedDataBase = compressedData)
{
using (IMemoryOwner<byte> destBuffer = this.memoryAllocator.Allocate<byte>(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<byte> dest = destBuffer.GetSpan();
Span<byte> destUncompressedData = destBuffer.GetSpan();
if (!inflateStream.AllocateNewBytes(compressedData.Length, false))
{
uncompressedBytesArray = Array.Empty<byte>();
return false;
}

var uncompressedBytes = new List<byte>(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;
}
}
Expand Down

0 comments on commit a0e38c8

Please sign in to comment.