NonDisposingStream.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.IO;
  3. namespace SharpCompress.IO
  4. {
  5. public class NonDisposingStream : Stream
  6. {
  7. public NonDisposingStream(Stream stream, bool throwOnDispose = false)
  8. {
  9. Stream = stream;
  10. ThrowOnDispose = throwOnDispose;
  11. }
  12. public bool ThrowOnDispose { get; set; }
  13. protected override void Dispose(bool disposing)
  14. {
  15. GC.SuppressFinalize(this);
  16. if (ThrowOnDispose)
  17. {
  18. throw new InvalidOperationException();
  19. }
  20. }
  21. protected 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. return Stream.Read(buffer, offset, count);
  34. }
  35. public override int ReadByte()
  36. {
  37. return Stream.ReadByte();
  38. }
  39. public override long Seek(long offset, SeekOrigin origin)
  40. {
  41. return Stream.Seek(offset, origin);
  42. }
  43. public override void SetLength(long value)
  44. {
  45. Stream.SetLength(value);
  46. }
  47. public override void Write(byte[] buffer, int offset, int count)
  48. {
  49. Stream.Write(buffer, offset, count);
  50. }
  51. public override void WriteByte(byte value)
  52. {
  53. Stream.WriteByte(value);
  54. }
  55. }
  56. }