2026-01-28 16:50:35 +00:00
|
|
|
using System;
|
2026-01-28 16:33:19 +00:00
|
|
|
using System.IO;
|
|
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
namespace SharpCompress.IO;
|
|
|
|
|
|
2026-02-03 17:07:39 +00:00
|
|
|
internal partial class RewindableStream : Stream, IStreamStack
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-02-03 17:07:39 +00:00
|
|
|
public virtual Stream BaseStream() => stream;
|
|
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
private readonly Stream stream;
|
|
|
|
|
private MemoryStream bufferStream = new MemoryStream();
|
|
|
|
|
private bool isRewound;
|
|
|
|
|
private bool isDisposed;
|
|
|
|
|
private long streamPosition;
|
|
|
|
|
|
|
|
|
|
public RewindableStream(Stream stream) => this.stream = stream;
|
|
|
|
|
|
|
|
|
|
internal virtual bool IsRecording { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
if (isDisposed)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
isDisposed = true;
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
if (disposing)
|
2026-01-29 14:42:29 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
stream.Dispose();
|
2026-01-29 14:42:29 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Rewind() => Rewind(false);
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public virtual void Rewind(bool stopRecording)
|
|
|
|
|
{
|
|
|
|
|
isRewound = true;
|
|
|
|
|
IsRecording = !stopRecording;
|
|
|
|
|
bufferStream.Position = 0;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public virtual void StopRecording()
|
|
|
|
|
{
|
|
|
|
|
if (!IsRecording)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-29 15:47:22 +00:00
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
"StopRecording can only be called when recording is active."
|
|
|
|
|
);
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
isRewound = true;
|
|
|
|
|
IsRecording = false;
|
|
|
|
|
bufferStream.Position = 0;
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public static RewindableStream EnsureSeekable(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
if (stream is RewindableStream rewindableStream)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
return rewindableStream;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
if (stream.CanSeek)
|
|
|
|
|
{
|
|
|
|
|
return new SeekableRewindableStream(stream);
|
|
|
|
|
}
|
|
|
|
|
return new RewindableStream(stream);
|
|
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public virtual void StartRecording()
|
|
|
|
|
{
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
{
|
2026-01-29 15:47:22 +00:00
|
|
|
throw new InvalidOperationException(
|
|
|
|
|
"StartRecording can only be called when not already recording."
|
|
|
|
|
);
|
2026-01-29 15:23:53 +00:00
|
|
|
}
|
|
|
|
|
//if (isRewound && bufferStream.Position != 0)
|
|
|
|
|
// throw new System.NotImplementedException();
|
|
|
|
|
if (bufferStream.Position != 0)
|
2026-01-29 09:08:40 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
var data = bufferStream.ToArray();
|
|
|
|
|
var position = bufferStream.Position;
|
|
|
|
|
bufferStream.SetLength(0);
|
|
|
|
|
bufferStream.Write(data, (int)position, data.Length - (int)position);
|
|
|
|
|
bufferStream.Position = 0;
|
2026-01-29 09:08:40 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
IsRecording = true;
|
|
|
|
|
}
|
2026-01-29 09:08:40 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override bool CanRead => true;
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override bool CanSeek => true;
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override bool CanWrite => false;
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override void Flush() => throw new NotSupportedException();
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override long Length => throw new NotSupportedException();
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override long Position
|
|
|
|
|
{
|
|
|
|
|
get
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
if (isRewound || bufferStream.Position < bufferStream.Length)
|
2026-01-28 19:15:34 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
return streamPosition - bufferStream.Length + bufferStream.Position;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
return streamPosition;
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-02-01 08:47:17 +00:00
|
|
|
set => SeekToPosition(value);
|
|
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
|
2026-02-01 08:47:17 +00:00
|
|
|
private void SeekToPosition(long targetPosition)
|
|
|
|
|
{
|
|
|
|
|
long bufferStart = streamPosition - bufferStream.Length;
|
|
|
|
|
long bufferEnd = streamPosition;
|
|
|
|
|
|
|
|
|
|
if (targetPosition >= bufferStart && targetPosition <= bufferEnd)
|
|
|
|
|
{
|
|
|
|
|
isRewound = true;
|
|
|
|
|
bufferStream.Position = targetPosition - bufferStart;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new NotSupportedException("Cannot seek outside buffered region.");
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
}
|
2026-01-28 16:33:19 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
{
|
|
|
|
|
if (count == 0)
|
2026-01-28 16:33:19 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int read;
|
|
|
|
|
if (isRewound && bufferStream.Position != bufferStream.Length)
|
|
|
|
|
{
|
|
|
|
|
var readCount = Math.Min(count, (int)(bufferStream.Length - bufferStream.Position));
|
|
|
|
|
read = bufferStream.Read(buffer, offset, readCount);
|
2026-02-03 08:56:35 +00:00
|
|
|
if (read < count)
|
2026-01-28 16:50:35 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
var tempRead = stream.Read(buffer, offset + read, count - read);
|
|
|
|
|
if (IsRecording)
|
2026-01-28 16:50:35 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
bufferStream.Write(buffer, offset + read, tempRead);
|
2026-01-28 16:50:35 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
streamPosition += tempRead;
|
|
|
|
|
read += tempRead;
|
2026-01-28 16:50:35 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
if (bufferStream.Position == bufferStream.Length)
|
2026-01-28 16:50:35 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
isRewound = false;
|
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
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
read = stream.Read(buffer, offset, count);
|
|
|
|
|
if (IsRecording)
|
2026-01-28 19:15:34 +00:00
|
|
|
{
|
2026-01-29 15:23:53 +00:00
|
|
|
bufferStream.Write(buffer, offset, read);
|
2026-01-28 19:15:34 +00:00
|
|
|
}
|
2026-01-29 15:23:53 +00:00
|
|
|
streamPosition += read;
|
|
|
|
|
return read;
|
|
|
|
|
}
|
2026-01-28 19:15:34 +00:00
|
|
|
|
2026-02-01 08:47:17 +00:00
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
long targetPosition = origin switch
|
|
|
|
|
{
|
|
|
|
|
SeekOrigin.Begin => offset,
|
|
|
|
|
SeekOrigin.Current => Position + offset,
|
|
|
|
|
SeekOrigin.End => throw new NotSupportedException("Seeking from end is not supported."),
|
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(origin)),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SeekToPosition(targetPosition);
|
|
|
|
|
return targetPosition;
|
|
|
|
|
}
|
2026-01-28 19:15:34 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
2026-01-28 19:15:34 +00:00
|
|
|
|
2026-01-29 15:23:53 +00:00
|
|
|
public override void Write(byte[] buffer, int offset, int count) =>
|
|
|
|
|
throw new NotSupportedException();
|
2026-01-28 16:33:19 +00:00
|
|
|
}
|