Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API Proposal]: BinaryReader.ReadExactly #101614

Open
terrajobst opened this issue Apr 26, 2024 · 3 comments
Open

[API Proposal]: BinaryReader.ReadExactly #101614

terrajobst opened this issue Apr 26, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO untriaged New issue has not been triaged by the area owner

Comments

@terrajobst
Copy link
Member

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 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

@terrajobst terrajobst added api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO labels Apr 26, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 26, 2024
@terrajobst terrajobst changed the title [API Proposal]: BinaryWriter.ReadExactly [API Proposal]: BinaryReader.ReadExactly Apr 26, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@PaulusParssinen
Copy link
Contributor

PaulusParssinen commented Apr 27, 2024

This would be equivalent to calling ReadExactly(Span<byte>) on the BinaryReader.BaseStream property, right? This method would be much nicer/accessible though.

@terrajobst
Copy link
Member Author

Logically yes, but practically the challenge is that the reader has its own buffer. By passing that would be problematic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.IO untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants