WrappedSystemStream.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.IO;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. internal class WrappedSystemStream : Stream
  6. {
  7. private InputStream _ist;
  8. private OutputStream _ost;
  9. int _position;
  10. int _markedPosition;
  11. public WrappedSystemStream(InputStream ist)
  12. {
  13. this._ist = ist;
  14. }
  15. public WrappedSystemStream(OutputStream ost)
  16. {
  17. this._ost = ost;
  18. }
  19. public InputStream InputStream
  20. {
  21. get { return _ist; }
  22. }
  23. public OutputStream OutputStream
  24. {
  25. get { return _ost; }
  26. }
  27. public void Close() //remove `override`
  28. {
  29. if (_ist != null)
  30. {
  31. //Stream.`Close` method deleted
  32. //_ist.Close ();
  33. _ist.Dispose();
  34. }
  35. if (_ost != null)
  36. {
  37. //Stream.`Close` method deleted
  38. //_ost.Close ();
  39. _ost.Dispose();
  40. }
  41. }
  42. public override void Flush()
  43. {
  44. _ost.Flush();
  45. }
  46. public override int Read(byte[] buffer, int offset, int count)
  47. {
  48. int res = _ist.Read(buffer, offset, count);
  49. if (res != -1)
  50. {
  51. _position += res;
  52. return res;
  53. }
  54. return 0;
  55. }
  56. public override int ReadByte()
  57. {
  58. int res = _ist.Read();
  59. if (res != -1)
  60. _position++;
  61. return res;
  62. }
  63. public override long Seek(long offset, SeekOrigin origin)
  64. {
  65. if (origin == SeekOrigin.Begin)
  66. Position = offset;
  67. else if (origin == SeekOrigin.Current)
  68. Position = Position + offset;
  69. else if (origin == SeekOrigin.End)
  70. Position = Length + offset;
  71. return Position;
  72. }
  73. public override void SetLength(long value)
  74. {
  75. throw new NotSupportedException();
  76. }
  77. public override void Write(byte[] buffer, int offset, int count)
  78. {
  79. _ost.Write(buffer, offset, count);
  80. _position += count;
  81. }
  82. public override void WriteByte(byte value)
  83. {
  84. _ost.Write(value);
  85. _position++;
  86. }
  87. public override bool CanRead
  88. {
  89. get { return (_ist != null); }
  90. }
  91. public override bool CanSeek
  92. {
  93. get { return true; }
  94. }
  95. public override bool CanWrite
  96. {
  97. get { return (_ost != null); }
  98. }
  99. public override long Length
  100. {
  101. get { return _ist.Length; }
  102. }
  103. internal void OnMark(int nb)
  104. {
  105. _markedPosition = _position;
  106. _ist.Mark(nb);
  107. }
  108. public override long Position
  109. {
  110. get
  111. {
  112. if (_ist != null && _ist.CanSeek())
  113. return _ist.Position;
  114. return _position;
  115. }
  116. set
  117. {
  118. if (value == _position)
  119. return;
  120. if (value == _markedPosition
  121. && _ist.MarkSupported())
  122. {
  123. _ist.Reset();
  124. }
  125. else if (_ist != null && _ist.CanSeek())
  126. {
  127. _ist.Position = value;
  128. }
  129. else
  130. {
  131. throw new NotSupportedException();
  132. }
  133. }
  134. }
  135. }
  136. }