Skip to content

Consolidate ArrayPool + ReadFully/ReadExact patterns into reusable utilities#1194

Draft
Copilot wants to merge 6 commits intomasterfrom
copilot/consolidate-readfully-readexact-logic
Draft

Consolidate ArrayPool + ReadFully/ReadExact patterns into reusable utilities#1194
Copilot wants to merge 6 commits intomasterfrom
copilot/consolidate-readfully-readexact-logic

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 10, 2026

Multiple files contained identical ArrayPool rent/return/try-finally patterns around stream reads. This consolidates them into helper methods.

Changes

Added utilities in Utility.cs and Utility.Async.cs:

  • WithRentedBufferReadFully<T>() - Rents buffer, reads until full, executes callback, returns buffer
  • TryWithRentedBufferReadFully<T>() - Same but returns false on EOF instead of throwing
  • WithRentedBufferReadExact<T>() - Throws on partial read

Replaced duplicate patterns in:

  • ArcFactory.cs - IsArchive signature check
  • SevenZipArchive.Factory.cs - Signature validation
  • GZipArchive.Factory.cs - Header validation
  • WinzipAesCryptoStream.cs - Auth bytes disposal

Before/After

// Before: Manual ArrayPool management (repeated ~6 times)
var buffer = ArrayPool<byte>.Shared.Rent(2);
try
{
    if (stream.ReadFully(buffer.AsSpan(0, 2)))
    {
        return buffer[0] == 0x1A && buffer[1] < 10;
    }
    return false;
}
finally
{
    ArrayPool<byte>.Shared.Return(buffer);
}

// After: Consolidated helper
return stream.TryWithRentedBufferReadFully(
    2,
    buffer => buffer[0] == 0x1A && buffer[1] < 10,
    out var result
) && result;

Removes ~85 lines of boilerplate. All 1160 existing tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>Consolidate ReadFully/ReadExact and other instances of things</issue_title>
<issue_description>Inspired by #1169

Surely, this same logic can be reused in places.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits February 10, 2026 11:49
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Consolidate ReadFully and ReadExact logic in codebase Consolidate ArrayPool + ReadFully/ReadExact patterns into reusable utilities Feb 10, 2026
Copilot AI requested a review from adamhathcock February 10, 2026 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consolidate ReadFully/ReadExact and other instances of things

2 participants