Stream.cs 3.1 KB

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