2016-09-26 10:55:52 +01:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
|
|
|
|
|
namespace SharpCompress.IO
|
|
|
|
|
|
{
|
|
|
|
|
|
internal class RewindableStream : Stream
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Stream stream;
|
|
|
|
|
|
private MemoryStream bufferStream = new MemoryStream();
|
|
|
|
|
|
private bool isRewound;
|
|
|
|
|
|
private bool isDisposed;
|
|
|
|
|
|
|
|
|
|
|
|
public RewindableStream(Stream stream)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.stream = stream;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal bool IsRecording { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (isDisposed)
|
|
|
|
|
|
{
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
isDisposed = true;
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
if (disposing)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Rewind(bool stopRecording)
|
|
|
|
|
|
{
|
|
|
|
|
|
isRewound = true;
|
|
|
|
|
|
IsRecording = !stopRecording;
|
|
|
|
|
|
bufferStream.Position = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Rewind(MemoryStream buffer)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (bufferStream.Position >= buffer.Length)
|
|
|
|
|
|
{
|
|
|
|
|
|
bufferStream.Position -= buffer.Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
bufferStream.TransferTo(buffer);
|
|
|
|
|
|
bufferStream = buffer;
|
|
|
|
|
|
bufferStream.Position = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
isRewound = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void StartRecording()
|
|
|
|
|
|
{
|
|
|
|
|
|
//if (isRewound && bufferStream.Position != 0)
|
|
|
|
|
|
// throw new System.NotImplementedException();
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
IsRecording = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-26 10:55:52 +01:00
|
|
|
|
public override bool CanRead { get { return true; } }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2016-08-12 11:56:49 +01:00
|
|
|
|
public override bool CanSeek => stream.CanSeek;
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
2016-09-26 10:55:52 +01:00
|
|
|
|
public override bool CanWrite { get { return false; } }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
|
|
|
|
|
public override void Flush()
|
|
|
|
|
|
{
|
2016-09-26 10:55:52 +01:00
|
|
|
|
throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-09-26 10:55:52 +01:00
|
|
|
|
public override long Length { get { throw new NotSupportedException(); } }
|
2015-12-30 11:19:42 +00:00
|
|
|
|
|
|
|
|
|
|
public override long Position
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return stream.Position + bufferStream.Position - bufferStream.Length; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!isRewound)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Position = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (value < stream.Position - bufferStream.Length || value >= stream.Position)
|
|
|
|
|
|
{
|
|
|
|
|
|
stream.Position = value;
|
|
|
|
|
|
isRewound = false;
|
|
|
|
|
|
bufferStream.SetLength(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
bufferStream.Position = value - stream.Position + bufferStream.Length;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
|
|
|
|
|
int read;
|
|
|
|
|
|
if (isRewound && bufferStream.Position != bufferStream.Length)
|
|
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
read += tempRead;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (bufferStream.Position == bufferStream.Length && !IsRecording)
|
|
|
|
|
|
{
|
|
|
|
|
|
isRewound = false;
|
|
|
|
|
|
bufferStream.SetLength(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return read;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
read = stream.Read(buffer, offset, count);
|
|
|
|
|
|
if (IsRecording)
|
|
|
|
|
|
{
|
|
|
|
|
|
bufferStream.Write(buffer, offset, read);
|
|
|
|
|
|
}
|
|
|
|
|
|
return read;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
|
{
|
2016-09-26 10:55:52 +01:00
|
|
|
|
throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void SetLength(long value)
|
|
|
|
|
|
{
|
2016-09-26 10:55:52 +01:00
|
|
|
|
throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
|
|
|
|
{
|
2016-09-26 10:55:52 +01:00
|
|
|
|
throw new NotSupportedException();
|
2015-12-30 11:19:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-04-28 12:32:55 +01:00
|
|
|
|
}
|