3 public sealed
class StreamWrapper(Stream Wrapped, IDisposable? AdditionalDisposable) : Stream
5 private readonly Stream wrapped = Wrapped;
6 private IDisposable? additionalDisposable = AdditionalDisposable;
8 public event EventHandler? Disposed;
10 public StreamWrapper(Stream Wrapped) : this(Wrapped, null)
14 public override long Position
16 get => this.wrapped.Position;
17 set => this.wrapped.Position = value;
20 public override long Length => this.wrapped.Length;
21 public override bool CanRead => this.wrapped.CanRead;
22 public override bool CanSeek => this.wrapped.CanSeek;
23 public override bool CanWrite => this.wrapped.CanWrite;
25 public override void Flush() => this.wrapped.Flush();
26 public override int Read(
byte[] Buffer,
int Offset,
int Count) => this.wrapped.Read(Buffer, Offset, Count);
27 public override long Seek(
long Offset, SeekOrigin Origin) => this.wrapped.Seek(Offset, Origin);
28 public override void SetLength(
long Value) => this.wrapped.SetLength(Value);
29 public override void Write(
byte[] Buffer,
int Offset,
int Count) => this.wrapped.Write(Buffer, Offset, Count);
31 protected override void Dispose(
bool Disposing)
33 this.wrapped.Dispose();
35 Disposed?.Invoke(
this, EventArgs.Empty);
37 this.additionalDisposable?.Dispose();
38 this.additionalDisposable =
null;
40 base.Dispose(Disposing);
43 public static async Task<Stream?> GetStreamAsync(Uri uri, HttpClient client, CancellationToken cancellationToken)
45 HttpResponseMessage response = await client.GetAsync(uri, cancellationToken).ConfigureAwait(
false);
47 if (!response.IsSuccessStatusCode)
54 return new StreamWrapper(await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(
false), response);