Stream.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // System.IO/Stream.cs
  3. //
  4. // Author:
  5. // Dietmar Maurer ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Threading;
  10. namespace System.IO
  11. {
  12. public abstract class Stream : MarshalByRefObject, IDisposable
  13. {
  14. // public static readonly Stream Null;
  15. static Stream ()
  16. {
  17. //Null = new NullStream ();
  18. }
  19. protected Stream ()
  20. {
  21. }
  22. public abstract bool CanRead
  23. {
  24. get;
  25. }
  26. public abstract bool CanSeek
  27. {
  28. get;
  29. }
  30. public abstract bool CanWrite
  31. {
  32. get;
  33. }
  34. public abstract long Length
  35. {
  36. get;
  37. }
  38. public abstract long Position
  39. {
  40. get;
  41. set;
  42. }
  43. public virtual void Close ()
  44. {
  45. Flush ();
  46. }
  47. public virtual void Dispose ()
  48. {
  49. }
  50. protected virtual WaitHandle CreateWaitHandle()
  51. {
  52. return(null);
  53. }
  54. protected virtual void Dispose (bool disposing)
  55. {
  56. }
  57. ~Stream ()
  58. {
  59. }
  60. public abstract void Flush ();
  61. public abstract int Read (byte[] buffer,
  62. int offset,
  63. int count);
  64. public virtual int ReadByte ()
  65. {
  66. byte[] buffer = new byte [1];
  67. if (Read (buffer, 0, 1) == 1)
  68. return buffer [0];
  69. return -1;
  70. }
  71. public abstract long Seek (long offset,
  72. SeekOrigin origin);
  73. public abstract void SetLength (long value);
  74. public abstract void Write (byte[] buffer,
  75. int offset,
  76. int count);
  77. public virtual void WriteByte (byte value)
  78. {
  79. byte[] buffer = new byte [1];
  80. buffer [0] = value;
  81. Write (buffer, 0, 1);
  82. }
  83. }
  84. class NullStream : Stream
  85. {
  86. private long position = 0;
  87. private long length = System.Int64.MaxValue;
  88. public override bool CanRead
  89. {
  90. get {
  91. return true;
  92. }
  93. }
  94. public override bool CanSeek
  95. {
  96. get {
  97. return true;
  98. }
  99. }
  100. public override bool CanWrite
  101. {
  102. get {
  103. return true;
  104. }
  105. }
  106. public override long Length
  107. {
  108. get {
  109. return length;
  110. }
  111. }
  112. public override long Position
  113. {
  114. get {
  115. return position;
  116. }
  117. set {
  118. position = value;
  119. }
  120. }
  121. public override void Flush ()
  122. {
  123. }
  124. public override int Read (byte[] buffer,
  125. int offset,
  126. int count)
  127. {
  128. int max = offset + count;
  129. for (int i = offset; i < max; i++)
  130. buffer [i] = 0;
  131. return count;
  132. }
  133. public override int ReadByte ()
  134. {
  135. return 0;
  136. }
  137. public override long Seek (long offset,
  138. SeekOrigin origin)
  139. {
  140. switch (origin) {
  141. case SeekOrigin.Begin:
  142. position = offset;
  143. break;
  144. case SeekOrigin.Current:
  145. position = position + offset;
  146. break;
  147. case SeekOrigin.End:
  148. position = Length - offset;
  149. break;
  150. }
  151. return position;
  152. }
  153. public override void SetLength (long value)
  154. {
  155. length = value;
  156. }
  157. public override void Write (byte[] buffer,
  158. int offset,
  159. int count)
  160. {
  161. }
  162. public override void WriteByte (byte value)
  163. {
  164. }
  165. }
  166. }