Skip to content

Instantly share code, notes, and snippets.

@Jozkee
Last active November 16, 2022 15:13
Show Gist options
  • Save Jozkee/aee5a36d7d1238a320acb366fa08539d to your computer and use it in GitHub Desktop.
Save Jozkee/aee5a36d7d1238a320acb366fa08539d to your computer and use it in GitHub Desktop.
Make Stream easier to implement
public abstract class Stream2022 : Stream
{
public sealed override int Read(byte[] buffer, int offset, int count)
=> Read(buffer.AsSpan(offset, count));
public abstract override int Read(Span<byte> buffer);
public sealed override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> ReadAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
public abstract override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default);
public sealed override void Write(byte[] buffer, int offset, int count)
=> Write(buffer.AsSpan(offset, count));
public abstract override void Write(ReadOnlySpan<byte> buffer);
public sealed override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> WriteAsync(buffer.AsMemory(offset, count), cancellationToken).AsTask();
public abstract override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment