[PR #1084] [MERGED] Avoid NotSupportedException overhead in SharpCompressStream for non-seekable streams #1506

Closed
opened 2026-01-29 22:20:53 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1084
Author: @Copilot
Created: 12/23/2025
Status: Merged
Merged: 12/23/2025
Merged by: @adamhathcock

Base: masterHead: copilot/fix-handled-system-exception


📝 Commits (5)

  • 32b1ec3 Initial plan
  • 253a46d Fix NotSupportedException in SharpCompressStream by checking CanSeek
  • d614beb Add explanatory comments for CanSeek checks and try-catch blocks
  • 6991900 Remove try/catch blocks, just check CanSeek as requested
  • 4dbe0b9 Update src/SharpCompress/IO/SharpCompressStream.cs

📊 Changes

1 file changed (+4 additions, -16 deletions)

View changed files

📝 src/SharpCompress/IO/SharpCompressStream.cs (+4 -16)

📄 Description

SharpCompressStream was attempting to access stream.Position without checking CanSeek, causing NotSupportedException to be thrown and caught for every non-seekable stream (e.g., XZ's ReadOnlyStream). This exception overhead occurred in the constructor and buffer allocation, impacting performance for formats using non-seekable streams.

Changes

  • Check stream.CanSeek before accessing Position in two locations:
    • Constructor initialization of _baseInitialPos
    • BufferSize setter initialization of _internalPosition
  • Use ternary operators for cleaner, simpler code

Impact

Eliminates exception overhead for non-seekable streams while maintaining identical behavior for seekable streams. No API changes.

// Before: Always tried Position, caught exception for non-seekable streams
try {
    _baseInitialPos = stream.Position;
} catch {
    _baseInitialPos = 0;
}

// After: Check CanSeek before accessing Position
_baseInitialPos = stream.CanSeek ? stream.Position : 0;

<issue_title>Handled System.NotSupportedException but can be avoided?</issue_title>
><issue_description>Open xz file like this:
>
> using var fileStream = new FileStream(updateFilename, FileMode.Open, FileAccess.Read);
> using var reader = ReaderFactory.Open(fileStream, new SharpCompress.Readers.ReaderOptions()
> {
> LeaveStreamOpen = true,
> });
>
>
> SharpCompressStream is wrapped around ReadOnlyStream and ReadOnlyStream doesnt' support Position get. In SharpCompressStream exception is handled:
>
> try
> {
> _baseInitialPos = stream.Position;
> }
> catch
> {
> _baseInitialPos = 0;
> }
>
> But this is still exception thrown for each entry I think which causes performance issues. Can it be avoided? Check for CanSeek ?</issue_description>
>
> ## Comments on the Issue (you are @copilot in this section)
>
>
>
>

Original prompt

This section details on the original issue you should resolve

<issue_title>Handled System.NotSupportedException but can be avoided?</issue_title>
<issue_description>Open xz file like this:

            using var fileStream = new FileStream(updateFilename, FileMode.Open, FileAccess.Read);
            using var reader = ReaderFactory.Open(fileStream, new SharpCompress.Readers.ReaderOptions()
            {
                LeaveStreamOpen = true,
            });

SharpCompressStream is wrapped around ReadOnlyStream and ReadOnlyStream doesnt' support Position get. In SharpCompressStream exception is handled:

    try
    {
        _baseInitialPos = stream.Position;
    }
    catch
    {
        _baseInitialPos = 0;
    }

But this is still exception thrown for each entry I think which causes performance issues. Can it be avoided? Check for CanSeek ?</issue_description>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1084 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 12/23/2025 **Status:** ✅ Merged **Merged:** 12/23/2025 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `copilot/fix-handled-system-exception` --- ### 📝 Commits (5) - [`32b1ec3`](https://github.com/adamhathcock/sharpcompress/commit/32b1ec32c6e9689061716d21cde552e6319c0310) Initial plan - [`253a46d`](https://github.com/adamhathcock/sharpcompress/commit/253a46d458200530fb2def55f505af84d9d52eae) Fix NotSupportedException in SharpCompressStream by checking CanSeek - [`d614beb`](https://github.com/adamhathcock/sharpcompress/commit/d614beb9eb6992ed169963a0c96db52a3b1092c0) Add explanatory comments for CanSeek checks and try-catch blocks - [`6991900`](https://github.com/adamhathcock/sharpcompress/commit/6991900eb0f398c940d54856bd554ae85350e774) Remove try/catch blocks, just check CanSeek as requested - [`4dbe0b9`](https://github.com/adamhathcock/sharpcompress/commit/4dbe0b91f129b5fdd5fd8fb8c3d1c5a007fc88f3) Update src/SharpCompress/IO/SharpCompressStream.cs ### 📊 Changes **1 file changed** (+4 additions, -16 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/IO/SharpCompressStream.cs` (+4 -16) </details> ### 📄 Description `SharpCompressStream` was attempting to access `stream.Position` without checking `CanSeek`, causing `NotSupportedException` to be thrown and caught for every non-seekable stream (e.g., XZ's `ReadOnlyStream`). This exception overhead occurred in the constructor and buffer allocation, impacting performance for formats using non-seekable streams. ## Changes - Check `stream.CanSeek` before accessing `Position` in two locations: - Constructor initialization of `_baseInitialPos` - `BufferSize` setter initialization of `_internalPosition` - Use ternary operators for cleaner, simpler code ## Impact Eliminates exception overhead for non-seekable streams while maintaining identical behavior for seekable streams. No API changes. ```csharp // Before: Always tried Position, caught exception for non-seekable streams try { _baseInitialPos = stream.Position; } catch { _baseInitialPos = 0; } // After: Check CanSeek before accessing Position _baseInitialPos = stream.CanSeek ? stream.Position : 0; ``` <issue_title>Handled System.NotSupportedException but can be avoided?</issue_title> &gt;<issue_description>Open xz file like this: &gt; &gt; using var fileStream = new FileStream(updateFilename, FileMode.Open, FileAccess.Read); &gt; using var reader = ReaderFactory.Open(fileStream, new SharpCompress.Readers.ReaderOptions() &gt; { &gt; LeaveStreamOpen = true, &gt; }); &gt; &gt; &gt; `SharpCompressStream` is wrapped around `ReadOnlyStream` and `ReadOnlyStream` doesnt' support Position get. In `SharpCompressStream` exception is handled: &gt; &gt; try &gt; { &gt; _baseInitialPos = stream.Position; &gt; } &gt; catch &gt; { &gt; _baseInitialPos = 0; &gt; } &gt; &gt; But this is still exception thrown for each entry I think which causes performance issues. Can it be avoided? Check for `CanSeek` ?</issue_description> &gt; &gt; ## Comments on the Issue (you are @copilot in this section) &gt; &gt;<comments> &gt;</comments> &gt; <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>Handled System.NotSupportedException but can be avoided?</issue_title> > <issue_description>Open xz file like this: > > using var fileStream = new FileStream(updateFilename, FileMode.Open, FileAccess.Read); > using var reader = ReaderFactory.Open(fileStream, new SharpCompress.Readers.ReaderOptions() > { > LeaveStreamOpen = true, > }); > > > `SharpCompressStream` is wrapped around `ReadOnlyStream` and `ReadOnlyStream` doesnt' support Position get. In `SharpCompressStream` exception is handled: > > try > { > _baseInitialPos = stream.Position; > } > catch > { > _baseInitialPos = 0; > } > > But this is still exception thrown for each entry I think which causes performance issues. Can it be avoided? Check for `CanSeek` ?</issue_description> > > ## Comments on the Issue (you are @copilot in this section) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes adamhathcock/sharpcompress#1083 <!-- START COPILOT CODING AGENT TIPS --> --- 💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:20:53 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1506