mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-03 21:23:38 +00:00
[PR #1100] [MERGED] Consolidate stream extension methods and simplify with framework methods #1530
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/adamhathcock/sharpcompress/pull/1100
Author: @Copilot
Created: 1/3/2026
Status: ✅ Merged
Merged: 1/5/2026
Merged by: @adamhathcock
Base:
adam/async← Head:copilot/fix-read-method-implementations📝 Commits (6)
5464054Consolidate reads1a71c01Consolidate ReadExact and ReadFully methods into Utility.cs05642cbUse ArrayPool for temporary buffers in BinaryReaderExtensions372ecb7Use threshold-based ArrayPool strategy for BinaryReaderExtensions7701522Add input validation for ReadBytesAsync count parameter9bd86f6Replace manual TransferTo implementation with Stream.CopyTo framework methods📊 Changes
7 files changed (+429 additions, -377 deletions)
View changed files
📝
src/SharpCompress/Common/AsyncBinaryReader.cs(+5 -27)📝
src/SharpCompress/Compressors/LZMA/Utilites/Utils.cs(+0 -35)📝
src/SharpCompress/Compressors/Xz/MultiByteIntegers.cs(+2 -35)➕
src/SharpCompress/Polyfills/BinaryReaderExtensions.cs(+65 -0)📝
src/SharpCompress/Polyfills/StreamExtensions.cs(+79 -43)📝
src/SharpCompress/Utility.cs(+186 -237)📝
tests/SharpCompress.Test/UtilityTests.cs(+92 -0)📄 Description
The codebase had duplicate implementations of stream reading extension methods scattered across multiple files. This PR consolidates all
ReadFully,ReadExact,ReadByteAsync, andReadBytesAsyncextension methods into a single location for better maintainability and optimizes memory allocation using a threshold-based ArrayPool strategy. Additionally, simplifiesTransferToimplementation by leveraging framework'sStream.CopyTomethods.Changes
ReadByteAsyncandReadBytesAsyncfromCompressors/Xz/MultiByteIntegers.cstoBinaryReaderExtensions.csReadExactandReadExactAsynctoUtility.cswith proper parameter validationReadFullyimplementations for non-.NET 6.0+ targets with conditional compilationReadExactfrom:Compressors/LZMA/Utilites/Utils.cs(removed duplicate)Common/AsyncBinaryReader.cs(now uses centralized version)Polyfills/BinaryReaderExtensions.cs(simplified to use centralized version)Polyfills/StreamExtensions.cs(delegates to centralized version)BinaryReaderExtensions.ReadBytesAsyncwas reading only 1 byte instead of the requested countMultiByteIntegers.ReadXZIntegerAsyncto callreader.ReadByteAsync()instead of local implementationBinaryReaderExtensions:ReadByteAsync: Uses simple allocation for single byte (ArrayPool overhead not justified)ReadBytesAsync: Uses direct allocation for small reads (≤256 bytes), ArrayPool for larger reads (>256 bytes)ReadBytesAsync(negative check, zero-length optimization)Stream.CopyTo()andCopyToAsync()methods:ReadOnlySubStreamto limit reading to specified maxLengthResult
All stream and binary reader extension methods now live in centralized locations with optimized memory allocation:
Performance Characteristics
Testing
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.