File size: 8,012 Bytes
68dd90e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
using System.IO;
using ScreenGrab.Models;
namespace ScreenGrab.Utilities;
/// <summary>
/// A <see cref="Stream" /> that wraps another stream. The major feature of <see cref="WrappingStream" /> is that it
/// does not dispose the
/// underlying stream when it is disposed; this is useful when using classes such as <see cref="BinaryReader" /> and
/// <see cref="System.Security.Cryptography.CryptoStream" /> that take ownership of the stream passed to their
/// constructors.
/// </summary>
public class WrappingStream : Stream
{
/// <summary>
/// Initializes a new instance of the <see cref="WrappingStream" /> class.
/// </summary>
/// <param name="streamBase">The wrapped stream.</param>
public WrappingStream(Stream streamBase)
{
// check parameters
if (streamBase == null)
throw new ArgumentNullException("streamBase");
WrappedStream = streamBase;
}
/// <summary>
/// Gets a value indicating whether the current stream supports reading.
/// </summary>
/// <returns><c>true</c> if the stream supports reading; otherwise, <c>false</c>.</returns>
public override bool CanRead => WrappedStream == null ? false : WrappedStream.CanRead;
/// <summary>
/// Gets a value indicating whether the current stream supports seeking.
/// </summary>
/// <returns><c>true</c> if the stream supports seeking; otherwise, <c>false</c>.</returns>
public override bool CanSeek => WrappedStream == null ? false : WrappedStream.CanSeek;
/// <summary>
/// Gets a value indicating whether the current stream supports writing.
/// </summary>
/// <returns><c>true</c> if the stream supports writing; otherwise, <c>false</c>.</returns>
public override bool CanWrite => WrappedStream == null ? false : WrappedStream.CanWrite;
/// <summary>
/// Gets the length in bytes of the stream.
/// </summary>
public override long Length
{
get
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Length;
return 0;
}
}
/// <summary>
/// Gets or sets the position within the current stream.
/// </summary>
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;
}
}
/// <summary>
/// Gets the wrapped stream.
/// </summary>
/// <value>The wrapped stream.</value>
protected Stream? WrappedStream { get; private set; }
/// <summary>
/// Begins an asynchronous read operation.
/// </summary>
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();
}
/// <summary>
/// Begins an asynchronous write operation.
/// </summary>
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();
}
/// <summary>
/// Waits for the pending asynchronous read to complete.
/// </summary>
public override int EndRead(IAsyncResult asyncResult)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.EndRead(asyncResult);
return 0;
}
/// <summary>
/// Ends an asynchronous write operation.
/// </summary>
public override void EndWrite(IAsyncResult asyncResult)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.EndWrite(asyncResult);
}
/// <summary>
/// Clears all buffers for this stream and causes any buffered data to be written to the underlying device.
/// </summary>
public override void Flush()
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.Flush();
}
/// <summary>
/// Reads a sequence of bytes from the current stream and advances the position
/// within the stream by the number of bytes read.
/// </summary>
public override int Read(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Read(buffer, offset, count);
return 0;
}
/// <summary>
/// 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.
/// </summary>
public override int ReadByte()
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.ReadByte();
return 0;
}
/// <summary>
/// Sets the position within the current stream.
/// </summary>
/// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
/// <param name="origin">
/// A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to
/// obtain the new position.
/// </param>
/// <returns>The new position within the current stream.</returns>
public override long Seek(long offset, SeekOrigin origin)
{
ThrowIfDisposed();
if (WrappedStream is not null)
return WrappedStream.Seek(offset, origin);
return 0;
}
/// <summary>
/// Sets the length of the current stream.
/// </summary>
/// <param name="value">The desired length of the current stream in bytes.</param>
public override void SetLength(long value)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.SetLength(value);
}
/// <summary>
/// Writes a sequence of bytes to the current stream and advances the current position
/// within this stream by the number of bytes written.
/// </summary>
public override void Write(byte[] buffer, int offset, int count)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.Write(buffer, offset, count);
}
/// <summary>
/// Writes a byte to the current position in the stream and advances the position within the stream by one byte.
/// </summary>
public override void WriteByte(byte value)
{
ThrowIfDisposed();
if (WrappedStream is not null)
WrappedStream.WriteByte(value);
}
/// <summary>
/// Releases the unmanaged resources used by the <see cref="WrappingStream" /> and optionally releases the managed
/// resources.
/// </summary>
/// <param name="disposing">
/// true to release both managed and unmanaged resources; false to release only unmanaged
/// resources.
/// </param>
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);
}
} |