tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
CachedResponseStream.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3using System.Net.Http;
4using System.Threading.Tasks;
5
7{
11 public sealed class CachedResponseStream : Stream
12 {
16 readonly HttpContent responseContent;
17
22
28 public static async ValueTask<CachedResponseStream> Create(HttpResponseMessage response)
29 {
30 if (response == null)
31 throw new ArgumentNullException(nameof(response));
32
33 using (response)
34 {
35 var content = response.Content;
36 response.Content = null;
37 try
38 {
39 // don't cry about the missing CancellationToken overload: https://github.com/dotnet/runtime/issues/916
40 var responseStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
41 return new CachedResponseStream(content, responseStream);
42 }
43 catch
44 {
45 content.Dispose();
46 throw;
47 }
48 }
49 }
50
52 public override bool CanRead => responseStream.CanRead;
53
55 public override bool CanSeek => responseStream.CanSeek;
56
58 public override bool CanWrite => responseStream.CanWrite;
59
61 public override long Length => responseStream.Length;
62
64 public override long Position
65 {
66 get => responseStream.Position;
67 set => responseStream.Position = value;
68 }
69
71 public override void Flush() => responseStream.Flush();
72
74 public override int Read(byte[] buffer, int offset, int count) => responseStream.Read(buffer, offset, count);
75
77 public override long Seek(long offset, SeekOrigin origin) => responseStream.Seek(offset, origin);
78
80 public override void SetLength(long value) => responseStream.SetLength(value);
81
83 public override void Write(byte[] buffer, int offset, int count) => responseStream.Write(buffer, offset, count);
84
86 protected override void Dispose(bool disposing)
87 {
88 base.Dispose(disposing);
89 if (!disposing)
90 return;
91
92 responseContent.Dispose();
93 responseStream.Dispose();
94 }
95
102 {
103 this.responseContent = responseContent;
104 this.responseStream = responseStream;
105 }
106 }
107}
Caches the Stream from a HttpResponseMessage for later use.
override int Read(byte[] buffer, int offset, int count)
static async ValueTask< CachedResponseStream > Create(HttpResponseMessage response)
Asyncronously creates a new CachedResponseStream.
override void Write(byte[] buffer, int offset, int count)
readonly HttpContent responseContent
The HttpContent for the CachedResponseStream.
readonly Stream responseStream
The reponse content Stream.
CachedResponseStream(HttpContent responseContent, Stream responseStream)
Initializes a new instance of the CachedResponseStream class.
override long Seek(long offset, SeekOrigin origin)