-
Notifications
You must be signed in to change notification settings - Fork 2
/
SophonAsset.Stream.cs
198 lines (165 loc) · 6.78 KB
/
SophonAsset.Stream.cs
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
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable ConvertIfStatementToNullCoalescingAssignment
// ReSharper disable OptionalParameterHierarchyMismatch
// ReSharper disable IdentifierTypo
// ReSharper disable StringLiteralTypo
// ReSharper disable CommentTypo
namespace Hi3Helper.Sophon
{
public class SophonAssetStream : Stream
{
#region Initializers
private protected HttpRequestMessage NetworkRequest;
private protected HttpResponseMessage NetworkResponse;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private protected Stream NetworkStream;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
public HttpStatusCode StatusCode;
public bool IsSuccessStatusCode;
#endregion
internal static async Task<Stream> CreateStreamAsync(HttpClient client, string url, long? startOffset,
long? endOffset, CancellationToken token)
{
if (startOffset == null)
{
startOffset = 0;
}
SophonAssetStream httpResponseInputStream = new SophonAssetStream();
httpResponseInputStream.NetworkRequest = new HttpRequestMessage
{
RequestUri = new Uri(url),
Method = HttpMethod.Get
};
token.ThrowIfCancellationRequested();
httpResponseInputStream.NetworkRequest.Headers.Range = new RangeHeaderValue(startOffset, endOffset);
httpResponseInputStream.NetworkResponse = await client
.SendAsync(httpResponseInputStream.NetworkRequest, HttpCompletionOption.ResponseHeadersRead, token);
httpResponseInputStream.StatusCode = httpResponseInputStream.NetworkResponse.StatusCode;
httpResponseInputStream.IsSuccessStatusCode = httpResponseInputStream.NetworkResponse.IsSuccessStatusCode;
if (!httpResponseInputStream.IsSuccessStatusCode)
{
throw new
HttpRequestException("Http request to the manifest file returns a non-successful status!"
#if NET6_0_OR_GREATER
,
null, httpResponseInputStream.StatusCode
#endif
);
}
if (httpResponseInputStream == null)
{
throw new NullReferenceException("Http response message returns a null entry");
}
if (httpResponseInputStream.IsSuccessStatusCode)
{
#if NET6_0_OR_GREATER
httpResponseInputStream.NetworkStream = await httpResponseInputStream.NetworkResponse
.Content.ReadAsStreamAsync(token);
#else
httpResponseInputStream.NetworkStream = await httpResponseInputStream.NetworkResponse
.Content.ReadAsStreamAsync();
#endif
return httpResponseInputStream;
}
if ((int)httpResponseInputStream.StatusCode != 416)
{
throw new
HttpRequestException(string.Format("HttpResponse for URL: \"{1}\" has returned unsuccessful code: {0}",
httpResponseInputStream.NetworkResponse.StatusCode, url));
}
#if NET6_0_OR_GREATER
await httpResponseInputStream.DisposeAsync();
#else
httpResponseInputStream.Dispose();
#endif
return null;
}
~SophonAssetStream()
{
Dispose();
}
#if NET6_0_OR_GREATER
public override async ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken =
default)
{
return await NetworkStream.ReadAsync(buffer, cancellationToken);
}
public override int Read(Span<byte> buffer)
{
return NetworkStream.Read(buffer);
}
public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
throw new NotSupportedException();
}
public override void Write(ReadOnlySpan<byte> buffer)
{
throw new NotSupportedException();
}
#endif
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
CancellationToken cancellationToken = default)
{
return await NetworkStream.ReadAsync(buffer, offset, count, cancellationToken);
}
public override int Read(byte[] buffer, int offset, int count)
{
return NetworkStream.Read(buffer, offset, count);
}
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override void Flush()
{
NetworkStream.Flush();
}
public override long Length => throw new NotSupportedException();
public override long Position
{
get => throw new NotSupportedException();
set => throw new NotSupportedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotSupportedException();
}
public override void SetLength(long value)
{
throw new NotSupportedException();
}
protected override void Dispose(bool disposing)
{
if (!disposing)
{
return;
}
NetworkRequest.Dispose();
NetworkResponse?.Dispose();
NetworkStream?.Dispose();
}
#if NET6_0_OR_GREATER
public override async ValueTask DisposeAsync()
{
NetworkRequest.Dispose();
NetworkResponse?.Dispose();
if (NetworkStream != null)
await NetworkStream.DisposeAsync();
GC.SuppressFinalize(this);
}
#endif
}
}