diff --git a/src/DotNext.IO/IO/PoolingBufferedStream.cs b/src/DotNext.IO/IO/PoolingBufferedStream.cs index ae2b74b99..6ca40bcee 100644 --- a/src/DotNext.IO/IO/PoolingBufferedStream.cs +++ b/src/DotNext.IO/IO/PoolingBufferedStream.cs @@ -489,21 +489,24 @@ private int ReadCore(Span data) if (data.IsEmpty) { - // nothing to do + if (readPosition == readLength) + Reset(); } else if (data.Length > maxBufferSize) { + Debug.Assert(readPosition == readLength); + bytesRead += stream.Read(data); + Reset(); } else { + Debug.Assert(readPosition == readLength); + readPosition = 0; readLength = stream.Read(EnsureBufferAllocated().Span); bytesRead += ReadFromBuffer(data); } - - if (readPosition == readLength) - Reset(); return bytesRead; } @@ -564,12 +567,13 @@ private async ValueTask ReadCoreAsync(Memory data, CancellationToken if (data.IsEmpty) { - // nothing to do + if (readPosition == readLength) + Reset(); } - else if (data.Length > MaxBufferSize) + else if (data.Length >= maxBufferSize) { - Debug.Assert(readPosition == readLength); bytesRead += await stream.ReadAsync(data, token).ConfigureAwait(false); + Reset(); } else { @@ -578,9 +582,6 @@ private async ValueTask ReadCoreAsync(Memory data, CancellationToken readLength = await stream.ReadAsync(EnsureBufferAllocated().Memory, token).ConfigureAwait(false); bytesRead += ReadFromBuffer(data.Span); } - - if (readPosition == readLength) - Reset(); return bytesRead; }