CrcBuilderStream.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.IO;
  3. namespace SharpCompress.Compressors.LZMA.Utilites
  4. {
  5. internal class CrcBuilderStream : Stream
  6. {
  7. private readonly Stream _mTarget;
  8. private uint _mCrc;
  9. private bool _mFinished;
  10. private bool _isDisposed;
  11. public CrcBuilderStream(Stream target)
  12. {
  13. _mTarget = target;
  14. _mCrc = Crc.INIT_CRC;
  15. }
  16. protected override void Dispose(bool disposing)
  17. {
  18. if (_isDisposed)
  19. {
  20. return;
  21. }
  22. _isDisposed = true;
  23. _mTarget.Dispose();
  24. base.Dispose(disposing);
  25. }
  26. public long Processed { get; private set; }
  27. public uint Finish()
  28. {
  29. if (!_mFinished)
  30. {
  31. _mFinished = true;
  32. _mCrc = Crc.Finish(_mCrc);
  33. }
  34. return _mCrc;
  35. }
  36. public override bool CanRead => false;
  37. public override bool CanSeek => false;
  38. public override bool CanWrite => true;
  39. public override void Flush()
  40. {
  41. }
  42. public override long Length => throw new NotSupportedException();
  43. public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
  44. public override int Read(byte[] buffer, int offset, int count)
  45. {
  46. throw new InvalidOperationException();
  47. }
  48. public override long Seek(long offset, SeekOrigin origin)
  49. {
  50. throw new NotSupportedException();
  51. }
  52. public override void SetLength(long value)
  53. {
  54. throw new NotSupportedException();
  55. }
  56. public override void Write(byte[] buffer, int offset, int count)
  57. {
  58. if (_mFinished)
  59. {
  60. throw new InvalidOperationException("CRC calculation has been finished.");
  61. }
  62. Processed += count;
  63. _mCrc = Crc.Update(_mCrc, buffer, offset, count);
  64. _mTarget.Write(buffer, offset, count);
  65. }
  66. }
  67. }