fix up rewindable stream and use it more, add NonDisposingStream

This commit is contained in:
Adam Hathcock
2026-01-29 09:08:40 +00:00
parent 58459bda12
commit e2cb9f39ab
12 changed files with 529 additions and 86 deletions

View File

@@ -18,7 +18,7 @@ public abstract partial class Volume : IVolume, IAsyncDisposable
_baseStream = stream;
if (ReaderOptions.LeaveStreamOpen)
{
stream = SharpCompressStream.Create(stream, leaveOpen: true);
stream = new NonDisposingStream(stream);
}
if (stream is IStreamStack ss)

View File

@@ -87,7 +87,7 @@ public abstract class Factory : IFactory
stream.Rewind();
if (IsArchive(stream, options.Password))
{
stream.Rewind();
stream.StopRecording();
reader = readerFactory.OpenReader(stream, options);
return true;
}

View File

@@ -120,11 +120,11 @@ public class GZipFactory
var testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
if (TarArchive.IsTarFile(testStream))
{
rewindableStream.Rewind();
rewindableStream.StopRecording();
reader = new TarReader(rewindableStream, options, CompressionType.GZip);
return true;
}
rewindableStream.Rewind();
rewindableStream.StopRecording();
reader = OpenReader(rewindableStream, options);
return true;
}

View File

@@ -168,7 +168,7 @@ public class TarFactory
var decompressedStream = wrapper.CreateStream(rewindableStream);
if (TarArchive.IsTarFile(decompressedStream))
{
rewindableStream.Rewind();
rewindableStream.StopRecording();
return new TarReader(rewindableStream, options, wrapper.CompressionType);
}
}
@@ -185,18 +185,19 @@ public class TarFactory
{
cancellationToken.ThrowIfCancellationRequested();
options ??= new ReaderOptions();
var rewindableStream = new SharpCompressStream(stream);
var pos = rewindableStream.GetPosition();
var rewindableStream = new RewindableStream(stream);
rewindableStream.StartRecording();
foreach (var wrapper in TarWrapper.Wrappers)
{
rewindableStream.StackSeek(pos);
rewindableStream.Rewind();
if (await wrapper.IsMatchAsync(rewindableStream, cancellationToken))
{
rewindableStream.StackSeek(pos);
rewindableStream.Rewind();
var decompressedStream = wrapper.CreateStream(rewindableStream);
if (await TarArchive.IsTarFileAsync(decompressedStream, cancellationToken))
{
rewindableStream.StackSeek(pos);
rewindableStream.Rewind();
rewindableStream.StopRecording();
return new TarReader(rewindableStream, options, wrapper.CompressionType);
}
}

View File

@@ -0,0 +1,216 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.IO;
/// <summary>
/// A stream wrapper that prevents disposal of the underlying stream.
/// This is useful when working with compression streams directly and you want
/// to keep the base stream open after the compression stream is disposed.
/// </summary>
internal class NonDisposingStream : Stream
{
private readonly Stream _stream;
private bool _isDisposed;
/// <summary>
/// Gets or sets a value indicating whether to throw an exception when the stream is disposed.
/// This is useful for testing to ensure streams are not disposed prematurely.
/// </summary>
public bool ThrowOnDispose { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="NonDisposingStream"/> class.
/// </summary>
/// <param name="stream">The stream to wrap. This stream will NOT be disposed when this wrapper is disposed.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="stream"/> is null.</exception>
public NonDisposingStream(Stream stream)
{
_stream = stream ?? throw new ArgumentNullException(nameof(stream));
}
public override bool CanRead => !_isDisposed && _stream.CanRead;
public override bool CanSeek => !_isDisposed && _stream.CanSeek;
public override bool CanWrite => !_isDisposed && _stream.CanWrite;
public override long Length
{
get
{
ThrowIfDisposed();
return _stream.Length;
}
}
public override long Position
{
get
{
ThrowIfDisposed();
return _stream.Position;
}
set
{
ThrowIfDisposed();
_stream.Position = value;
}
}
public override void Flush()
{
ThrowIfDisposed();
_stream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
return _stream.Read(buffer, offset, count);
}
#if !LEGACY_DOTNET
public override int Read(Span<byte> buffer)
{
ThrowIfDisposed();
return _stream.Read(buffer);
}
#endif
public override long Seek(long offset, SeekOrigin origin)
{
ThrowIfDisposed();
return _stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
ThrowIfDisposed();
_stream.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
_stream.Write(buffer, offset, count);
}
#if !LEGACY_DOTNET
public override void Write(ReadOnlySpan<byte> buffer)
{
ThrowIfDisposed();
_stream.Write(buffer);
}
#endif
public override Task<int> ReadAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
)
{
ThrowIfDisposed();
return _stream.ReadAsync(buffer, offset, count, cancellationToken);
}
#if !LEGACY_DOTNET
public override ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default
)
{
ThrowIfDisposed();
return _stream.ReadAsync(buffer, cancellationToken);
}
#endif
public override Task WriteAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
)
{
ThrowIfDisposed();
return _stream.WriteAsync(buffer, offset, count, cancellationToken);
}
#if !LEGACY_DOTNET
public override ValueTask WriteAsync(
ReadOnlyMemory<byte> buffer,
CancellationToken cancellationToken = default
)
{
ThrowIfDisposed();
return _stream.WriteAsync(buffer, cancellationToken);
}
#endif
public override Task FlushAsync(CancellationToken cancellationToken)
{
ThrowIfDisposed();
return _stream.FlushAsync(cancellationToken);
}
public override Task CopyToAsync(
Stream destination,
int bufferSize,
CancellationToken cancellationToken
)
{
ThrowIfDisposed();
return _stream.CopyToAsync(destination, bufferSize, cancellationToken);
}
/// <summary>
/// Disposes this wrapper without disposing the underlying stream.
/// </summary>
protected override void Dispose(bool disposing)
{
if (!_isDisposed)
{
if (ThrowOnDispose)
{
throw new InvalidOperationException(
$"Attempt to dispose of a {nameof(NonDisposingStream)} when {nameof(ThrowOnDispose)} is true"
);
}
_isDisposed = true;
// Intentionally do NOT dispose _stream
}
base.Dispose(disposing);
}
#if !LEGACY_DOTNET
/// <summary>
/// Asynchronously disposes this wrapper without disposing the underlying stream.
/// </summary>
public override async ValueTask DisposeAsync()
{
if (!_isDisposed)
{
if (ThrowOnDispose)
{
throw new InvalidOperationException(
$"Attempt to dispose of a {nameof(NonDisposingStream)} when {nameof(ThrowOnDispose)} is true"
);
}
_isDisposed = true;
// Intentionally do NOT dispose _stream
}
await base.DisposeAsync();
}
#endif
private void ThrowIfDisposed()
{
if (_isDisposed)
{
throw new ObjectDisposedException(nameof(NonDisposingStream));
}
}
}

View File

@@ -84,6 +84,13 @@ namespace SharpCompress.IO
IsRecording = true;
}
public void StopRecording()
{
_isRewound = true;
IsRecording = false;
_bufferPosition = 0;
}
public override bool CanRead => true;
public override bool CanSeek => stream.CanSeek;

View File

@@ -54,8 +54,8 @@ public static partial class ReaderFactory
stream.NotNull(nameof(stream));
options ??= new ReaderOptions() { LeaveStreamOpen = false };
var bStream = new SharpCompressStream(stream, bufferSize: options.BufferSize);
long pos = bStream.GetPosition();
var bStream = new RewindableStream(stream);
bStream.StartRecording();
var factories = Factory.Factories.OfType<Factory>();
@@ -68,7 +68,7 @@ public static partial class ReaderFactory
);
if (testedFactory is IReaderFactory readerFactory)
{
bStream.StackSeek(pos);
bStream.Rewind();
if (
await testedFactory.IsArchiveAsync(
bStream,
@@ -76,11 +76,12 @@ public static partial class ReaderFactory
)
)
{
bStream.StackSeek(pos);
bStream.Rewind();
bStream.StopRecording();
return await readerFactory.OpenAsyncReader(bStream, options, cancellationToken);
}
}
bStream.StackSeek(pos);
bStream.Rewind();
}
foreach (var factory in factories)
@@ -89,13 +90,14 @@ public static partial class ReaderFactory
{
continue; // Already tested above
}
bStream.StackSeek(pos);
bStream.Rewind();
if (
factory is IReaderFactory readerFactory
&& await factory.IsArchiveAsync(bStream, cancellationToken: cancellationToken)
)
{
bStream.StackSeek(pos);
bStream.Rewind();
bStream.StopRecording();
return await readerFactory.OpenAsyncReader(bStream, options, cancellationToken);
}
}

View File

@@ -52,7 +52,7 @@ public static partial class ReaderFactory
&& reader != null
)
{
bStream.Rewind();
bStream.StopRecording();
return reader;
}
}