some clean up

This commit is contained in:
Adam Hathcock
2026-02-05 08:52:00 +00:00
parent d26db95aff
commit d12b539720
4 changed files with 34 additions and 85 deletions

View File

@@ -12,18 +12,18 @@ internal sealed partial class SeekableSharpCompressStream
int offset,
int count,
CancellationToken cancellationToken
) => _underlyingStream.ReadAsync(buffer, offset, count, cancellationToken);
) => _stream.ReadAsync(buffer, offset, count, cancellationToken);
#if !LEGACY_DOTNET
public override ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default
) => _underlyingStream.ReadAsync(buffer, cancellationToken);
) => _stream.ReadAsync(buffer, cancellationToken);
public override ValueTask WriteAsync(
ReadOnlyMemory<byte> buffer,
CancellationToken cancellationToken = default
) => _underlyingStream.WriteAsync(buffer, cancellationToken);
) => _stream.WriteAsync(buffer, cancellationToken);
public override ValueTask DisposeAsync()
{
@@ -40,7 +40,7 @@ internal sealed partial class SeekableSharpCompressStream
_isDisposed = true;
if (!LeaveStreamOpen)
{
_underlyingStream.Dispose();
_stream.Dispose();
}
return base.DisposeAsync();
}
@@ -51,14 +51,14 @@ internal sealed partial class SeekableSharpCompressStream
int offset,
int count,
CancellationToken cancellationToken
) => _underlyingStream.WriteAsync(buffer, offset, count, cancellationToken);
) => _stream.WriteAsync(buffer, offset, count, cancellationToken);
public override Task FlushAsync(CancellationToken cancellationToken) =>
_underlyingStream.FlushAsync(cancellationToken);
_stream.FlushAsync(cancellationToken);
public override Task CopyToAsync(
Stream destination,
int bufferSize,
CancellationToken cancellationToken
) => _underlyingStream.CopyToAsync(destination, bufferSize, cancellationToken);
) => _stream.CopyToAsync(destination, bufferSize, cancellationToken);
}

View File

@@ -5,9 +5,9 @@ namespace SharpCompress.IO;
internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
{
public override Stream BaseStream() => _underlyingStream;
public override Stream BaseStream() => _stream;
private readonly Stream _underlyingStream;
private readonly Stream _stream;
private long? _recordedPosition;
private bool _isDisposed;
@@ -23,7 +23,7 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
public new bool ThrowOnDispose { get; set; }
public SeekableSharpCompressStream(Stream stream)
: base(new NullStream())
: base(Null, true, false, null)
{
if (stream is null)
{
@@ -33,44 +33,44 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
{
throw new ArgumentException("Stream must be seekable", nameof(stream));
}
_underlyingStream = stream;
_stream = stream;
}
public override bool CanRead => _underlyingStream.CanRead;
public override bool CanRead => _stream.CanRead;
public override bool CanSeek => _underlyingStream.CanSeek;
public override bool CanSeek => _stream.CanSeek;
public override bool CanWrite => _underlyingStream.CanWrite;
public override bool CanWrite => _stream.CanWrite;
public override long Length => _underlyingStream.Length;
public override long Length => _stream.Length;
public override long Position
{
get => _underlyingStream.Position;
set => _underlyingStream.Position = value;
get => _stream.Position;
set => _stream.Position = value;
}
internal override bool IsRecording => _recordedPosition.HasValue;
public override void Flush() => _underlyingStream.Flush();
public override void Flush() => _stream.Flush();
public override int Read(byte[] buffer, int offset, int count) =>
_underlyingStream.Read(buffer, offset, count);
_stream.Read(buffer, offset, count);
#if !LEGACY_DOTNET
public override int Read(Span<byte> buffer) => _underlyingStream.Read(buffer);
public override int Read(Span<byte> buffer) => _stream.Read(buffer);
#endif
public override long Seek(long offset, SeekOrigin origin) =>
_underlyingStream.Seek(offset, origin);
_stream.Seek(offset, origin);
public override void SetLength(long value) => _underlyingStream.SetLength(value);
public override void SetLength(long value) => _stream.SetLength(value);
public override void Write(byte[] buffer, int offset, int count) =>
_underlyingStream.Write(buffer, offset, count);
_stream.Write(buffer, offset, count);
#if !LEGACY_DOTNET
public override void Write(ReadOnlySpan<byte> buffer) => _underlyingStream.Write(buffer);
public override void Write(ReadOnlySpan<byte> buffer) => _stream.Write(buffer);
#endif
public override void Rewind(bool stopRecording = false)
@@ -80,22 +80,16 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
return;
}
_underlyingStream.Seek(_recordedPosition.Value, SeekOrigin.Begin);
_stream.Seek(_recordedPosition.Value, SeekOrigin.Begin);
if (stopRecording)
{
_recordedPosition = null;
}
}
public override void StartRecording()
{
_recordedPosition = _underlyingStream.Position;
}
public override void StartRecording() => _recordedPosition = _stream.Position;
public override void StopRecording()
{
_recordedPosition = null;
}
public override void StopRecording() => _recordedPosition = null;
protected override void Dispose(bool disposing)
{
@@ -112,45 +106,8 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
_isDisposed = true;
if (disposing && !LeaveStreamOpen)
{
_underlyingStream.Dispose();
_stream.Dispose();
}
base.Dispose(disposing);
}
private sealed class NullStream : Stream
{
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override void Flush() { }
public override int Read(byte[] buffer, int offset, int count) => 0;
#if !LEGACY_DOTNET
public override int Read(Span<byte> buffer) => 0;
#endif
public override long Seek(long offset, SeekOrigin origin) =>
throw new NotSupportedException();
public override void SetLength(long value) => throw new NotSupportedException();
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
#if !LEGACY_DOTNET
public override void Write(ReadOnlySpan<byte> buffer) => throw new NotSupportedException();
#endif
}
}

