Stream.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // System.IO/Stream.cs
  3. //
  4. // Authors:
  5. // Dietmar Maurer ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System.Threading;
  11. namespace System.IO
  12. {
  13. [Serializable]
  14. public abstract class Stream : MarshalByRefObject, IDisposable
  15. {
  16. public static readonly Stream Null;
  17. static Stream ()
  18. {
  19. Null = new NullStream ();
  20. }
  21. protected Stream ()
  22. {
  23. }
  24. public abstract bool CanRead
  25. {
  26. get;
  27. }
  28. public abstract bool CanSeek
  29. {
  30. get;
  31. }
  32. public abstract bool CanWrite
  33. {
  34. get;
  35. }
  36. public abstract long Length
  37. {
  38. get;
  39. }
  40. public abstract long Position
  41. {
  42. get;
  43. set;
  44. }
  45. public virtual void Close ()
  46. {
  47. Flush ();
  48. }
  49. public virtual void Dispose ()
  50. {
  51. }
  52. protected virtual WaitHandle CreateWaitHandle()
  53. {
  54. return new ManualResetEvent (false);
  55. }
  56. protected virtual void Dispose (bool disposing)
  57. {
  58. }
  59. ~Stream ()
  60. {
  61. }
  62. public abstract void Flush ();
  63. public abstract int Read (byte[] buffer,
  64. int offset,
  65. int count);
  66. public virtual int ReadByte ()
  67. {
  68. byte[] buffer = new byte [1];
  69. if (Read (buffer, 0, 1) == 1)
  70. return buffer [0];
  71. return -1;
  72. }
  73. public abstract long Seek (long offset,
  74. SeekOrigin origin);
  75. public abstract void SetLength (long value);
  76. public abstract void Write (byte[] buffer,
  77. int offset,
  78. int count);
  79. public virtual void WriteByte (byte value)
  80. {
  81. byte[] buffer = new byte [1];
  82. buffer [0] = value;
  83. Write (buffer, 0, 1);
  84. }
  85. delegate int ReadDelegate (byte [] buffer, int offset, int count);
  86. [MonoTODO]
  87. public virtual IAsyncResult
  88. BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  89. {
  90. if (!CanRead)
  91. throw new NotSupportedException ("This stream does not support reading");
  92. return null;
  93. }
  94. [MonoTODO]
  95. public virtual IAsyncResult
  96. BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  97. {
  98. return null;
  99. }
  100. [MonoTODO]
  101. public virtual int EndRead (IAsyncResult async_result)
  102. {
  103. if (async_result == null)
  104. throw new ArgumentNullException ("async_result");
  105. return 0;
  106. }
  107. [MonoTODO]
  108. public virtual int EndWrite (IAsyncResult async_result)
  109. {
  110. if (async_result == null)
  111. throw new ArgumentNullException ("async_result");
  112. return 0;
  113. }
  114. }
  115. class NullStream : Stream
  116. {
  117. public override bool CanRead
  118. {
  119. get {
  120. return true;
  121. }
  122. }
  123. public override bool CanSeek
  124. {
  125. get {
  126. return true;
  127. }
  128. }
  129. public override bool CanWrite
  130. {
  131. get {
  132. return true;
  133. }
  134. }
  135. public override long Length
  136. {
  137. get {
  138. return 0;
  139. }
  140. }
  141. public override long Position
  142. {
  143. get {
  144. return 0;
  145. }
  146. set {
  147. }
  148. }
  149. public override void Flush ()
  150. {
  151. }
  152. public override int Read (byte[] buffer,
  153. int offset,
  154. int count)
  155. {
  156. return 0;
  157. }
  158. public override int ReadByte ()
  159. {
  160. return 0;
  161. }
  162. public override long Seek (long offset,
  163. SeekOrigin origin)
  164. {
  165. return 0;
  166. }
  167. public override void SetLength (long value)
  168. {
  169. }
  170. public override void Write (byte[] buffer,
  171. int offset,
  172. int count)
  173. {
  174. }
  175. public override void WriteByte (byte value)
  176. {
  177. }
  178. }
  179. }