-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
27 lines (23 loc) · 851 Bytes
/
Copy pathStreamExtensions.cs
File metadata and controls
27 lines (23 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace ExcelDataReader;
internal static class StreamExtensions
{
public static int ReadAtLeast(this Stream stream, byte[] buffer, int offset, int minimumBytes)
{
#if NET8_0_OR_GREATER
// Delegate to the BCL overload (Stream.ReadAtLeast added in .NET 7).
return stream.ReadAtLeast(buffer.AsSpan(offset, minimumBytes), minimumBytes, throwOnEndOfStream: false);
#else
if (minimumBytes < 0 || buffer.Length < offset + minimumBytes)
throw new ArgumentOutOfRangeException(nameof(minimumBytes));
int totalRead = 0;
while (totalRead < minimumBytes)
{
int read = stream.Read(buffer, offset + totalRead, minimumBytes - totalRead);
if (read == 0)
return totalRead;
totalRead += read;
}
return totalRead;
#endif
}
}