using System;
using System.IO;
namespace NAudio.Utils
{
///
/// Pass-through stream that ignores Dispose
/// Useful for dealing with MemoryStreams that you want to re-use
///
public class IgnoreDisposeStream : Stream
{
///
/// The source stream all other methods fall through to
///
public Stream SourceStream { get; private set; }
///
/// If true the Dispose will be ignored, if false, will pass through to the SourceStream
/// Set to true by default
///
public bool IgnoreDispose { get; set; }
///
/// Creates a new IgnoreDisposeStream
///
/// The source stream
public IgnoreDisposeStream(Stream sourceStream)
{
SourceStream = sourceStream;
IgnoreDispose = true;
}
///
/// Can Read
///
public override bool CanRead => SourceStream.CanRead;
///
/// Can Seek
///
public override bool CanSeek => SourceStream.CanSeek;
///
/// Can write to the underlying stream
///
public override bool CanWrite => SourceStream.CanWrite;
///
/// Flushes the underlying stream
///
public override void Flush()
{
SourceStream.Flush();
}
///
/// Gets the length of the underlying stream
///
public override long Length => SourceStream.Length;
///
/// Gets or sets the position of the underlying stream
///
public override long Position
{
get
{
return SourceStream.Position;
}
set
{
SourceStream.Position = value;
}
}
///
/// Reads from the underlying stream
///
public override int Read(byte[] buffer, int offset, int count)
{
return SourceStream.Read(buffer, offset, count);
}
///
/// Seeks on the underlying stream
///
public override long Seek(long offset, SeekOrigin origin)
{
return SourceStream.Seek(offset, origin);
}
///
/// Sets the length of the underlying stream
///
public override void SetLength(long value)
{
SourceStream.SetLength(value);
}
///
/// Writes to the underlying stream
///
public override void Write(byte[] buffer, int offset, int count)
{
SourceStream.Write(buffer, offset, count);
}
///
/// Dispose - by default (IgnoreDispose = true) will do nothing,
/// leaving the underlying stream undisposed
///
protected override void Dispose(bool disposing)
{
if (!IgnoreDispose)
{
SourceStream.Dispose();
SourceStream = null;
}
}
}
}