Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
DeflateStream.cs
Go to the documentation of this file.
6
8
9public class DeflateStream : Stream
10{
11 private sealed class CopyToStream : Stream
12 {
13 private readonly DeflateStream _deflateStream;
14
15 private readonly Stream _destination;
16
18
19 private byte[] _arrayPoolBuffer;
20
21 public override bool CanWrite => true;
22
23 public override bool CanRead => false;
24
25 public override bool CanSeek => false;
26
27 public override long Length
28 {
29 get
30 {
31 throw new NotSupportedException();
32 }
33 }
34
35 public override long Position
36 {
37 get
38 {
39 throw new NotSupportedException();
40 }
41 set
42 {
43 throw new NotSupportedException();
44 }
45 }
46
47 public CopyToStream(DeflateStream deflateStream, Stream destination, int bufferSize)
48 : this(deflateStream, destination, bufferSize, CancellationToken.None)
49 {
50 }
51
59
61 {
63 try
64 {
66 {
68 if (num > 0)
69 {
70 await _destination.WriteAsync(new ReadOnlyMemory<byte>(_arrayPoolBuffer, 0, num), _cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
71 }
73 {
74 break;
75 }
76 }
77 await _deflateStream._stream.CopyToAsync(this, _arrayPoolBuffer.Length, _cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
78 }
79 finally
80 {
83 _arrayPoolBuffer = null;
84 }
85 }
86
88 {
89 try
90 {
92 {
94 if (num > 0)
95 {
97 }
99 {
100 break;
101 }
102 }
104 }
105 finally
106 {
108 _arrayPoolBuffer = null;
109 }
110 }
111
113 {
115 if (count <= 0)
116 {
117 return Task.CompletedTask;
118 }
119 if (count > buffer.Length - offset)
120 {
122 }
124 }
125
131
133 {
136 {
138 if (num > 0)
139 {
140 await _destination.WriteAsync(new ReadOnlyMemory<byte>(_arrayPoolBuffer, 0, num), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
141 }
143 {
144 break;
145 }
146 }
147 }
148
149 public override void Write(byte[] buffer, int offset, int count)
150 {
152 if (count <= 0)
153 {
154 return;
155 }
156 if (count > buffer.Length - offset)
157 {
159 }
162 {
164 if (num > 0)
165 {
167 }
169 {
170 break;
171 }
172 }
173 }
174
175 public override void Flush()
176 {
177 }
178
179 public override int Read(byte[] buffer, int offset, int count)
180 {
181 throw new NotSupportedException();
182 }
183
184 public override long Seek(long offset, SeekOrigin origin)
185 {
186 throw new NotSupportedException();
187 }
188
189 public override void SetLength(long value)
190 {
191 throw new NotSupportedException();
192 }
193 }
194
196
198
199 private bool _leaveOpen;
200
202
204
205 private byte[] _buffer;
206
208
209 private bool _wroteBytes;
210
212
213 public override bool CanRead
214 {
215 get
216 {
217 if (_stream == null)
218 {
219 return false;
220 }
221 if (_mode == CompressionMode.Decompress)
222 {
223 return _stream.CanRead;
224 }
225 return false;
226 }
227 }
228
229 public override bool CanWrite
230 {
231 get
232 {
233 if (_stream == null)
234 {
235 return false;
236 }
237 if (_mode == CompressionMode.Compress)
238 {
239 return _stream.CanWrite;
240 }
241 return false;
242 }
243 }
244
245 public override bool CanSeek => false;
246
247 public override long Length
248 {
249 get
250 {
252 }
253 }
254
255 public override long Position
256 {
257 get
258 {
260 }
261 set
262 {
264 }
265 }
266
268 {
269 get
270 {
271 if (_inflater.Finished())
272 {
274 {
275 return !_inflater.NeedsInput();
276 }
277 return true;
278 }
279 return false;
280 }
281 }
282
284
285 internal DeflateStream(Stream stream, CompressionMode mode, long uncompressedSize)
286 : this(stream, mode, leaveOpen: false, -15, uncompressedSize)
287 {
288 }
289
291 : this(stream, mode, leaveOpen: false)
292 {
293 }
294
295 public DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
296 : this(stream, mode, leaveOpen, -15, -1L)
297 {
298 }
299
300 public DeflateStream(Stream stream, CompressionLevel compressionLevel)
301 : this(stream, compressionLevel, leaveOpen: false)
302 {
303 }
304
305 public DeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen)
306 : this(stream, compressionLevel, leaveOpen, -15)
307 {
308 }
309
310 internal DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, int windowBits, long uncompressedSize = -1L)
311 {
312 if (stream == null)
313 {
314 throw new ArgumentNullException("stream");
315 }
316 switch (mode)
317 {
318 case CompressionMode.Decompress:
319 if (!stream.CanRead)
320 {
322 }
323 _inflater = new Inflater(windowBits, uncompressedSize);
324 _stream = stream;
325 _mode = CompressionMode.Decompress;
326 _leaveOpen = leaveOpen;
327 break;
328 case CompressionMode.Compress:
329 InitializeDeflater(stream, leaveOpen, windowBits, CompressionLevel.Optimal);
330 break;
331 default:
333 }
334 }
335
336 internal DeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen, int windowBits)
337 {
338 if (stream == null)
339 {
340 throw new ArgumentNullException("stream");
341 }
342 InitializeDeflater(stream, leaveOpen, windowBits, compressionLevel);
343 }
344
345 [MemberNotNull("_stream")]
346 internal void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
347 {
348 if (!stream.CanWrite)
349 {
351 }
352 _deflater = new Deflater(compressionLevel, windowBits);
353 _stream = stream;
354 _mode = CompressionMode.Compress;
355 _leaveOpen = leaveOpen;
357 }
358
359 [MemberNotNull("_buffer")]
360 private void InitializeBuffer()
361 {
362 _buffer = ArrayPool<byte>.Shared.Rent(8192);
363 }
364
365 [MemberNotNull("_buffer")]
367 {
368 if (_buffer == null)
369 {
371 }
372 }
373
374 public override void Flush()
375 {
377 if (_mode == CompressionMode.Compress)
378 {
379 FlushBuffers();
380 }
381 }
382
384 {
387 if (cancellationToken.IsCancellationRequested)
388 {
390 }
391 if (_mode == CompressionMode.Compress)
392 {
393 return Core(cancellationToken);
394 }
395 return Task.CompletedTask;
397 {
399 try
400 {
401 await WriteDeflaterOutputAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
402 bool flushSuccessful;
403 do
404 {
405 flushSuccessful = _deflater.Flush(_buffer, out var bytesRead);
406 if (flushSuccessful)
407 {
408 await _stream.WriteAsync(new ReadOnlyMemory<byte>(_buffer, 0, bytesRead), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
409 }
410 }
411 while (flushSuccessful);
412 await _stream.FlushAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
413 }
414 finally
415 {
417 }
418 }
419 }
420
421 public override long Seek(long offset, SeekOrigin origin)
422 {
424 }
425
426 public override void SetLength(long value)
427 {
429 }
430
431 public override int ReadByte()
432 {
435 if (!_inflater.Inflate(out var b))
436 {
437 return base.ReadByte();
438 }
439 return b;
440 }
441
442 public override int Read(byte[] buffer, int offset, int count)
443 {
445 return ReadCore(new Span<byte>(buffer, offset, count));
446 }
447
448 public override int Read(Span<byte> buffer)
449 {
450 if (GetType() != typeof(DeflateStream))
451 {
452 return base.Read(buffer);
453 }
454 return ReadCore(buffer);
455 }
456
458 {
462 int num;
463 do
464 {
465 num = _inflater.Inflate(buffer);
466 if (num != 0 || InflatorIsFinished)
467 {
468 break;
469 }
470 if (_inflater.NeedsInput())
471 {
472 int num2 = _stream.Read(_buffer, 0, _buffer.Length);
473 if (num2 <= 0)
474 {
475 break;
476 }
477 if (num2 > _buffer.Length)
478 {
480 }
481 else
482 {
483 _inflater.SetInput(_buffer, 0, num2);
484 }
485 }
486 }
487 while (!buffer.IsEmpty);
488 return num;
489 }
490
491 private void EnsureNotDisposed()
492 {
493 if (_stream == null)
494 {
495 ThrowStreamClosedException();
496 }
497 static void ThrowStreamClosedException()
498 {
500 }
501 }
502
504 {
505 if (_mode != 0)
506 {
507 ThrowCannotReadFromDeflateStreamException();
508 }
509 static void ThrowCannotReadFromDeflateStreamException()
510 {
512 }
513 }
514
516 {
517 if (_mode != CompressionMode.Compress)
518 {
519 ThrowCannotWriteToDeflateStreamException();
520 }
521 static void ThrowCannotWriteToDeflateStreamException()
522 {
524 }
525 }
526
527 private static void ThrowGenericInvalidData()
528 {
530 }
531
532 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
533 {
534 return System.Threading.Tasks.TaskToApm.Begin(ReadAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
535 }
536
543
549
551 {
552 if (GetType() != typeof(DeflateStream))
553 {
554 return base.ReadAsync(buffer, cancellationToken);
555 }
557 }
558
560 {
564 if (cancellationToken.IsCancellationRequested)
565 {
567 }
569 return Core(buffer, cancellationToken);
571 {
573 try
574 {
575 int bytesRead;
576 do
577 {
578 bytesRead = _inflater.Inflate(buffer.Span);
579 if (bytesRead != 0 || InflatorIsFinished)
580 {
581 break;
582 }
583 if (_inflater.NeedsInput())
584 {
585 int num = await _stream.ReadAsync(new Memory<byte>(_buffer, 0, _buffer.Length), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
586 if (num <= 0)
587 {
588 break;
589 }
590 if (num > _buffer.Length)
591 {
593 }
594 else
595 {
596 _inflater.SetInput(_buffer, 0, num);
597 }
598 }
599 }
600 while (!buffer.IsEmpty);
601 return bytesRead;
602 }
603 finally
604 {
606 }
607 }
608 }
609
610 public override void Write(byte[] buffer, int offset, int count)
611 {
614 }
615
616 public override void Write(ReadOnlySpan<byte> buffer)
617 {
618 if (GetType() != typeof(DeflateStream))
619 {
620 base.Write(buffer);
621 }
622 else
623 {
625 }
626 }
627
628 internal unsafe void WriteCore(ReadOnlySpan<byte> buffer)
629 {
633 fixed (byte* inputBufferPtr = &MemoryMarshal.GetReference(buffer))
634 {
635 _deflater.SetInput(inputBufferPtr, buffer.Length);
637 _wroteBytes = true;
638 }
639 }
640
641 private void WriteDeflaterOutput()
642 {
643 while (!_deflater.NeedsInput())
644 {
645 int deflateOutput = _deflater.GetDeflateOutput(_buffer);
646 if (deflateOutput > 0)
647 {
648 _stream.Write(_buffer, 0, deflateOutput);
649 }
650 }
651 }
652
653 private void FlushBuffers()
654 {
655 if (_wroteBytes)
656 {
658 bool flag;
659 do
660 {
661 flag = _deflater.Flush(_buffer, out var bytesRead);
662 if (flag)
663 {
664 _stream.Write(_buffer, 0, bytesRead);
665 }
666 }
667 while (flag);
668 }
669 _stream.Flush();
670 }
671
672 private void PurgeBuffers(bool disposing)
673 {
674 if (!disposing || _stream == null || _mode != CompressionMode.Compress)
675 {
676 return;
677 }
678 if (_wroteBytes)
679 {
681 bool flag;
682 do
683 {
684 flag = _deflater.Finish(_buffer, out var bytesRead);
685 if (bytesRead > 0)
686 {
687 _stream.Write(_buffer, 0, bytesRead);
688 }
689 }
690 while (!flag);
691 }
692 else
693 {
694 int bytesRead2;
695 while (!_deflater.Finish(_buffer, out bytesRead2))
696 {
697 }
698 }
699 }
700
702 {
703 if (_stream == null || _mode != CompressionMode.Compress)
704 {
705 return;
706 }
707 if (_wroteBytes)
708 {
709 await WriteDeflaterOutputAsync(default(CancellationToken)).ConfigureAwait(continueOnCapturedContext: false);
710 bool finished;
711 do
712 {
713 finished = _deflater.Finish(_buffer, out var bytesRead);
714 if (bytesRead > 0)
715 {
716 await _stream.WriteAsync(new ReadOnlyMemory<byte>(_buffer, 0, bytesRead)).ConfigureAwait(continueOnCapturedContext: false);
717 }
718 }
719 while (!finished);
720 }
721 else
722 {
723 int bytesRead2;
724 while (!_deflater.Finish(_buffer, out bytesRead2))
725 {
726 }
727 }
728 }
729
730 protected override void Dispose(bool disposing)
731 {
732 try
733 {
734 PurgeBuffers(disposing);
735 }
736 finally
737 {
738 try
739 {
740 if (disposing && !_leaveOpen)
741 {
742 _stream?.Dispose();
743 }
744 }
745 finally
746 {
747 _stream = null;
748 try
749 {
752 }
753 finally
754 {
755 _deflater = null;
756 _inflater = null;
757 byte[] buffer = _buffer;
758 if (buffer != null)
759 {
760 _buffer = null;
762 {
764 }
765 }
766 base.Dispose(disposing);
767 }
768 }
769 }
770 }
771
772 public override ValueTask DisposeAsync()
773 {
774 if (!(GetType() == typeof(DeflateStream)))
775 {
776 return base.DisposeAsync();
777 }
778 return Core();
779 async ValueTask Core()
780 {
781 try
782 {
783 await PurgeBuffersAsync().ConfigureAwait(continueOnCapturedContext: false);
784 }
785 finally
786 {
788 _stream = null;
789 try
790 {
791 if (!_leaveOpen && stream != null)
792 {
793 await stream.DisposeAsync().ConfigureAwait(continueOnCapturedContext: false);
794 }
795 }
796 finally
797 {
798 try
799 {
802 }
803 finally
804 {
805 _deflater = null;
806 _inflater = null;
807 byte[] buffer = _buffer;
808 if (buffer != null)
809 {
810 _buffer = null;
812 {
814 }
815 }
816 }
817 }
818 }
819 }
820 }
821
822 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
823 {
824 return System.Threading.Tasks.TaskToApm.Begin(WriteAsync(buffer, offset, count, CancellationToken.None), asyncCallback, asyncState);
825 }
826
833
839
841 {
842 if (GetType() != typeof(DeflateStream))
843 {
844 return base.WriteAsync(buffer, cancellationToken);
845 }
847 }
848
850 {
854 if (!cancellationToken.IsCancellationRequested)
855 {
856 return Core(buffer, cancellationToken);
857 }
860 {
862 try
863 {
864 await WriteDeflaterOutputAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
866 await WriteDeflaterOutputAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
867 _wroteBytes = true;
868 }
869 finally
870 {
872 }
873 }
874 }
875
877 {
878 while (!_deflater.NeedsInput())
879 {
880 int deflateOutput = _deflater.GetDeflateOutput(_buffer);
881 if (deflateOutput > 0)
882 {
883 await _stream.WriteAsync(new ReadOnlyMemory<byte>(_buffer, 0, deflateOutput), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
884 }
885 }
886 }
887
888 public override void CopyTo(Stream destination, int bufferSize)
889 {
892 if (!CanRead)
893 {
894 throw new NotSupportedException();
895 }
897 }
898
900 {
903 if (!CanRead)
904 {
905 throw new NotSupportedException();
906 }
908 if (cancellationToken.IsCancellationRequested)
909 {
911 }
913 }
914
916 {
918 {
920 }
921 }
922
924 {
926 {
928 }
929 }
930
932 {
934 }
935
936 private static void ThrowInvalidBeginCall()
937 {
939 }
940}
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
CopyToStream(DeflateStream deflateStream, Stream destination, int bufferSize)
override long Seek(long offset, SeekOrigin origin)
override int Read(byte[] buffer, int offset, int count)
async ValueTask WriteAsyncCore(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override void Write(byte[] buffer, int offset, int count)
CopyToStream(DeflateStream deflateStream, Stream destination, int bufferSize, CancellationToken cancellationToken)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override void EndWrite(IAsyncResult asyncResult)
void InitializeDeflater(Stream stream, bool leaveOpen, int windowBits, CompressionLevel compressionLevel)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override void SetLength(long value)
override Task FlushAsync(CancellationToken cancellationToken)
override void Write(byte[] buffer, int offset, int count)
DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen, int windowBits, long uncompressedSize=-1L)
ValueTask WriteAsyncMemory(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override int EndRead(IAsyncResult asyncResult)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
override void Write(ReadOnlySpan< byte > buffer)
override void CopyTo(Stream destination, int bufferSize)
override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
DeflateStream(Stream stream, CompressionMode mode, long uncompressedSize)
unsafe void WriteCore(ReadOnlySpan< byte > buffer)
async ValueTask WriteDeflaterOutputAsync(CancellationToken cancellationToken)
DeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen)
DeflateStream(Stream stream, CompressionLevel compressionLevel, bool leaveOpen, int windowBits)
DeflateStream(Stream stream, CompressionMode mode)
DeflateStream(Stream stream, CompressionMode mode, bool leaveOpen)
ValueTask< int > ReadAsyncMemory(Memory< byte > buffer, CancellationToken cancellationToken)
DeflateStream(Stream stream, CompressionLevel compressionLevel)
override long Seek(long offset, SeekOrigin origin)
override int Read(Span< byte > buffer)
int ReadCore(Span< byte > buffer)
override int Read(byte[] buffer, int offset, int count)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? asyncCallback, object? asyncState)
override void Dispose(bool disposing)
bool Finish(byte[] outputBuffer, out int bytesRead)
Definition Deflater.cs:152
int GetDeflateOutput(byte[] outputBuffer)
Definition Deflater.cs:121
unsafe void SetInput(ReadOnlyMemory< byte > inputBuffer)
Definition Deflater.cs:94
bool Flush(byte[] outputBuffer, out int bytesRead)
Definition Deflater.cs:158
void Dispose(bool disposing)
Definition Inflater.cs:174
void SetInput(byte[] inputBuffer, int startIndex, int count)
Definition Inflater.cs:154
unsafe bool Inflate(out byte b)
Definition Inflater.cs:41
Task FlushAsync()
Definition Stream.cs:669
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
static void ValidateCopyToArguments(Stream destination, int bufferSize)
Definition Stream.cs:1060
Task WriteAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:914
int Read(byte[] buffer, int offset, int count)
void CopyTo(Stream destination)
Definition Stream.cs:540
void Dispose()
Definition Stream.cs:639
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
void Write(byte[] buffer, int offset, int count)
Task CopyToAsync(Stream destination)
Definition Stream.cs:571
virtual ValueTask DisposeAsync()
Definition Stream.cs:654
static string ObjectDisposed_StreamClosed
Definition SR.cs:20
static string ArgumentOutOfRange_Enum
Definition SR.cs:18
static string NotSupported_UnwritableStream
Definition SR.cs:30
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 CannotReadFromDeflateStream
Definition SR.cs:16
static string NotSupported
Definition SR.cs:28
Definition SR.cs:7
static int Exchange(ref int location1, int value)
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static Task FromException(Exception exception)
Definition Task.cs:3341
static Task FromCanceled(CancellationToken cancellationToken)
Definition Task.cs:3363
static Task CompletedTask
Definition Task.cs:1120
static void Write(ref bool location, bool value)
Definition Volatile.cs:74
static ValueTask FromCanceled(CancellationToken cancellationToken)
Definition ValueTask.cs:180
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312