diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs index 38507d58c8..7c1cd72061 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs @@ -6,6 +6,8 @@ using System.Buffers; using System.IO; using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Memory; + namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { /// @@ -25,12 +27,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// buffer[i:j] are the buffered bytes read from the underlying /// stream that haven't yet been passed further on. /// - public byte[] Buffer; + public Buffer Buffer; /// /// Values of converted to -s /// - public int[] BufferAsInt; + public Buffer BufferAsInt; /// /// Start of bytes read @@ -48,20 +50,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// public int UnreadableBytes; - private static readonly ArrayPool BytePool = ArrayPool.Create(BufferSize, 50); - - private static readonly ArrayPool IntPool = ArrayPool.Create(BufferSize, 50); - /// /// Creates a new instance of the , and initializes it's buffer. /// + /// The to use for buffer allocations. /// The bytes created - public static Bytes Create() + public static Bytes Create(MemoryManager memoryManager) { return new Bytes { - Buffer = BytePool.Rent(BufferSize), - BufferAsInt = IntPool.Rent(BufferSize) + Buffer = memoryManager.Allocate(BufferSize), + BufferAsInt = memoryManager.Allocate(BufferSize) }; } @@ -70,11 +69,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// public void Dispose() { - if (this.Buffer != null) - { - BytePool.Return(this.Buffer); - IntPool.Return(this.BufferAsInt); - } + this.Buffer?.Dispose(); + this.BufferAsInt?.Dispose(); this.Buffer = null; this.BufferAsInt = null; @@ -244,7 +240,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder } // Fill in the rest of the buffer. - int n = inputStream.Read(this.Buffer, this.J, this.Buffer.Length - this.J); + int n = inputStream.Read(this.Buffer.Array, this.J, this.Buffer.Length - this.J); if (n == 0) { return OrigDecoderErrorCode.UnexpectedEndOfStream; diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs index 5aa0fa3097..f065092004 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs @@ -5,6 +5,8 @@ using System; using System.IO; using System.Runtime.CompilerServices; +using SixLabors.ImageSharp.Memory; + namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { /// @@ -26,12 +28,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder /// /// Initializes a new instance of the struct. /// + /// The to use for buffer allocations. /// The input /// Temporal buffer, same as - public InputProcessor(Stream inputStream, byte[] temp) + public InputProcessor(MemoryManager memoryManager, Stream inputStream, byte[] temp) { this.Bits = default(Bits); - this.Bytes = Bytes.Create(); + this.Bytes = Bytes.Create(memoryManager); this.InputStream = inputStream; this.Temp = temp; this.LastErrorCode = OrigDecoderErrorCode.NoError; @@ -155,13 +158,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder { if (this.Bytes.J - this.Bytes.I >= length) { - Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, length); + Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, length); this.Bytes.I += length; length -= length; } else { - Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I); + Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I); offset += this.Bytes.J - this.Bytes.I; length -= this.Bytes.J - this.Bytes.I; this.Bytes.I += this.Bytes.J - this.Bytes.I; diff --git a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs index 0a0ddeba43..2d34aee3b1 100644 --- a/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs @@ -210,7 +210,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort { this.MetaData = new ImageMetaData(); this.InputStream = stream; - this.InputProcessor = new InputProcessor(stream, this.Temp); + this.InputProcessor = new InputProcessor(this.configuration.MemoryManager, stream, this.Temp); // Check for the Start Of Image marker. this.InputProcessor.ReadFull(this.Temp, 0, 2);