View File

@@ -19,7 +19,7 @@ internal partial class SharpCompressStream
int? bufferSize = null
)
{
int rewindableBufferSize = bufferSize ?? Constants.RewindableBufferSize;
var rewindableBufferSize = bufferSize ?? Constants.RewindableBufferSize;
// If it's a passthrough SharpCompressStream, unwrap it and create proper seekable wrapper
if (stream is SharpCompressStream sharpCompressStream)
@@ -31,10 +31,7 @@ internal partial class SharpCompressStream
if (underlying.CanSeek)
{
// Create SeekableSharpCompressStream that preserves LeaveStreamOpen
return new SeekableSharpCompressStream(underlying)
{
LeaveStreamOpen = true, // Preserve non-disposing behavior
};
return new SeekableSharpCompressStream(underlying);
}
// Non-seekable underlying stream - wrap with rolling buffer
return new SharpCompressStream(underlying, true, false, rewindableBufferSize);

View File

@@ -29,15 +29,10 @@ internal partial class SharpCompressStream : Stream, IStreamStack
/// </summary>
internal bool IsPassthrough => _isPassthrough;
/// <summary>
/// Default size for rolling buffer (same as .NET Stream.CopyTo default)
/// </summary>
public const int DefaultRollingBufferSize = 81920;
/// <summary>
/// Gets or sets whether to leave the underlying stream open when disposed.
/// </summary>
public bool LeaveStreamOpen { get; set; }
public bool LeaveStreamOpen { get; }
/// <summary>
/// Gets or sets whether to throw an exception when Dispose is called.
@@ -55,7 +50,7 @@ internal partial class SharpCompressStream : Stream, IStreamStack
/// <summary>
/// Private constructor for passthrough mode.
/// </summary>
private SharpCompressStream(Stream stream, bool leaveStreamOpen, bool passthrough, int? bufferSize)
protected SharpCompressStream(Stream stream, bool leaveStreamOpen, bool passthrough, int? bufferSize)
{
this.stream = stream;
LeaveStreamOpen = leaveStreamOpen;
@@ -182,7 +177,7 @@ internal partial class SharpCompressStream : Stream, IStreamStack
// Ensure ring buffer exists
if (_ringBuffer is null)
{
_ringBuffer = new RingBuffer(DefaultRollingBufferSize);
_ringBuffer = new RingBuffer(Constants.BufferSize);
}
// Mark current position as recording anchor
@@ -193,7 +188,7 @@ internal partial class SharpCompressStream : Stream, IStreamStack
public override bool CanRead => true;
public override bool CanSeek => _isPassthrough ? stream.CanSeek : true;
public override bool CanSeek => !_isPassthrough || stream.CanSeek;
public override bool CanWrite => _isPassthrough && stream.CanWrite;