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

Closed
terrajobst opened this issue Apr 26, 2024 · 9 comments · Fixed by #106238
Closed

[API Proposal]: BinaryReader.ReadExactly #101614

terrajobst opened this issue Apr 26, 2024 · 9 comments · Fixed by #106238
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.IO good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@terrajobst
Copy link
Member

terrajobst commented Apr 26, 2024

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

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

@adamsitnik adamsitnik added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner labels Jun 25, 2024
@adamsitnik adamsitnik added this to the Future milestone Jun 25, 2024
@adamsitnik
Copy link
Member

The proposal LGTM, I've marked it as ready for review.

@terrajobst
Copy link
Member Author

terrajobst commented Aug 6, 2024

Video

  • Looks good as proposed
namespace System.IO;

public partial class BinaryReader
{
    public virtual void ReadExactly(Span<byte> buffer);
}

@terrajobst terrajobst added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Aug 6, 2024
@paulbartrum
Copy link

public partial class BinaryReader
{
    public virtual ReadExactly(Span<byte> buffer);
}

The method return type is missing (I'm assuming it should be void?)

@terrajobst
Copy link
Member Author

LOL. Look, I only work here. Fixed.

@adamsitnik adamsitnik added good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors labels Aug 7, 2024
@adamsitnik
Copy link
Member

Logically yes, but practically the challenge is that the reader has its own buffer.

It used to have it's own buffer, after #80331 it does not.

I've marked this issue as "help wanted" and "good first issue" to attract some new contributors (any .NET Team member who whishes to contribute please wait for a PR from community and review it, it's the best way you can help).

The person who wishes to work on this issue should let others know here, so there are no multiple contributors working on the same problem.

Here are some tips that you may find useful:

  • here you can find our workflow instructions, the first step is to install everything required to build the product
  • to build the clr, libs and tests you need to build following subsets:
.\build.cmd -c Release -subset clr+libs+libs.tests
.\build.cmd -c Release -subset clr.corelib+clr.nativecorelib+libs.PreTest && .\dotnet.cmd build -c Release .\src\libraries\System.Runtime\System.Runtime.sln && .\dotnet.cmd build -c Release .\src\libraries\System.Runtime\tests\System.IO.Tests\System.IO.Tests.csproj /t:Test

Good luck!

@adamsitnik adamsitnik modified the milestones: Future, 10.0.0 Aug 8, 2024
@Log234
Copy link
Contributor

Log234 commented Aug 10, 2024

I would like to attempt providing an implementation for this proposal, if I may have the chance.

Log234 added a commit to Log234/dotnet-runtime that referenced this issue Aug 11, 2024
Implements the BinaryReader.ReadExactly method, as proposed in dotnet#101614

Fix dotnet#101614
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 11, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 20, 2024
mikelle-rogers pushed a commit to mikelle-rogers/runtime that referenced this issue Dec 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.IO good first issue Issue should be easy to implement, good for first-time contributors help wanted [up-for-grabs] Good issue for external contributors in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants