diff --git a/src/SharpCompress/IO/SharpCompressStream.cs b/src/SharpCompress/IO/SharpCompressStream.cs index 00c0ada2f..185a87668 100644 --- a/src/SharpCompress/IO/SharpCompressStream.cs +++ b/src/SharpCompress/IO/SharpCompressStream.cs @@ -57,14 +57,8 @@ int IStreamStack.BufferSize { ValidateBufferState(); // Add here } - try - { - _internalPosition = Stream.Position; - } - catch - { - _internalPosition = 0; - } + // Check CanSeek before accessing Position to avoid exception overhead on non-seekable streams. + _internalPosition = Stream.CanSeek ? Stream.Position : 0; } } } @@ -136,14 +130,8 @@ public SharpCompressStream( _readOnly = !Stream.CanSeek; ((IStreamStack)this).SetBuffer(bufferSize, forceBuffer); - try - { - _baseInitialPos = stream.Position; - } - catch - { - _baseInitialPos = 0; - } + // Check CanSeek before accessing Position to avoid exception overhead on non-seekable streams. + _baseInitialPos = Stream.CanSeek ? Stream.Position : 0; #if DEBUG_STREAMS this.DebugConstruct(typeof(SharpCompressStream));