InputStream.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.IO;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. public class InputStream : IDisposable
  6. {
  7. private long _mark;
  8. protected Stream Wrapped;
  9. protected Stream BaseStream;
  10. public static implicit operator InputStream(Stream s)
  11. {
  12. return Wrap(s);
  13. }
  14. public static implicit operator Stream(InputStream s)
  15. {
  16. return s.GetWrappedStream();
  17. }
  18. public virtual int Available()
  19. {
  20. if (Wrapped is WrappedSystemStream)
  21. return ((WrappedSystemStream)Wrapped).InputStream.Available();
  22. return 0;
  23. }
  24. public virtual void Close()
  25. {
  26. if (Wrapped != null)
  27. {
  28. //Stream.`Close` method deleted
  29. //Wrapped.Close();
  30. Wrapped.Dispose();
  31. }
  32. }
  33. public void Dispose()
  34. {
  35. Close();
  36. }
  37. internal Stream GetWrappedStream()
  38. {
  39. // Always create a wrapper stream (not directly Wrapped) since the subclass
  40. // may be overriding methods that need to be called when used through the Stream class
  41. return new WrappedSystemStream(this);
  42. }
  43. public virtual void Mark(int readlimit)
  44. {
  45. if (Wrapped is WrappedSystemStream)
  46. ((WrappedSystemStream)Wrapped).InputStream.Mark(readlimit);
  47. else
  48. {
  49. if (BaseStream is WrappedSystemStream)
  50. ((WrappedSystemStream)BaseStream).OnMark(readlimit);
  51. if (Wrapped != null)
  52. _mark = Wrapped.Position;
  53. }
  54. }
  55. public virtual bool MarkSupported()
  56. {
  57. if (Wrapped is WrappedSystemStream)
  58. return ((WrappedSystemStream)Wrapped).InputStream.MarkSupported();
  59. return ((Wrapped != null) && Wrapped.CanSeek);
  60. }
  61. public virtual int Read()
  62. {
  63. if (Wrapped == null)
  64. {
  65. throw new NotImplementedException();
  66. }
  67. return Wrapped.ReadByte();
  68. }
  69. public virtual int Read(byte[] buf)
  70. {
  71. return Read(buf, 0, buf.Length);
  72. }
  73. public virtual int Read(byte[] b, int off, int len)
  74. {
  75. if (Wrapped is WrappedSystemStream)
  76. return ((WrappedSystemStream)Wrapped).InputStream.Read(b, off, len);
  77. if (Wrapped != null)
  78. {
  79. int num = Wrapped.Read(b, off, len);
  80. return ((num <= 0) ? -1 : num);
  81. }
  82. int totalRead = 0;
  83. while (totalRead < len)
  84. {
  85. int nr = Read();
  86. if (nr == -1)
  87. return -1;
  88. b[off + totalRead] = (byte)nr;
  89. totalRead++;
  90. }
  91. return totalRead;
  92. }
  93. public virtual void Reset()
  94. {
  95. if (Wrapped is WrappedSystemStream)
  96. ((WrappedSystemStream)Wrapped).InputStream.Reset();
  97. else
  98. {
  99. if (Wrapped == null)
  100. throw new IOException();
  101. Wrapped.Position = _mark;
  102. }
  103. }
  104. public virtual long Skip(long cnt)
  105. {
  106. if (Wrapped is WrappedSystemStream)
  107. return ((WrappedSystemStream)Wrapped).InputStream.Skip(cnt);
  108. long n = cnt;
  109. while (n > 0)
  110. {
  111. if (Read() == -1)
  112. return cnt - n;
  113. n--;
  114. }
  115. return cnt - n;
  116. }
  117. internal virtual bool CanSeek()
  118. {
  119. if (Wrapped != null)
  120. return Wrapped.CanSeek;
  121. return false;
  122. }
  123. internal virtual long Position
  124. {
  125. get
  126. {
  127. if (Wrapped != null)
  128. return Wrapped.Position;
  129. throw new NotSupportedException();
  130. }
  131. set
  132. {
  133. if (Wrapped != null)
  134. Wrapped.Position = value;
  135. else
  136. throw new NotSupportedException();
  137. }
  138. }
  139. public virtual long Length
  140. {
  141. get
  142. {
  143. if (Wrapped != null)
  144. {
  145. return Wrapped.Length;
  146. }
  147. throw new NotSupportedException();
  148. }
  149. }
  150. static internal InputStream Wrap(Stream s)
  151. {
  152. InputStream stream = new InputStream();
  153. stream.Wrapped = s;
  154. return stream;
  155. }
  156. }
  157. }