Files
Just.Core/Core/Extensions/SystemIOStreamExtensions.cs
JustFixMe 2b68ba982d
All checks were successful
.NET Test / test (push) Successful in 1m6s
.NET Publish / publish (push) Successful in 38s
added more tests
2024-08-15 22:00:49 +04:00

45 lines
1.6 KiB
C#

namespace Just.Core.Extensions;
public static class SystemIOStreamExtensions
{
public static void Populate(this Stream stream, byte[] buffer, int offset, int length)
=> stream.Populate(buffer.AsSpan(offset, length));
public static void Populate(this Stream stream, byte[] buffer)
=> stream.Populate(buffer.AsSpan());
public static void Populate(this Stream stream, Span<byte> buffer)
{
while (buffer.Length > 0)
{
var readed = stream.Read(buffer);
if (readed == 0)
{
throw new EndOfStreamException();
}
buffer = buffer[readed..];
}
}
public static async ValueTask PopulateAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken = default)
=> await stream.PopulateAsync(buffer.AsMemory(), cancellationToken);
public static async ValueTask PopulateAsync(this Stream stream, byte[] buffer, int offset, int length, CancellationToken cancellationToken = default)
=> await stream.PopulateAsync(buffer.AsMemory(offset, length), cancellationToken);
public static async ValueTask PopulateAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
{
while (buffer.Length > 0)
{
cancellationToken.ThrowIfCancellationRequested();
var readed = await stream.ReadAsync(buffer, cancellationToken);
if (readed == 0)
{
throw new EndOfStreamException();
}
buffer = buffer[readed..];
}
}
}