diff --git a/src/ImageSharp/IO/BufferedReadStream.cs b/src/ImageSharp/IO/BufferedReadStream.cs index 9200dbf7f..efa8f6f4b 100644 --- a/src/ImageSharp/IO/BufferedReadStream.cs +++ b/src/ImageSharp/IO/BufferedReadStream.cs @@ -90,6 +90,7 @@ internal sealed class BufferedReadStream : Stream set { Guard.MustBeGreaterThanOrEqualTo(value, 0, nameof(this.Position)); + this.cancellationToken.ThrowIfCancellationRequested(); // Only reset readBufferIndex if we are out of bounds of our working buffer // otherwise we should simply move the value by the diff. @@ -262,6 +263,7 @@ internal sealed class BufferedReadStream : Stream [MethodImpl(MethodImplOptions.NoInlining)] private void FillReadBuffer() { + this.cancellationToken.ThrowIfCancellationRequested(); Stream baseStream = this.BaseStream; if (this.readerPosition != baseStream.Position) { diff --git a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs index 08c0d0004..b8a915ef3 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.Decode_Cancellation.cs @@ -86,7 +86,7 @@ public partial class ImageTests Configuration configuration = Configuration.CreateDefaultInstance(); configuration.FileSystem = new SingleStreamFileSystem((Stream)pausedStream); - configuration.StreamProcessingBufferSize = 256; + configuration.StreamProcessingBufferSize = (int)Math.Min(128, pausedStream.Length / 4); DecoderOptions options = new() { diff --git a/tests/ImageSharp.Tests/TestUtilities/IPausedStream.cs b/tests/ImageSharp.Tests/TestUtilities/IPausedStream.cs index 1eedef9d0..ec9b2e7e1 100644 --- a/tests/ImageSharp.Tests/TestUtilities/IPausedStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/IPausedStream.cs @@ -12,4 +12,6 @@ public interface IPausedStream : IDisposable public void Next(); public void Release(); + + public long Length { get; } } diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs index 5b285b4ca..ae4af24f1 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedMemoryStream.cs @@ -12,9 +12,9 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities; /// public class PausedMemoryStream : MemoryStream, IPausedStream { - private readonly SemaphoreSlim semaphore = new SemaphoreSlim(0); + private readonly SemaphoreSlim semaphore = new(0); - private readonly CancellationTokenSource cancelationTokenSource = new CancellationTokenSource(); + private readonly CancellationTokenSource cancelationTokenSource = new(); private Action onWaitingCallback; diff --git a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs index 8af168d67..3c780f347 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PausedStream.cs @@ -7,9 +7,9 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities; public class PausedStream : Stream, IPausedStream { - private readonly SemaphoreSlim semaphore = new SemaphoreSlim(0); + private readonly SemaphoreSlim semaphore = new(0); - private readonly CancellationTokenSource cancelationTokenSource = new CancellationTokenSource(); + private readonly CancellationTokenSource cancelationTokenSource = new(); private readonly Stream innerStream; private Action onWaitingCallback;