first commit

This commit is contained in:
2023-12-14 21:48:48 +04:00
commit ef5d53a67f
22 changed files with 1387 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
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)
{
do
{
var readed = stream.Read(buffer);
if (readed == 0)
{
throw new EndOfStreamException();
}
buffer = buffer[readed..];
}
while (buffer.Length > 0);
}
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)
{
do
{
cancellationToken.ThrowIfCancellationRequested();
var readed = await stream.ReadAsync(buffer, cancellationToken);
if (readed == 0)
{
throw new EndOfStreamException();
}
buffer = buffer[readed..];
}
while (buffer.Length > 0);
}
}