Skip to content

Commit c418bb0

Browse files
Revert "Remove ChunkedMemoryStream"
This reverts commit 1e58db2.
1 parent ee53532 commit c418bb0

File tree

9 files changed

+983
-36
lines changed

9 files changed

+983
-36
lines changed

src/ImageSharp/Formats/Gif/GifDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private void ReadApplicationExtension(BufferedReadStream stream)
317317
bool isXmp = this.buffer.Span.StartsWith(GifConstants.XmpApplicationIdentificationBytes);
318318
if (isXmp && !this.skipMetadata)
319319
{
320-
GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(stream);
320+
GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(stream, this.memoryAllocator);
321321
if (extension.Data.Length > 0)
322322
{
323323
this.metadata!.XmpProfile = new XmpProfile(extension.Data);

src/ImageSharp/Formats/Gif/Sections/GifXmpApplicationExtension.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.ImageSharp.IO;
45
using SixLabors.ImageSharp.Memory;
56

67
namespace SixLabors.ImageSharp.Formats.Gif;
@@ -25,10 +26,11 @@ namespace SixLabors.ImageSharp.Formats.Gif;
2526
/// Reads the XMP metadata from the specified stream.
2627
/// </summary>
2728
/// <param name="stream">The stream to read from.</param>
29+
/// <param name="allocator">The memory allocator.</param>
2830
/// <returns>The XMP metadata</returns>
29-
public static GifXmpApplicationExtension Read(Stream stream)
31+
public static GifXmpApplicationExtension Read(Stream stream, MemoryAllocator allocator)
3032
{
31-
byte[] xmpBytes = ReadXmpData(stream);
33+
byte[] xmpBytes = ReadXmpData(stream, allocator);
3234

3335
// Exclude the "magic trailer", see XMP Specification Part 3, 1.1.2 GIF
3436
int xmpLength = xmpBytes.Length - 256; // 257 - unread 0x0
@@ -69,9 +71,9 @@ public int WriteTo(Span<byte> buffer)
6971
return this.ContentLength;
7072
}
7173

72-
private static byte[] ReadXmpData(Stream stream)
74+
private static byte[] ReadXmpData(Stream stream, MemoryAllocator allocator)
7375
{
74-
using MemoryStream bytes = new();
76+
using ChunkedMemoryStream bytes = new(allocator);
7577

7678
// XMP data doesn't have a fixed length nor is there an indicator of the length.
7779
// So we simply read one byte at a time until we hit the 0x0 value at the end

src/ImageSharp/Formats/ImageDecoder.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.ImageSharp.IO;
45
using SixLabors.ImageSharp.PixelFormats;
56
using SixLabors.ImageSharp.Processing;
67

@@ -209,7 +210,7 @@ T PerformActionAndResetPosition(Stream s, long position)
209210
}
210211

211212
Configuration configuration = options.Configuration;
212-
using MemoryStream memoryStream = new();
213+
using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);
213214
stream.CopyTo(memoryStream, configuration.StreamProcessingBufferSize);
214215
memoryStream.Position = 0;
215216

@@ -265,6 +266,11 @@ Task<T> PerformActionAndResetPosition(Stream s, long position, CancellationToken
265266
return PerformActionAndResetPosition(ms, ms.Position, cancellationToken);
266267
}
267268

269+
if (stream is ChunkedMemoryStream cms)
270+
{
271+
return PerformActionAndResetPosition(cms, cms.Position, cancellationToken);
272+
}
273+
268274
return CopyToMemoryStreamAndActionAsync(options, stream, PerformActionAndResetPosition, cancellationToken);
269275
}
270276

@@ -276,11 +282,9 @@ private static async Task<T> CopyToMemoryStreamAndActionAsync<T>(
276282
{
277283
long position = stream.CanSeek ? stream.Position : 0;
278284
Configuration configuration = options.Configuration;
279-
280-
await using MemoryStream memoryStream = new();
285+
await using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);
281286
await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false);
282287
memoryStream.Position = 0;
283-
284288
return await action(memoryStream, position, cancellationToken).ConfigureAwait(false);
285289
}
286290

src/ImageSharp/Formats/ImageEncoder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using SixLabors.ImageSharp.IO;
45
using SixLabors.ImageSharp.PixelFormats;
56

67
namespace SixLabors.ImageSharp.Formats;
@@ -47,8 +48,8 @@ private void EncodeWithSeekableStream<TPixel>(Image<TPixel> image, Stream stream
4748
}
4849
else
4950
{
50-
using MemoryStream ms = new();
51-
this.Encode(image, ms, cancellationToken);
51+
using ChunkedMemoryStream ms = new(configuration.MemoryAllocator);
52+
this.Encode(image, stream, cancellationToken);
5253
ms.Position = 0;
5354
ms.CopyTo(stream, configuration.StreamProcessingBufferSize);
5455
}
@@ -64,7 +65,7 @@ private async Task EncodeWithSeekableStreamAsync<TPixel>(Image<TPixel> image, St
6465
}
6566
else
6667
{
67-
await using MemoryStream ms = new();
68+
using ChunkedMemoryStream ms = new(configuration.MemoryAllocator);
6869
await DoEncodeAsync(ms);
6970
ms.Position = 0;
7071
await ms.CopyToAsync(stream, configuration.StreamProcessingBufferSize, cancellationToken)

0 commit comments

Comments
 (0)