2026-01-28 16:50:35 +00:00
|
|
|
using System;
|
2026-01-28 19:15:34 +00:00
|
|
|
using System.Buffers;
|
2026-01-28 16:33:19 +00:00
|
|
|
using System.IO;
|
2026-01-28 19:15:34 +00:00
|
|
|
using SharpCompress.Common;
|
2026-01-28 16:33:19 +00:00
|
|
|
|
|
|
|
|
namespace SharpCompress.IO
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
internal partial class RewindableStream(Stream stream) : Stream
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
private readonly int _bufferSize = Constants.RewindableBufferSize;
|
|
|
|
|
private byte[]? _buffer = ArrayPool<byte>.Shared.Rent(Constants.RewindableBufferSize);
|
|
|
|
|
private int _bufferLength = 0;
|
|
|
|
|
private int _bufferPosition = 0;
|
2026-01-28 16:50:35 +00:00
|
|
|
private bool _isRewound;
|
|
|
|
|
private bool _isDisposed;
|
2026-01-28 16:33:19 +00:00
|
|
|
|
|
|
|
|
internal bool IsRecording { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
if (_isDisposed)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-28 16:50:35 +00:00
|
|
|
_isDisposed = true;
|
2026-01-28 16:33:19 +00:00
|
|
|
base.Dispose(disposing);
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
ArrayPool<byte>.Shared.Return(_buffer!);
|
|
|
|
|
_buffer = null;
|
2026-01-28 16:33:19 +00:00
|
|
|
stream.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Rewind(bool stopRecording = false)
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = true;
|
2026-01-28 16:33:19 +00:00
|
|
|
IsRecording = !stopRecording;
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferPosition = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Rewind(MemoryStream buffer)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
long bufferLength = buffer.Length;
|
|
|
|
|
if (_bufferPosition >= bufferLength)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferPosition -= (int)bufferLength;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
int bytesToKeep = _bufferLength - _bufferPosition;
|
|
|
|
|
if (bytesToKeep > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(_buffer!, _bufferPosition, _buffer!, 0, bytesToKeep);
|
|
|
|
|
}
|
|
|
|
|
if (bufferLength > _bufferSize)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"External buffer size ({bufferLength} bytes) exceeds internal buffer capacity ({_bufferSize} bytes)"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
_bufferLength = (int)bufferLength;
|
|
|
|
|
_bufferPosition = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
buffer.Position = 0;
|
2026-01-28 19:15:34 +00:00
|
|
|
int bytesRead = buffer.Read(_buffer!, 0, _bufferLength);
|
|
|
|
|
_bufferLength = bytesRead;
|
|
|
|
|
_bufferPosition = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = true;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void StartRecording()
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
if (_bufferPosition != 0)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
int bytesToKeep = _bufferLength - _bufferPosition;
|
|
|
|
|
if (bytesToKeep > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(_buffer!, _bufferPosition, _buffer!, 0, bytesToKeep);
|
|
|
|
|
}
|
|
|
|
|
_bufferLength = bytesToKeep;
|
|
|
|
|
_bufferPosition = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
IsRecording = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool CanRead => true;
|
|
|
|
|
|
|
|
|
|
public override bool CanSeek => stream.CanSeek;
|
|
|
|
|
|
|
|
|
|
public override bool CanWrite => false;
|
|
|
|
|
|
2026-01-28 16:50:35 +00:00
|
|
|
public override void Flush() => throw new NotSupportedException();
|
2026-01-28 16:33:19 +00:00
|
|
|
|
|
|
|
|
public override long Length => throw new NotSupportedException();
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_isRewound || _bufferPosition < _bufferLength)
|
|
|
|
|
{
|
|
|
|
|
return stream.Position + _bufferPosition - _bufferLength;
|
|
|
|
|
}
|
|
|
|
|
return stream.Position;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
set
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
if (!_isRewound)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
|
|
|
|
stream.Position = value;
|
|
|
|
|
}
|
2026-01-28 19:15:34 +00:00
|
|
|
else if (value < stream.Position - _bufferLength || value >= stream.Position)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
|
|
|
|
stream.Position = value;
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = false;
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferLength = 0;
|
|
|
|
|
_bufferPosition = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferPosition = (int)(value - stream.Position + _bufferLength);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int read;
|
2026-01-28 19:15:34 +00:00
|
|
|
if (_isRewound && _bufferPosition != _bufferLength)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
read = ReadFromBuffer(buffer, offset, count);
|
2026-01-28 16:33:19 +00:00
|
|
|
if (read < count)
|
|
|
|
|
{
|
|
|
|
|
int tempRead = stream.Read(buffer, offset + read, count - read);
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
WriteToBuffer(buffer, offset + read, tempRead);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
read += tempRead;
|
|
|
|
|
}
|
2026-01-28 19:15:34 +00:00
|
|
|
if (_bufferPosition == _bufferLength)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = false;
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferPosition = 0;
|
|
|
|
|
if (!IsRecording)
|
|
|
|
|
{
|
|
|
|
|
_bufferLength = 0;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read = stream.Read(buffer, offset, count);
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
WriteToBuffer(buffer, offset, read);
|
|
|
|
|
_bufferPosition = _bufferLength;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-28 16:50:35 +00:00
|
|
|
#if !LEGACY_DOTNET
|
|
|
|
|
public override int Read(Span<byte> buffer)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
if (buffer.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int read;
|
2026-01-28 19:15:34 +00:00
|
|
|
if (_isRewound && _bufferPosition != _bufferLength)
|
2026-01-28 16:50:35 +00:00
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
read = ReadFromBuffer(buffer);
|
2026-01-28 16:50:35 +00:00
|
|
|
if (read < buffer.Length)
|
|
|
|
|
{
|
|
|
|
|
int tempRead = stream.Read(buffer.Slice(read));
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
WriteToBuffer(buffer.Slice(read, tempRead));
|
2026-01-28 16:50:35 +00:00
|
|
|
}
|
|
|
|
|
read += tempRead;
|
|
|
|
|
}
|
2026-01-28 19:15:34 +00:00
|
|
|
if (_bufferPosition == _bufferLength)
|
2026-01-28 16:50:35 +00:00
|
|
|
{
|
|
|
|
|
_isRewound = false;
|
2026-01-28 19:15:34 +00:00
|
|
|
_bufferPosition = 0;
|
|
|
|
|
if (!IsRecording)
|
|
|
|
|
{
|
|
|
|
|
_bufferLength = 0;
|
|
|
|
|
}
|
2026-01-28 16:50:35 +00:00
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-28 16:50:35 +00:00
|
|
|
read = stream.Read(buffer);
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-28 19:15:34 +00:00
|
|
|
WriteToBuffer(buffer.Slice(0, read));
|
|
|
|
|
_bufferPosition = _bufferLength;
|
2026-01-28 16:50:35 +00:00
|
|
|
}
|
|
|
|
|
return read;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-28 16:50:35 +00:00
|
|
|
#endif
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-28 19:15:34 +00:00
|
|
|
public override long Seek(long offset, SeekOrigin origin) =>
|
|
|
|
|
throw new NotSupportedException();
|
2026-01-28 16:50:35 +00:00
|
|
|
|
|
|
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
|
|
|
|
|
2026-01-28 19:15:34 +00:00
|
|
|
public override void Write(byte[] buffer, int offset, int count) =>
|
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
|
|
|
|
private int ReadFromBuffer(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
int bytesToRead = Math.Min(count, _bufferLength - _bufferPosition);
|
|
|
|
|
if (bytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(_buffer!, _bufferPosition, buffer, offset, bytesToRead);
|
|
|
|
|
_bufferPosition += bytesToRead;
|
|
|
|
|
}
|
|
|
|
|
return bytesToRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int ReadFromBuffer(Span<byte> buffer)
|
|
|
|
|
{
|
|
|
|
|
int bytesToRead = Math.Min(buffer.Length, _bufferLength - _bufferPosition);
|
|
|
|
|
if (bytesToRead > 0)
|
|
|
|
|
{
|
|
|
|
|
_buffer!.AsSpan(_bufferPosition, bytesToRead).CopyTo(buffer);
|
|
|
|
|
_bufferPosition += bytesToRead;
|
|
|
|
|
}
|
|
|
|
|
return bytesToRead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteToBuffer(byte[] data, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
int spaceAvailable = _bufferSize - _bufferLength;
|
|
|
|
|
if (count > spaceAvailable)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"Buffer overflow: Cannot write {count} bytes. Only {spaceAvailable} bytes available in {_bufferSize} byte buffer."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
Array.Copy(data, offset, _buffer!, _bufferLength, count);
|
|
|
|
|
_bufferLength += count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteToBuffer(ReadOnlySpan<byte> data)
|
|
|
|
|
{
|
|
|
|
|
int spaceAvailable = _bufferSize - _bufferLength;
|
|
|
|
|
if (data.Length > spaceAvailable)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
$"Buffer overflow: Cannot write {data.Length} bytes. Only {spaceAvailable} bytes available in {_bufferSize} byte buffer."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
data.CopyTo(_buffer!.AsSpan(_bufferLength));
|
|
|
|
|
_bufferLength += data.Length;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
}
|