Skip to content

Commit

Permalink
Remove ChunkedMemoryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants committed Oct 22, 2024
1 parent e69a507 commit 1e58db2
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 983 deletions.
2 changes: 1 addition & 1 deletion src/ImageSharp/Formats/Gif/GifDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private void ReadApplicationExtension(BufferedReadStream stream)
bool isXmp = this.buffer.Span.StartsWith(GifConstants.XmpApplicationIdentificationBytes);
if (isXmp && !this.skipMetadata)
{
GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(stream, this.memoryAllocator);
GifXmpApplicationExtension extension = GifXmpApplicationExtension.Read(stream);
if (extension.Data.Length > 0)
{
this.metadata!.XmpProfile = new XmpProfile(extension.Data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory;

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

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

private static byte[] ReadXmpData(Stream stream, MemoryAllocator allocator)
private static byte[] ReadXmpData(Stream stream)
{
using ChunkedMemoryStream bytes = new(allocator);
using MemoryStream bytes = new();

// XMP data doesn't have a fixed length nor is there an indicator of the length.
// So we simply read one byte at a time until we hit the 0x0 value at the end
Expand Down
12 changes: 4 additions & 8 deletions src/ImageSharp/Formats/ImageDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;

Expand Down Expand Up @@ -210,7 +209,7 @@ T PerformActionAndResetPosition(Stream s, long position)
}

Configuration configuration = options.Configuration;
using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);
using MemoryStream memoryStream = new();
stream.CopyTo(memoryStream, configuration.StreamProcessingBufferSize);
memoryStream.Position = 0;

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

if (stream is ChunkedMemoryStream cms)
{
return PerformActionAndResetPosition(cms, cms.Position, cancellationToken);
}

return CopyToMemoryStreamAndActionAsync(options, stream, PerformActionAndResetPosition, cancellationToken);
}

Expand All @@ -282,9 +276,11 @@ private static async Task<T> CopyToMemoryStreamAndActionAsync<T>(
{
long position = stream.CanSeek ? stream.Position : 0;
Configuration configuration = options.Configuration;
await using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator);

await using MemoryStream memoryStream = new();
await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false);
memoryStream.Position = 0;

return await action(memoryStream, position, cancellationToken).ConfigureAwait(false);
}

Expand Down
7 changes: 3 additions & 4 deletions src/ImageSharp/Formats/ImageEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.PixelFormats;

namespace SixLabors.ImageSharp.Formats;
Expand Down Expand Up @@ -48,8 +47,8 @@ private void EncodeWithSeekableStream<TPixel>(Image<TPixel> image, Stream stream
}
else
{
using ChunkedMemoryStream ms = new(configuration.MemoryAllocator);
this.Encode(image, stream, cancellationToken);
using MemoryStream ms = new();
this.Encode(image, ms, cancellationToken);
ms.Position = 0;
ms.CopyTo(stream, configuration.StreamProcessingBufferSize);
}
Expand All @@ -65,7 +64,7 @@ private async Task EncodeWithSeekableStreamAsync<TPixel>(Image<TPixel> image, St
}
else
{
using ChunkedMemoryStream ms = new(configuration.MemoryAllocator);
await using MemoryStream ms = new();
await DoEncodeAsync(ms);
ms.Position = 0;
await ms.CopyToAsync(stream, configuration.StreamProcessingBufferSize, cancellationToken)
Expand Down
Loading

0 comments on commit 1e58db2

Please sign in to comment.