Skip to content

Add endianness-aware BinaryReader/BinaryWriter extensions; bump PolyShim to 2.12.1 - #91

Merged
Tyrrrz merged 8 commits into
primefrom
copilot/add-binaryreader-binarywriter-extensions
Jul 20, 2026
Merged

Add endianness-aware BinaryReader/BinaryWriter extensions; bump PolyShim to 2.12.1#91
Tyrrrz merged 8 commits into
primefrom
copilot/add-binaryreader-binarywriter-extensions

Conversation

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Adds explicit big-endian and little-endian read/write extension methods for BinaryReader and BinaryWriter backed by System.Buffers.Binary.BinaryPrimitives. Also updates PolyShim to 2.12.2, which introduces the BinaryPrimitives polyfill for older target frameworks.

Changes

  • Directory.Packages.props — PolyShim 2.11.02.12.1
  • BinaryReaderExtensions — 16 new methods: ReadInt16/UInt16/Int32/UInt32/Int64/UInt64/Single/DoubleBigEndian|LittleEndian
  • BinaryWriterExtensions — 16 matching WriteInt16/UInt16/Int32/UInt32/Int64/UInt64/Single/DoubleBigEndian|LittleEndian methods
  • Float/double methods are gated on #if NET5_0_OR_GREATER || !FEATURE_MEMORYBinaryPrimitives overloads for float/double were added in .NET 5 and aren't present in System.Memory 4.6.3 (used by net462/netstandard2.0); they are available natively on .NET 5+ and via PolyShim on net35

Usage

// Read a big-endian int from a stream regardless of host byte order
using var reader = new BinaryReader(stream);
int magic = reader.ReadInt32BigEndian();

// Write a little-endian ulong
using var writer = new BinaryWriter(stream);
writer.WriteUInt64LittleEndian(0xDEADBEEFCAFEBABEuL);

Comment thread Directory.Packages.props
@Tyrrrz
Tyrrrz marked this pull request as ready for review July 20, 2026 18:52
Copilot AI review requested due to automatic review settings July 20, 2026 18:52
@codecov

codecov Bot commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.61905% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.13%. Comparing base (560c9ce) to head (a934c52).

Files with missing lines Patch % Lines
PowerKit/Extensions/BinaryReaderExtensions.cs 90.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            prime      #91      +/-   ##
==========================================
+ Coverage   86.43%   87.13%   +0.69%     
==========================================
  Files          75       75              
  Lines        1268     1352      +84     
  Branches      235      236       +1     
==========================================
+ Hits         1096     1178      +82     
- Misses        121      122       +1     
- Partials       51       52       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI requested a review from Tyrrrz July 20, 2026 18:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds explicit endianness-aware read/write extension methods for BinaryReader/BinaryWriter using System.Buffers.Binary.BinaryPrimitives, and bumps PolyShim to support older target frameworks via a BinaryPrimitives polyfill.

Changes:

  • Add Read*BigEndian/Read*LittleEndian methods for integers (and conditionally for float/double) on BinaryReader.
  • Add matching Write*BigEndian/Write*LittleEndian methods on BinaryWriter.
  • Add comprehensive unit tests for the new APIs and bump PolyShim from 2.11.0 to 2.12.1.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
PowerKit/Extensions/BinaryWriterExtensions.cs Adds big-/little-endian primitive write helpers using BinaryPrimitives.
PowerKit/Extensions/BinaryReaderExtensions.cs Adds big-/little-endian primitive read helpers using BinaryPrimitives.
PowerKit.Tests/Extensions/BinaryWriterExtensionsTests.cs Adds byte-level assertions for all new writer methods.
PowerKit.Tests/Extensions/BinaryReaderExtensionsTests.cs Adds value-level assertions for all new reader methods.
Directory.Packages.props Updates PolyShim dependency to 2.12.1.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread PowerKit/Extensions/BinaryReaderExtensions.cs
Comment thread PowerKit/Extensions/BinaryReaderExtensions.cs Outdated
Comment thread PowerKit/Extensions/BinaryWriterExtensions.cs Outdated
@Tyrrrz

