mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-04 05:25:00 +00:00
[PR #1100] Consolidate stream extension methods and simplify with framework methods #1533
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?
Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/1100
State: closed
Merged: Yes
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.