Stream.cs 4.0 KB

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