AbstractWriter.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Common;
  4. namespace SharpCompress.Writers
  5. {
  6. public abstract class AbstractWriter : IWriter
  7. {
  8. private bool _isDisposed;
  9. protected AbstractWriter(ArchiveType type, WriterOptions writerOptions)
  10. {
  11. WriterType = type;
  12. WriterOptions = writerOptions;
  13. }
  14. protected void InitalizeStream(Stream stream)
  15. {
  16. OutputStream = stream;
  17. }
  18. protected Stream OutputStream { get; private set; }
  19. public ArchiveType WriterType { get; }
  20. protected WriterOptions WriterOptions { get; }
  21. public abstract void Write(string filename, Stream source, DateTime? modificationTime);
  22. protected virtual void Dispose(bool isDisposing)
  23. {
  24. if (isDisposing)
  25. {
  26. OutputStream.Dispose();
  27. }
  28. }
  29. public void Dispose()
  30. {
  31. if (!_isDisposed)
  32. {
  33. GC.SuppressFinalize(this);
  34. Dispose(true);
  35. _isDisposed = true;
  36. }
  37. }
  38. ~AbstractWriter()
  39. {
  40. if (!_isDisposed)
  41. {
  42. Dispose(false);
  43. _isDisposed = true;
  44. }
  45. }
  46. }
  47. }