WrappedSystemStream.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. get { return _ist; }
  21. }
  22. public OutputStream OutputStream {
  23. get { return _ost; }
  24. }
  25. public void Close() //remove `override`
  26. {
  27. if (_ist != null) {
  28. //Stream.`Close` method deleted
  29. //_ist.Close ();
  30. _ist.Dispose();
  31. }
  32. if (_ost != null) {
  33. //Stream.`Close` method deleted
  34. //_ost.Close ();
  35. _ost.Dispose();
  36. }
  37. }
  38. public override void Flush ()
  39. {
  40. _ost.Flush ();
  41. }
  42. public override int Read (byte[] buffer, int offset, int count)
  43. {
  44. int res = _ist.Read (buffer, offset, count);
  45. if (res != -1) {
  46. _position += res;
  47. return res;
  48. }
  49. return 0;
  50. }
  51. public override int ReadByte ()
  52. {
  53. int res = _ist.Read ();
  54. if (res != -1)
  55. _position++;
  56. return res;
  57. }
  58. public override long Seek (long offset, SeekOrigin origin)
  59. {
  60. if (origin == SeekOrigin.Begin)
  61. Position = offset;
  62. else if (origin == SeekOrigin.Current)
  63. Position = Position + offset;
  64. else if (origin == SeekOrigin.End)
  65. Position = Length + offset;
  66. return Position;
  67. }
  68. public override void SetLength (long value)
  69. {
  70. throw new NotSupportedException ();
  71. }
  72. public override void Write (byte[] buffer, int offset, int count)
  73. {
  74. _ost.Write (buffer, offset, count);
  75. _position += count;
  76. }
  77. public override void WriteByte (byte value)
  78. {
  79. _ost.Write (value);
  80. _position++;
  81. }
  82. public override bool CanRead {
  83. get { return (_ist != null); }
  84. }
  85. public override bool CanSeek {
  86. get { return true; }
  87. }
  88. public override bool CanWrite {
  89. get { return (_ost != null); }
  90. }
  91. public override long Length {
  92. get { return _ist.Length; }
  93. }
  94. internal void OnMark (int nb)
  95. {
  96. _markedPosition = _position;
  97. _ist.Mark (nb);
  98. }
  99. public override long Position {
  100. get
  101. {
  102. if (_ist != null && _ist.CanSeek ())
  103. return _ist.Position;
  104. return _position;
  105. }
  106. set
  107. {
  108. if (value == _position)
  109. return;
  110. if (value == _markedPosition)
  111. _ist.Reset ();
  112. else if (_ist != null && _ist.CanSeek ()) {
  113. _ist.Position = value;
  114. }
  115. else
  116. throw new NotSupportedException ();
  117. }
  118. }
  119. }
  120. }