Guest User

Untitled

a guest
Mar 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. /// <summary>
  2. /// Stream that reports read progress
  3. /// </summary>
  4. public class ProgressStream : Stream
  5. {
  6. /// <summary>
  7. /// The progress will be reported in percent
  8. /// when reading from the stream;
  9. /// </summary>
  10. private IProgress<double> _readProgress;
  11.  
  12. /// <summary>
  13. /// The actual stream that contains the desired data.
  14. /// </summary>
  15. private Stream _innerStream;
  16. private long _totalBytesRead = 0;
  17. private long _totalBytesWritten = 0;
  18.  
  19. /// <summary>
  20. /// Creates a new instance of the <see cref="ProgressStream"/>
  21. /// </summary>
  22. /// <param name="innerStream">The actual stream that contains our data</param>
  23. /// <param name="readProgress">An implementation of <see cref="IProgress{T}"/>
  24. /// that will be used to get the progress of the read</param>
  25. /// <exception cref="ArgumentNullException">Thrown when innerStream or readProgress is null</exception>
  26. public ProgressStream(Stream innerStream, IProgress<double> readProgress)
  27. {
  28. _innerStream = innerStream ?? throw new ArgumentNullException(nameof(innerStream));
  29. _readProgress = readProgress ?? throw new ArgumentNullException(nameof(readProgress));
  30. }
  31.  
  32. #region Overrides
  33. public override bool CanRead => _innerStream.CanRead;
  34.  
  35. public override bool CanSeek => _innerStream.CanSeek;
  36.  
  37. public override bool CanWrite => _innerStream.CanWrite;
  38.  
  39. public override long Length => _innerStream.Length;
  40.  
  41. public override long Position { get => _innerStream.Position; set => _innerStream.Position = value; }
  42.  
  43. public override void Flush()
  44. => _innerStream.Flush();
  45.  
  46. public override int Read(byte[] buffer, int offset, int count)
  47. {
  48. var read = _innerStream.Read(buffer, offset, count);
  49. _totalBytesRead += read;
  50. _readProgress.Report((_totalBytesRead / (double)_innerStream.Length) * 100F);
  51. return read;
  52. }
  53.  
  54. public override long Seek(long offset, SeekOrigin origin)
  55. => _innerStream.Seek(offset, origin);
  56.  
  57. public override void SetLength(long value)
  58. => _innerStream.SetLength(value);
  59.  
  60. public override void Write(byte[] buffer, int offset, int count)
  61. => _innerStream.Write(buffer, offset, count);
  62.  
  63. public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  64. => _innerStream.WriteAsync(buffer, offset, count, cancellationToken);
  65.  
  66. public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  67. {
  68. var read = await _innerStream.ReadAsync(buffer, offset, count, cancellationToken);
  69. _totalBytesRead += read;
  70. _readProgress.Report((_totalBytesRead / (double)_innerStream.Length) * 100F);
  71. return read;
  72. }
  73.  
  74. protected override void Dispose(bool disposing)
  75. {
  76. if (disposing)
  77. {
  78. _innerStream.Dispose();
  79. }
  80. base.Dispose(disposing);
  81. }
  82. #endregion
  83. }
Add Comment
Please, Sign In to add comment