using System.IO;
using ScreenGrab.Models;
namespace ScreenGrab.Utilities;
///
/// A that wraps another stream. The major feature of is that it
/// does not dispose the
/// underlying stream when it is disposed; this is useful when using classes such as and
/// that take ownership of the stream passed to their
/// constructors.
///
public class WrappingStream : Stream
{
///
/// Initializes a new instance of the class.
///
/// The wrapped stream.
public WrappingStream(Stream streamBase)
{
// check parameters
if (streamBase == null)
throw new ArgumentNullException("streamBase");
WrappedStream = streamBase;
}
///
/// Gets a value indicating whether the current stream supports reading.
///
/// true if the stream supports reading; otherwise, false.
public override bool CanRead => WrappedStream == null ? false : WrappedStream.CanRead;
///
/// Gets a value indicating whether the current stream supports seeking.
///
/// true if the stream supports seeking; otherwise, false.
public override bool CanSeek => WrappedStream == null ? false : WrappedStream.CanSeek;
///
/// Gets a value indicating whether the current stream supports writing.
///
/// true if the stream supports writing; otherwise, false.
public override bool CanWrite => WrappedStream == null ? false : WrappedStream.CanWrite;
///
/// Gets the length in bytes of the stream.
///
public override long Length
{
get
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Length;
return 0;
}
}
///
/// Gets or sets the position within the current stream.
///
public override long Position
{
get
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Position;
return 0;
}
set
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.Position = value;
}
}
///
/// Gets the wrapped stream.
///
/// The wrapped stream.
protected Stream? WrappedStream { get; private set; }
///
/// Begins an asynchronous read operation.
///
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
{
ThrowIfDisposed();
if (WrappedStream is not null && callback is not null && state is not null)
return WrappedStream.BeginRead(buffer, offset, count, callback, state);
return new NullAsyncResult();
}
///
/// Begins an asynchronous write operation.
///
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback,
object? state)
{
ThrowIfDisposed();
if (WrappedStream is not null && callback is not null && state is not null)
return WrappedStream.BeginWrite(buffer, offset, count, callback, state);
return new NullAsyncResult();
}
///
/// Waits for the pending asynchronous read to complete.
///
public override int EndRead(IAsyncResult asyncResult)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.EndRead(asyncResult);
return 0;
}
///
/// Ends an asynchronous write operation.
///
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.EndWrite(asyncResult);
}
///
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
///
public override void Flush()
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.Flush();
}
///
/// Reads a sequence of bytes from the current stream and advances the position
/// within the stream by the number of bytes read.
///
public override int Read(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Read(buffer, offset, count);
return 0;
}
///
/// Reads a byte from the stream and advances the position within the stream by one byte, or returns -1 if at the end
/// of the stream.
///
public override int ReadByte()
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.ReadByte();
return 0;
}
///
/// Sets the position within the current stream.
///
/// A byte offset relative to the parameter.
///
/// A value of type indicating the reference point used to
/// obtain the new position.
///
/// The new position within the current stream.
public override long Seek(long offset, SeekOrigin origin)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Seek(offset, origin);
return 0;
}
///
/// Sets the length of the current stream.
///
/// The desired length of the current stream in bytes.
public override void SetLength(long value)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.SetLength(value);
}
///
/// Writes a sequence of bytes to the current stream and advances the current position
/// within this stream by the number of bytes written.
///
public override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.Write(buffer, offset, count);
}
///
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
///
public override void WriteByte(byte value)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.WriteByte(value);
}
///
/// Releases the unmanaged resources used by the and optionally releases the managed
/// resources.
///
///
/// true to release both managed and unmanaged resources; false to release only unmanaged
/// resources.
///
protected override void Dispose(bool disposing)
{
// doesn't close the base stream, but just prevents access to it through this WrappingStream
if (disposing)
WrappedStream = null;
base.Dispose(disposing);
}
private void ThrowIfDisposed()
{
// throws an ObjectDisposedException if this object has been disposed
if (WrappedStream == null)
throw new ObjectDisposedException(GetType().Name);
}
}