Tyrrrz commented Jul 20, 2026

Copy link
Copy Markdown
Owner

@copilot if you update PolyShim to 2.12.2, you can drop the #if guards on the float/double overloads

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

@copilot if you update PolyShim to 2.12.2, you can drop the #if guards on the float/double overloads

Done — bumped PolyShim to 2.12.2 and removed the #if guards from both BinaryReaderExtensions and BinaryWriterExtensions. The float/double overloads now compile unconditionally across all TFMs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (4)

PowerKit/Extensions/BinaryReaderExtensions.cs:154

  • These float read helpers call BinaryPrimitives.ReadSingle*Endian. This repo targets netstandard2.0/net462 (see PowerKit.csproj), and the PR description notes these float/double BinaryPrimitives overloads aren’t available on all target frameworks (e.g., when using System.Memory 4.6.3). Consider implementing float/double endianness via BitConverter + conditional Array.Reverse so the methods compile consistently across TFMs.
        /// <summary>
        /// Reads a 4-byte floating-point value from the stream using big-endian byte order.
        /// </summary>
        public float ReadSingleBigEndian() =>
            BinaryPrimitives.ReadSingleBigEndian(ReadExactBytes(reader, sizeof(float)));

PowerKit/Extensions/BinaryReaderExtensions.cs:166

  • Same concern as ReadSingleEndian: BinaryPrimitives.ReadDoubleEndian isn’t uniformly available across all TFMs this library targets per the PR description. A BitConverter-based implementation avoids conditional compilation and keeps behavior consistent.
        /// <summary>
        /// Reads an 8-byte floating-point value from the stream using big-endian byte order.
        /// </summary>
        public double ReadDoubleBigEndian() =>
            BinaryPrimitives.ReadDoubleBigEndian(ReadExactBytes(reader, sizeof(double)));

PowerKit/Extensions/BinaryWriterExtensions.cs:165

  • These float write helpers call BinaryPrimitives.WriteSingle*Endian, but the PR description indicates those overloads aren’t available on all target frameworks (netstandard2.0/net462 with System.Memory 4.6.3). Using BitConverter + conditional Array.Reverse avoids potential multi-target build failures and keeps the API available everywhere.
        /// <summary>
        /// Writes a 4-byte floating-point value to the stream using big-endian byte order.
        /// </summary>
        public void WriteSingleBigEndian(float value)
        {

PowerKit/Extensions/BinaryWriterExtensions.cs:185

  • Same concern as WriteSingleEndian: BinaryPrimitives.WriteDoubleEndian may not exist for all targeted TFMs per the PR description. BitConverter-based conversion keeps these methods available across targets.
        /// <summary>
        /// Writes an 8-byte floating-point value to the stream using big-endian byte order.
        /// </summary>
        public void WriteDoubleBigEndian(double value)
        {

Comment thread Directory.Packages.props
@Tyrrrz

Tyrrrz commented Jul 20, 2026

Copy link
Copy Markdown
Owner

@copilot since BinaryWriter's intrinsic methods are all just Write(...) overloads, we should do the same for our extensions: WriteBigEndian()/WriteLittleEndian()

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

@copilot since BinaryWriter's intrinsic methods are all just Write(...) overloads, we should do the same for our extensions: WriteBigEndian()/WriteLittleEndian()

Done — renamed all Write*BigEndian/Write*LittleEndian methods to WriteBigEndian/WriteLittleEndian overloads, matching the Write(...) pattern of BinaryWriter's intrinsic methods. Tests updated accordingly.

@Tyrrrz

Tyrrrz commented Jul 20, 2026

Copy link
Copy Markdown
Owner

@copilot order the methods you added by name and make the tests match that order

Copilot AI commented Jul 20, 2026

Copy link
Copy Markdown
Contributor Author

@copilot order the methods you added by name and make the tests match that order

Done — all WriteBigEndian overloads are now grouped together first, followed by all WriteLittleEndian overloads. Tests are reordered to match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants