Browse Source

Simplify position checks.

pull/2301/head
James Jackson-South 4 years ago
parent
commit
32965d38b9
  1. 5
      src/ImageSharp/Formats/ImageDecoder.cs
  2. 36
      src/ImageSharp/Image.FromStream.cs

5
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)
{

36
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);
}
/// <summary>
@ -583,20 +569,6 @@ public abstract partial class Image
throw new NotSupportedException("Cannot read from the stream.");
}
async Task<T> 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]

Loading…
Cancel
Save