InputStream.cs 3.6 KB

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