2026-01-28 16:50:35 +00:00
|
|
|
using System;
|
2026-01-28 16:33:19 +00:00
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
|
|
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 16:50:35 +00:00
|
|
|
private MemoryStream _bufferStream = new MemoryStream();
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
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 16:50:35 +00:00
|
|
|
_bufferStream.Position = 0;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Rewind(MemoryStream buffer)
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
if (_bufferStream.Position >= buffer.Length)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_bufferStream.Position -= buffer.Length;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_bufferStream.TransferTo(buffer, buffer.Length - _bufferStream.Position);
|
2026-01-28 16:33:19 +00:00
|
|
|
//create new memorystream to allow proper resizing as memorystream could be a user provided buffer
|
|
|
|
|
//https://github.com/adamhathcock/sharpcompress/issues/306
|
2026-01-28 16:50:35 +00:00
|
|
|
_bufferStream = new MemoryStream();
|
2026-01-28 16:33:19 +00:00
|
|
|
buffer.Position = 0;
|
2026-01-28 16:50:35 +00:00
|
|
|
buffer.TransferTo(_bufferStream, buffer.Length);
|
|
|
|
|
_bufferStream.Position = 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()
|
|
|
|
|
{
|
|
|
|
|
//if (isRewound && bufferStream.Position != 0)
|
|
|
|
|
// throw new System.NotImplementedException();
|
2026-01-28 16:50:35 +00:00
|
|
|
if (_bufferStream.Position != 0)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
byte[] data = _bufferStream.ToArray();
|
|
|
|
|
long position = _bufferStream.Position;
|
|
|
|
|
_bufferStream.SetLength(0);
|
|
|
|
|
_bufferStream.Write(data, (int)position, data.Length - (int)position);
|
|
|
|
|
_bufferStream.Position = 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 16:50:35 +00:00
|
|
|
get => stream.Position + _bufferStream.Position - _bufferStream.Length;
|
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 16:50:35 +00:00
|
|
|
else if (value < stream.Position - _bufferStream.Length || value >= stream.Position)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
|
|
|
|
stream.Position = value;
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = false;
|
|
|
|
|
_bufferStream.SetLength(0);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_bufferStream.Position = value - stream.Position + _bufferStream.Length;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
//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;
|
2026-01-28 16:50:35 +00:00
|
|
|
if (_isRewound && _bufferStream.Position != _bufferStream.Length)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
read = _bufferStream.Read(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 16:50:35 +00:00
|
|
|
_bufferStream.Write(buffer, offset + read, tempRead);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
read += tempRead;
|
|
|
|
|
}
|
2026-01-28 16:50:35 +00:00
|
|
|
if (_bufferStream.Position == _bufferStream.Length && !IsRecording)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_isRewound = false;
|
|
|
|
|
_bufferStream.SetLength(0);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
return read;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
read = stream.Read(buffer, offset, count);
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-28 16:50:35 +00:00
|
|
|
_bufferStream.Write(buffer, offset, read);
|
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
|
|
|
//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;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-28 16:50:35 +00:00
|
|
|
read = stream.Read(buffer);
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
|
|
|
|
_bufferStream.Write(buffer.Slice(0, read));
|
|
|
|
|
}
|
|
|
|
|
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 16:50:35 +00:00
|
|
|
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();
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
|
|
|
|
}
|