making RewindableStream more proper

This commit is contained in:
Adam Hathcock
2026-01-28 16:50:35 +00:00
parent c770bc4788
commit 8dfd5349f0
4 changed files with 635 additions and 111 deletions

View File

@@ -0,0 +1,108 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SharpCompress.IO;
internal partial class RewindableStream
{
public override async Task<int> ReadAsync(
byte[] buffer,
int offset,
int count,
CancellationToken cancellationToken
)
{
//don't actually read if we don't really want to read anything
//currently a network stream bug on Windows for .NET Core
if (count == 0)
{
return 0;
}
int read;
if (_isRewound && _bufferStream.Position != _bufferStream.Length)
{
read = await _bufferStream
.ReadAsync(buffer, offset, count, cancellationToken)
.ConfigureAwait(false);
if (read < count)
{
int tempRead = await stream
.ReadAsync(buffer, offset + read, count - read, cancellationToken)
.ConfigureAwait(false);
if (IsRecording)
{
await _bufferStream
.WriteAsync(buffer, offset + read, tempRead, cancellationToken)
.ConfigureAwait(false);
}
read += tempRead;
}
if (_bufferStream.Position == _bufferStream.Length && !IsRecording)
{
_isRewound = false;
_bufferStream.SetLength(0);
}
return read;
}
read = await stream
.ReadAsync(buffer, offset, count, cancellationToken)
.ConfigureAwait(false);
if (IsRecording)
{
await _bufferStream
.WriteAsync(buffer, offset, read, cancellationToken)
.ConfigureAwait(false);
}
return read;
}
#if !LEGACY_DOTNET
public override async ValueTask<int> ReadAsync(
Memory<byte> buffer,
CancellationToken cancellationToken = default
)
{
//don't actually read if we don't really want to read anything
//currently a network stream bug on Windows for .NET Core
if (buffer.Length == 0)
{
return 0;
}
int read;
if (_isRewound && _bufferStream.Position != _bufferStream.Length)
{
var bufferSpan = buffer.Span;
read = _bufferStream.Read(bufferSpan);
if (read < bufferSpan.Length)
{
int tempRead = await stream
.ReadAsync(buffer.Slice(read), cancellationToken)
.ConfigureAwait(false);
if (IsRecording)
{
await _bufferStream
.WriteAsync(buffer.Slice(read, tempRead), cancellationToken)
.ConfigureAwait(false);
}
read += tempRead;
}
if (_bufferStream.Position == _bufferStream.Length && !IsRecording)
{
_isRewound = false;
_bufferStream.SetLength(0);
}
return read;
}
read = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (IsRecording)
{
await _bufferStream.WriteAsync(buffer.Slice(0, read), cancellationToken).ConfigureAwait(false);
}
return read;
}
#endif
}

View File

