Stream.cs 4.1 KB

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