StreamWatcher.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.IO;
  9. namespace SMBServer
  10. {
  11. /// <summary>
  12. /// A wrapper for the stream class that notify when the stream is closed
  13. /// </summary>
  14. public class StreamWatcher : Stream
  15. {
  16. private Stream m_stream;
  17. public event EventHandler Closed;
  18. public StreamWatcher(Stream stream)
  19. {
  20. m_stream = stream;
  21. }
  22. public override int Read(byte[] buffer, int offset, int count)
  23. {
  24. return m_stream.Read(buffer, offset, count);
  25. }
  26. public override void Write(byte[] buffer, int offset, int count)
  27. {
  28. m_stream.Write(buffer, offset, count);
  29. }
  30. public override void Close()
  31. {
  32. m_stream.Close();
  33. EventHandler handler = Closed;
  34. if (handler != null)
  35. {
  36. handler(this, EventArgs.Empty);
  37. }
  38. }
  39. public override void Flush()
  40. {
  41. m_stream.Flush();
  42. }
  43. public override long Seek(long offset, SeekOrigin origin)
  44. {
  45. return m_stream.Seek(offset, origin);
  46. }
  47. public override void SetLength(long value)
  48. {
  49. m_stream.SetLength(value);
  50. }
  51. public override bool CanSeek
  52. {
  53. get
  54. {
  55. return m_stream.CanSeek;
  56. }
  57. }
  58. public override bool CanRead
  59. {
  60. get
  61. {
  62. return m_stream.CanRead;
  63. }
  64. }
  65. public override bool CanWrite
  66. {
  67. get
  68. {
  69. return m_stream.CanWrite;
  70. }
  71. }
  72. public override long Length
  73. {
  74. get
  75. {
  76. return m_stream.Length;
  77. }
  78. }
  79. public override long Position
  80. {
  81. get
  82. {
  83. return m_stream.Position;
  84. }
  85. set
  86. {
  87. m_stream.Position = value;
  88. }
  89. }
  90. public Stream Stream
  91. {
  92. get
  93. {
  94. return m_stream;
  95. }
  96. }
  97. }
  98. }