mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-02-10 13:39:12 +00:00
Especially for streams, it is more appropriate to throw NotSupportedException instead of NotImplementedException. Usually consumers of streams expect NotSupportedException to handle errors. There are other places that also use NotImplementedException but I didn't examine them for now. I only modified stream classes in SharpCompress.IO. For reference about this best practise, please see these articles: http://blogs.msdn.com/b/brada/archive/2004/07/29/201354.aspx http://blogs.msdn.com/b/jaredpar/archive/2008/12/12/notimplementedexception-vs-notsupportedexception.aspx
163 lines
4.7 KiB
C#
163 lines
4.7 KiB
C#
using System.IO;
|
|
|
|
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;
|
|
}
|
|
|
|
public override bool CanRead
|
|
{
|
|
get { return true; }
|
|
}
|
|
|
|
public override bool CanSeek
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
public override bool CanWrite
|
|
{
|
|
get { return false; }
|
|
}
|
|
|
|
public override void Flush()
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override long Length
|
|
{
|
|
get { throw new System.NotSupportedException(); }
|
|
}
|
|
|
|
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)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override void SetLength(long value)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
|
|
public override void Write(byte[] buffer, int offset, int count)
|
|
{
|
|
throw new System.NotSupportedException();
|
|
}
|
|
}
|
|
} |