Skip to content

Instantly share code, notes, and snippets.

@neuecc
Created December 23, 2021 03:54
Show Gist options
  • Select an option

  • Save neuecc/21b98b71074828e246d384eb2568a559 to your computer and use it in GitHub Desktop.

Select an option

Save neuecc/21b98b71074828e246d384eb2568a559 to your computer and use it in GitHub Desktop.
public static async Task ReadFromAsync(NativeMemoryArray<byte> buffer, Stream stream, CancellationToken cancellationToken = default)
{
var writer = buffer.CreateBufferWriter();
int read;
while ((read = await stream.ReadAsync(writer.GetMemory(), cancellationToken).ConfigureAwait(false)) != 0)
{
writer.Advance(read);
}
}
public static async Task WriteToAsync(NativeMemoryArray<byte> buffer, Stream stream, CancellationToken cancellationToken = default)
{
foreach (var item in buffer.AsMemorySequence())
{
await stream.WriteAsync(item, cancellationToken);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment