From 32965d38b96aab85adf42464a24f5997dbf4cf00 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 22 Nov 2022 22:23:26 +1000 Subject: [PATCH] Simplify position checks. --- src/ImageSharp/Formats/ImageDecoder.cs | 5 ++-- src/ImageSharp/Image.FromStream.cs | 36 +++----------------------- 2 files changed, 7 insertions(+), 34 deletions(-) diff --git a/src/ImageSharp/Formats/ImageDecoder.cs b/src/ImageSharp/Formats/ImageDecoder.cs index 9e78602315..1c7e95693f 100644 --- a/src/ImageSharp/Formats/ImageDecoder.cs +++ b/src/ImageSharp/Formats/ImageDecoder.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.IO; using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; @@ -157,7 +158,7 @@ public abstract class ImageDecoder : IImageDecoder { T result = action(s); - // Our buffered reads may have left the stream in an incorrect non-zero position. + // Issue #2259. Our buffered reads may have left the stream in an incorrect non-zero position. // Reset the position of the seekable stream if we did not read to the end to allow additional reads. if (stream.CanSeek && stream.Position != s.Position && s.Position != s.Length) { @@ -198,7 +199,7 @@ public abstract class ImageDecoder : IImageDecoder { T result = action(s, ct); - // Our buffered reads may have left the stream in an incorrect non-zero position. + // Issue #2259. Our buffered reads may have left the stream in an incorrect non-zero position. // Reset the position of the seekable stream if we did not read to the end to allow additional reads. if (stream.CanSeek && stream.Position != s.Position && s.Position != s.Length) { diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs index 19d85cafb6..33653434d5 100644 --- a/src/ImageSharp/Image.FromStream.cs +++ b/src/ImageSharp/Image.FromStream.cs @@ -527,20 +527,6 @@ public abstract partial class Image throw new NotSupportedException("Cannot read from the stream."); } - T Action(Stream s, long position) - { - T result = action(s); - - // Our buffered reads may have left the stream in an incorrect non-zero position. - // Reset the position of the seekable stream if we did not read to the end to allow additional reads. - if (stream.CanSeek && stream.Position != s.Position && s.Position != s.Length) - { - stream.Position = position + s.Position; - } - - return result; - } - Configuration configuration = options.Configuration; if (stream.CanSeek) { @@ -549,14 +535,14 @@ public abstract partial class Image stream.Position = 0; } - return Action(stream, stream.Position); + return action(stream); } using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator); stream.CopyTo(memoryStream, configuration.StreamProcessingBufferSize); memoryStream.Position = 0; - return Action(memoryStream, 0); + return action(memoryStream); } /// @@ -583,20 +569,6 @@ public abstract partial class Image throw new NotSupportedException("Cannot read from the stream."); } - async Task Action(Stream s, long position, CancellationToken ct) - { - T result = await action(s, ct).ConfigureAwait(false); - - // Our buffered reads may have left the stream in an incorrect non-zero position. - // Reset the position of the seekable stream if we did not read to the end to allow additional reads. - if (stream.CanSeek && stream.Position != s.Position && s.Position != s.Length) - { - stream.Position = position + s.Position; - } - - return result; - } - Configuration configuration = options.Configuration; if (stream.CanSeek) { @@ -605,14 +577,14 @@ public abstract partial class Image stream.Position = 0; } - return await Action(stream, stream.Position, cancellationToken).ConfigureAwait(false); + return await action(stream, cancellationToken).ConfigureAwait(false); } using ChunkedMemoryStream memoryStream = new(configuration.MemoryAllocator); await stream.CopyToAsync(memoryStream, configuration.StreamProcessingBufferSize, cancellationToken).ConfigureAwait(false); memoryStream.Position = 0; - return await Action(memoryStream, 0, cancellationToken).ConfigureAwait(false); + return await action(memoryStream, cancellationToken).ConfigureAwait(false); } [DoesNotReturn]