Closed
Description
Background and motivation
When reading custom binary formats one very often needs a specific number of bytes. The existing Read()
method doesn't read a specific number of bytes, it reads as many bytes are currently available. Calling code has to handle this.
We had the same issue on Stream
and solved this by exposing Stream.ReadExactly
and Stream.ReadAtLeast
.
API Proposal
namespace System.IO;
public partial class BinaryReader
{
public virtual void ReadExactly(Span<byte> buffer);
}
API Usage
BinaryReader binaryReader = GetReader();
Span<byte> guidBytes = (Span<byte>)stackalloc byte[16];
reader.ReadExactly(guidBytes);
Guid guid = new Guid(guidBytes);
Alternative Designs
No response
Risks
No response