Files
sharpcompress/src/SharpCompress/IO/RewindableStream.cs

169 lines
4.6 KiB
C#
Raw Normal View History

2026-01-28 16:50:35 +00:00
using System;
using System.IO;
2026-01-29 15:23:53 +00:00
namespace SharpCompress.IO;
internal partial class RewindableStream : 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-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-29 15:23:53 +00:00
public virtual void Rewind(bool stopRecording)
{
isRewound = true;
IsRecording = !stopRecording;
bufferStream.Position = 0;
}
2026-01-29 15:23:53 +00:00
public virtual void StopRecording()
{
if (!IsRecording)
{
2026-01-29 15:47:22 +00:00
throw new InvalidOperationException(
"StopRecording can only be called when recording is active."
);
}
2026-01-29 15:23:53 +00:00
isRewound = true;
IsRecording = false;
bufferStream.Position = 0;
}
2026-01-29 15:23:53 +00:00
public static RewindableStream EnsureSeekable(Stream stream)
{
if (stream is RewindableStream rewindableStream)
{
2026-01-29 15:23:53 +00:00
return rewindableStream;
}
2026-01-29 15:23:53 +00:00
if (stream.CanSeek)
{
return new SeekableRewindableStream(stream);
}
return new RewindableStream(stream);
}
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 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 15:23:53 +00:00
IsRecording = true;
}
2026-01-29 15:23:53 +00:00
public override bool CanRead => true;
2026-01-29 15:23:53 +00:00
public override bool CanSeek => true;
2026-01-29 15:23:53 +00:00
public override bool CanWrite => false;
2026-01-29 15:23:53 +00:00
public override void Flush() => throw new NotSupportedException();
2026-01-29 15:23:53 +00:00
public override long Length => throw new NotSupportedException();
2026-01-29 15:23:53 +00:00
public override long Position
{
get
{
2026-01-29 15:23:53 +00:00
if (isRewound || bufferStream.Position < bufferStream.Length)
{
2026-01-29 15:23:53 +00:00
return streamPosition - bufferStream.Length + bufferStream.Position;
}
2026-01-29 15:23:53 +00:00
return streamPosition;
}
2026-01-29 15:23:53 +00:00
set
{
2026-01-29 15:23:53 +00:00
long bufferStart = streamPosition - bufferStream.Length;
long bufferEnd = streamPosition;
if (value >= bufferStart && value < bufferEnd)
{
2026-01-29 15:23:53 +00:00
isRewound = true;
bufferStream.Position = value - bufferStart;
}
2026-01-29 15:23:53 +00:00
else
{
2026-01-29 15:23:53 +00:00
throw new NotSupportedException("Cannot seek outside buffered region.");
}
}
2026-01-29 15:23:53 +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-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);
if (read < readCount)
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:50:35 +00:00
2026-01-29 15:23:53 +00:00
read = stream.Read(buffer, offset, count);
if (IsRecording)
{
2026-01-29 15:23:53 +00:00
bufferStream.Write(buffer, offset, read);
}
2026-01-29 15:23:53 +00:00
streamPosition += read;
return read;
}
2026-01-29 15:23:53 +00:00
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
2026-01-29 15:23:53 +00:00
public override void SetLength(long value) => throw new NotSupportedException();
2026-01-29 15:23:53 +00:00
public override void Write(byte[] buffer, int offset, int count) =>
throw new NotSupportedException();
}