Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DeflateManagedStream.cs
Go to the documentation of this file.
4
6
7internal sealed class DeflateManagedStream : Stream
8{
9 private Stream _stream;
10
12
13 private readonly byte[] _buffer;
14
15 private int _asyncOperations;
16
17 public override bool CanRead
18 {
19 get
20 {
21 if (_stream == null)
22 {
23 return false;
24 }
25 return _stream.CanRead;
26 }
27 }
28
29 public override bool CanWrite => false;
30
31 public override bool CanSeek => false;
32
33 public override long Length
34 {
35 get
36 {
38 }
39 }
40
41 public override long Position
42 {
43 get
44 {
46 }
47 set
48 {
50 }
51 }
52
53 internal DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize = -1L)
54 {
55 if (stream == null)
56 {
57 throw new ArgumentNullException("stream");
58 }
59 if (!stream.CanRead)
60 {
62 }
63 if (!stream.CanRead)
64 {
66 }
67 _inflater = new InflaterManaged(method == ZipArchiveEntry.CompressionMethodValues.Deflate64, uncompressedSize);
69 _buffer = new byte[8192];
70 }
71
72 public override void Flush()
73 {
75 }
76
78 {
80 if (!cancellationToken.IsCancellationRequested)
81 {
82 return Task.CompletedTask;
83 }
85 }
86
87 public override long Seek(long offset, SeekOrigin origin)
88 {
90 }
91
92 public override void SetLength(long value)
93 {
95 }
96
97 public override int Read(byte[] buffer, int offset, int count)
98 {
100 return Read(new Span<byte>(buffer, offset, count));
101 }
102
103 public override int Read(Span<byte> buffer)
104 {
106 int length = buffer.Length;
107 while (true)
108 {
110 buffer = buffer.Slice(start);
111 if (buffer.Length == 0 || _inflater.Finished())
112 {
113 break;
114 }
115 int num = _stream.Read(_buffer, 0, _buffer.Length);
116 if (num <= 0)
117 {
118 break;
119 }
120 if (num > _buffer.Length)
121 {
123 }
124 _inflater.SetInput(_buffer, 0, num);
125 }
126 return length - buffer.Length;
127 }
128
129 public override int ReadByte()
130 {
131 byte reference = 0;
132 if (Read(MemoryMarshal.CreateSpan(ref reference, 1)) != 1)
133 {
134 return -1;
135 }
136 return reference;
137 }
138
139 private void EnsureNotDisposed()
140 {
141 if (_stream == null)
142 {
144 }
145 }
146
147 private static void ThrowStreamClosedException()
148 {
150 }
151
152 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
153 {
154 return System.Threading.Tasks.TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
155 }
156
157 public override int EndRead(IAsyncResult asyncResult)
158 {
160 }
161
163 {
164 if (cancellationToken.IsCancellationRequested)
165 {
167 }
169 bool flag = false;
170 try
171 {
172 int num = _inflater.Inflate(buffer.Span);
173 if (num != 0)
174 {
175 return ValueTask.FromResult(num);
176 }
177 if (_inflater.Finished())
178 {
179 return ValueTask.FromResult(0);
180 }
182 flag = true;
183 return ReadAsyncCore(readTask, buffer, cancellationToken);
184 }
185 finally
186 {
187 if (!flag)
188 {
190 }
191 }
192 }
193
195 {
196 try
197 {
198 int num;
199 while (true)
200 {
201 num = await readTask.ConfigureAwait(continueOnCapturedContext: false);
203 if (num <= 0)
204 {
205 return 0;
206 }
207 if (num > _buffer.Length)
208 {
210 }
211 cancellationToken.ThrowIfCancellationRequested();
212 _inflater.SetInput(_buffer, 0, num);
213 num = _inflater.Inflate(buffer.Span);
214 if (num != 0 || _inflater.Finished())
215 {
216 break;
217 }
218 readTask = _stream.ReadAsync(_buffer.AsMemory(), cancellationToken);
219 }
220 return num;
221 }
222 finally
223 {
225 }
226 }
227
238
248
249 public override void Write(byte[] buffer, int offset, int count)
250 {
252 }
253
254 private void PurgeBuffers(bool disposing)
255 {
256 if (disposing && _stream != null)
257 {
258 Flush();
259 }
260 }
261
262 protected override void Dispose(bool disposing)
263 {
264 try
265 {
266 PurgeBuffers(disposing);
267 }
268 finally
269 {
270 try
271 {
272 if (disposing && _stream != null)
273 {
275 }
276 }
277 finally
278 {
279 _stream = null;
280 try
281 {
283 }
284 finally
285 {
286 _inflater = null;
287 base.Dispose(disposing);
288 }
289 }
290 }
291 }
292}
override Task FlushAsync(CancellationToken cancellationToken)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override int Read(byte[] buffer, int offset, int count)
override void Write(byte[] buffer, int offset, int count)
DeflateManagedStream(Stream stream, ZipArchiveEntry.CompressionMethodValues method, long uncompressedSize=-1L)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback asyncCallback, object asyncState)
ValueTask< int > ReadAsyncInternal(Memory< byte > buffer, CancellationToken cancellationToken)
async ValueTask< int > ReadAsyncCore(ValueTask< int > readTask, Memory< byte > buffer, CancellationToken cancellationToken)
override long Seek(long offset, SeekOrigin origin)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override int EndRead(IAsyncResult asyncResult)
void SetInput(byte[] inputBytes, int offset, int length)
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
int Read(byte[] buffer, int offset, int count)
void Dispose()
Definition Stream.cs:639
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
static string ObjectDisposed_StreamClosed
Definition SR.cs:20
static string GenericInvalidData
Definition SR.cs:20
static string NotSupported_UnreadableStream
Definition SR.cs:32
static string InvalidBeginCall
Definition SR.cs:22
static string CannotWriteToDeflateStream
Definition SR.cs:18
static string NotSupported
Definition SR.cs:28
Definition SR.cs:7
static int Decrement(ref int location)
static int Increment(ref int location)
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312