ListeningStream.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.IO;
  2. using SharpCompress.Common;
  3. namespace SharpCompress.IO
  4. {
  5. internal class ListeningStream : Stream
  6. {
  7. private long currentEntryTotalReadBytes;
  8. private readonly IExtractionListener listener;
  9. public ListeningStream(IExtractionListener listener, Stream stream)
  10. {
  11. Stream = stream;
  12. this.listener = listener;
  13. }
  14. protected override void Dispose(bool disposing)
  15. {
  16. if (disposing)
  17. {
  18. Stream.Dispose();
  19. }
  20. }
  21. public Stream Stream { get; }
  22. public override bool CanRead => Stream.CanRead;
  23. public override bool CanSeek => Stream.CanSeek;
  24. public override bool CanWrite => Stream.CanWrite;
  25. public override void Flush()
  26. {
  27. Stream.Flush();
  28. }
  29. public override long Length => Stream.Length;
  30. public override long Position { get => Stream.Position; set => Stream.Position = value; }
  31. public override int Read(byte[] buffer, int offset, int count)
  32. {
  33. int read = Stream.Read(buffer, offset, count);
  34. currentEntryTotalReadBytes += read;
  35. listener.FireCompressedBytesRead(currentEntryTotalReadBytes, currentEntryTotalReadBytes);
  36. return read;
  37. }
  38. public override int ReadByte()
  39. {
  40. int value = Stream.ReadByte();
  41. if (value == -1)
  42. {
  43. return -1;
  44. }
  45. ++currentEntryTotalReadBytes;
  46. listener.FireCompressedBytesRead(currentEntryTotalReadBytes, currentEntryTotalReadBytes);
  47. return value;
  48. }
  49. public override long Seek(long offset, SeekOrigin origin)
  50. {
  51. return Stream.Seek(offset, origin);
  52. }
  53. public override void SetLength(long value)
  54. {
  55. Stream.SetLength(value);
  56. }
  57. public override void Write(byte[] buffer, int offset, int count)
  58. {
  59. Stream.Write(buffer, offset, count);
  60. }
  61. public override void WriteByte(byte value)
  62. {
  63. Stream.WriteByte(value);
  64. }
  65. }
  66. }