Skip to content

Commit bfad8dc

Browse files
committed
[fix] handle partial reads in StaticFile.GetDataAsync and add NET7_0_OR_GREATER branch using ReadExactlyAsync
1 parent 74d7a74 commit bfad8dc

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/Simplify.Web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
- Removed .NET 6 Support
1212

13+
### Changed
14+
15+
- Handle partial reads in StaticFile.GetDataAsync and add NET7_0_OR_GREATER branch using ReadExactlyAsync
16+
1317
### Dependencies
1418

1519
- Microsoft.Extensions.Configuration.Binder bump to 10.0.9

src/Simplify.Web/StaticFiles/IO/StaticFile.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ public async Task<byte[]> GetDataAsync(string relativeFilePath)
5858
var result = new byte[stream.Length];
5959

6060
await stream.ReadAsync(result, 0, (int)stream.Length);
61+
62+
#elif NET7_0_OR_GREATER
63+
await using var stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
64+
65+
var result = new byte[stream.Length];
66+
67+
await stream.ReadExactlyAsync(result);
6168
#else
6269
await using var stream = File.Open(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);
6370

6471
var result = new byte[stream.Length];
72+
var totalBytesRead = 0;
6573

66-
await stream.ReadAsync(result.AsMemory(0, (int)stream.Length));
74+
while (totalBytesRead < result.Length)
75+
totalBytesRead += await stream.ReadAsync(result.AsMemory(totalBytesRead, result.Length - totalBytesRead));
6776
#endif
6877

6978
return result;

0 commit comments

Comments
 (0)