@@ -1,29 +1,23 @@
using System;
using System;
using System.IO;
namespace SharpCompress.IO
{
internal partial class RewindableStream : Stream
internal partial class RewindableStream(Stream stream) : Stream
{
private readonly Stream stream;
private MemoryStream bufferStream = new MemoryStream();
private bool isRewound;
private bool isDisposed;
public RewindableStream(Stream stream)
{
this.stream = stream;
}
private MemoryStream _bufferStream = new MemoryStream();
private bool _isRewound;
private bool _isDisposed;
internal bool IsRecording { get; private set; }
protected override void Dispose(bool disposing)
{
if (isDisposed)
if (_isDisposed)
{
return;
}
isDisposed = true;
_isDisposed = true;
base.Dispose(disposing);
if (disposing)
{
@@ -33,42 +27,41 @@ namespace SharpCompress.IO
public void Rewind(bool stopRecording = false)
{
isRewound = true;
_isRewound = true;
IsRecording = !stopRecording;
bufferStream.Position = 0;
_bufferStream.Position = 0;
}
public void Rewind(MemoryStream buffer)
{
if (bufferStream.Position >= buffer.Length)
if (_bufferStream.Position >= buffer.Length)
{
bufferStream.Position -= buffer.Length;
_bufferStream.Position -= buffer.Length;
}
else
{
bufferStream.TransferTo(buffer, buffer.Length - bufferStream.Position);
_bufferStream.TransferTo(buffer, buffer.Length - _bufferStream.Position);
//create new memorystream to allow proper resizing as memorystream could be a user provided buffer
//https://github.com/adamhathcock/sharpcompress/issues/306
bufferStream = new MemoryStream();
_bufferStream = new MemoryStream();
buffer.Position = 0;
buffer.TransferTo(bufferStream, buffer.Length);
bufferStream.Position = 0;
buffer.TransferTo(_bufferStream, buffer.Length);
_bufferStream.Position = 0;
}
isRewound = true;
_isRewound = true;
}
public void StartRecording()
{
//if (isRewound && bufferStream.Position != 0)
// throw new System.NotImplementedException();
if (bufferStream.Position != 0)
if (_bufferStream.Position != 0)
{
byte[] data = bufferStream.ToArray();
long position = bufferStream.Position;
bufferStream.SetLength(0);
bufferStream.Write(data, (int)position, data.Length - (int)position);
bufferStream.Position = 0;
byte[] data = _bufferStream.ToArray();
long position = _bufferStream.Position;
_bufferStream.SetLength(0);
_bufferStream.Write(data, (int)position, data.Length - (int)position);
_bufferStream.Position = 0;
}
IsRecording = true;
}
@@ -79,31 +72,28 @@ namespace SharpCompress.IO
public override bool CanWrite => false;
public override void Flush()
{
throw new NotSupportedException();
}
public override void Flush() => throw new NotSupportedException();
public override long Length => throw new NotSupportedException();
public override long Position
{
get { return stream.Position + bufferStream.Position - bufferStream.Length; }
get => stream.Position + _bufferStream.Position - _bufferStream.Length;
set
{
if (!isRewound)
if (!_isRewound)
{
stream.Position = value;
}
else if (value < stream.Position - bufferStream.Length || value >= stream.Position)
else if (value < stream.Position - _bufferStream.Length || value >= stream.Position)
{
stream.Position = value;
isRewound = false;
bufferStream.SetLength(0);
_isRewound = false;
_bufferStream.SetLength(0);
}
else
{
bufferStream.Position = value - stream.Position + bufferStream.Length;
_bufferStream.Position = value - stream.Position + _bufferStream.Length;
}
}
}
@@ -117,22 +107,22 @@ namespace SharpCompress.IO
return 0;
}
int read;
if (isRewound && bufferStream.Position != bufferStream.Length)
if (_isRewound && _bufferStream.Position != _bufferStream.Length)
{
read = bufferStream.Read(buffer, offset, count);
read = _bufferStream.Read(buffer, offset, count);
if (read < count)
{
int tempRead = stream.Read(buffer, offset + read, count - read);
if (IsRecording)
{
bufferStream.Write(buffer, offset + read, tempRead);
_bufferStream.Write(buffer, offset + read, tempRead);
}
read += tempRead;
}
if (bufferStream.Position == bufferStream.Length && !IsRecording)
if (_bufferStream.Position == _bufferStream.Length && !IsRecording)
{
isRewound = false;
bufferStream.SetLength(0);
_isRewound = false;
_bufferStream.SetLength(0);
}
return read;
}
@@ -140,24 +130,54 @@ namespace SharpCompress.IO
read = stream.Read(buffer, offset, count);
if (IsRecording)
{
bufferStream.Write(buffer, offset, read);
_bufferStream.Write(buffer, offset, read);
}
return read;
}
public override long Seek(long offset, SeekOrigin origin)
#if !LEGACY_DOTNET
public override int Read(Span<byte> buffer)
{
throw new NotSupportedException();
}
//don't actually read if we don't really want to read anything
//currently a network stream bug on Windows for .NET Core
if (buffer.Length == 0)
{
return 0;
}
int read;
if (_isRewound && _bufferStream.Position != _bufferStream.Length)
{
read = _bufferStream.Read(buffer);
if (read < buffer.Length)
{
int tempRead = stream.Read(buffer.Slice(read));
if (IsRecording)
{
_bufferStream.Write(buffer.Slice(read, tempRead));
}
read += tempRead;
}
if (_bufferStream.Position == _bufferStream.Length && !IsRecording)
{
_isRewound = false;
_bufferStream.SetLength(0);
}
return read;
}
public override void SetLength(long value)
{
throw new NotSupportedException();
read = stream.Read(buffer);
if (IsRecording)
{
_bufferStream.Write(buffer.Slice(0, read));
}
return read;
}
#endif
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
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();
}
}