diff --git a/src/DotNext.IO/IO/PoolingBufferedStream.cs b/src/DotNext.IO/IO/PoolingBufferedStream.cs index 52c42cc2d..bff81c3c0 100644 --- a/src/DotNext.IO/IO/PoolingBufferedStream.cs +++ b/src/DotNext.IO/IO/PoolingBufferedStream.cs @@ -674,13 +674,13 @@ public override IAsyncResult BeginRead(byte[] data, int offset, int count, Async public override Task FlushAsync(CancellationToken token) { Task task; - if (writePosition > 0) + if (stream is null) { - task = WriteAndFlushAsync(token); + task = DisposedTask; } - else if (stream is null) + else if (writePosition > 0) { - task = DisposedTask; + task = WriteAndFlushAsync(token); } else { @@ -697,11 +697,10 @@ public override Task FlushAsync(CancellationToken token) private async Task WriteAndFlushAsync(CancellationToken token) { + Debug.Assert(stream is not null); Debug.Assert(writePosition > 0); Debug.Assert(buffer.Length > 0); - ThrowIfDisposed(); - await stream.WriteAsync(WrittenMemory, token).ConfigureAwait(false); await stream.FlushAsync(token).ConfigureAwait(false);