Skip to content

Commit d4f98cc

Browse files
committed
Refactor chunked stream reading logic for accuracy
Refactored the chunking logic in ChunkedStream to use nested loops, ensuring each yielded chunk is exactly _chunkSize unless at EOF. Improved resource management by returning buffers in a finally block and removed redundant code.
1 parent 04afdf5 commit d4f98cc

1 file changed

Lines changed: 33 additions & 7 deletions

File tree

Sources/EasyExtensions/Streams/ChunkedStream.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,42 @@ public IEnumerable<Stream> GetChunks()
5252
{
5353
throw new ObjectDisposedException(nameof(ChunkedStream));
5454
}
55+
5556
byte[] buffer = ArrayPool<byte>.Shared.Rent(_chunkSize);
56-
int bytesRead;
57-
while ((bytesRead = _baseStream.Read(buffer, 0, _chunkSize)) > 0)
57+
try
58+
{
59+
while (true)
60+
{
61+
var chunkStream = new MemoryStream(_chunkSize);
62+
int totalInChunk = 0;
63+
64+
while (totalInChunk < _chunkSize)
65+
{
66+
int toRead = Math.Min(_chunkSize - totalInChunk, _chunkSize);
67+
int bytesRead = _baseStream.Read(buffer, 0, toRead);
68+
69+
if (bytesRead <= 0)
70+
{
71+
break; // EOF
72+
}
73+
74+
chunkStream.Write(buffer, 0, bytesRead);
75+
totalInChunk += bytesRead;
76+
}
77+
78+
if (totalInChunk == 0)
79+
{
80+
yield break;
81+
}
82+
83+
chunkStream.Position = 0;
84+
yield return chunkStream;
85+
}
86+
}
87+
finally
5888
{
59-
MemoryStream chunkStream = new MemoryStream();
60-
chunkStream.Write(buffer, 0, bytesRead);
61-
chunkStream.Position = 0;
62-
yield return chunkStream;
89+
ArrayPool<byte>.Shared.Return(buffer);
6390
}
64-
ArrayPool<byte>.Shared.Return(buffer);
6591
}
6692

6793
/// <summary>

0 commit comments

Comments
 (0)