Stream.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. void IDisposable.Dispose ()
  50. {
  51. Close ();
  52. }
  53. protected virtual WaitHandle CreateWaitHandle()
  54. {
  55. return new ManualResetEvent (false);
  56. }
  57. public abstract void Flush ();
  58. public abstract int Read (byte[] buffer,
  59. int offset,
  60. int count);
  61. public virtual int ReadByte ()
  62. {
  63. byte[] buffer = new byte [1];
  64. if (Read (buffer, 0, 1) == 1)
  65. return buffer [0];
  66. return -1;
  67. }
  68. public abstract long Seek (long offset,
  69. SeekOrigin origin);
  70. public abstract void SetLength (long value);
  71. public abstract void Write (byte[] buffer,
  72. int offset,
  73. int count);
  74. public virtual void WriteByte (byte value)
  75. {
  76. byte[] buffer = new byte [1];
  77. buffer [0] = value;
  78. Write (buffer, 0, 1);
  79. }
  80. delegate int ReadDelegate (byte [] buffer, int offset, int count);
  81. [MonoTODO]
  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. return null;
  88. }
  89. [MonoTODO]
  90. public virtual IAsyncResult
  91. BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  92. {
  93. return null;
  94. }
  95. [MonoTODO]
  96. public virtual int EndRead (IAsyncResult async_result)
  97. {
  98. if (async_result == null)
  99. throw new ArgumentNullException ("async_result");
  100. return 0;
  101. }
  102. [MonoTODO]
  103. public virtual void EndWrite (IAsyncResult async_result)
  104. {
  105. if (async_result == null)
  106. throw new ArgumentNullException ("async_result");
  107. }
  108. }
  109. class NullStream : Stream
  110. {
  111. public override bool CanRead
  112. {
  113. get {
  114. return true;
  115. }
  116. }
  117. public override bool CanSeek
  118. {
  119. get {
  120. return true;
  121. }
  122. }
  123. public override bool CanWrite
  124. {
  125. get {
  126. return true;
  127. }
  128. }
  129. public override long Length
  130. {
  131. get {
  132. return 0;
  133. }
  134. }
  135. public override long Position
  136. {
  137. get {
  138. return 0;
  139. }
  140. set {
  141. }
  142. }
  143. public override void Flush ()
  144. {
  145. }
  146. public override int Read (byte[] buffer,
  147. int offset,
  148. int count)
  149. {
  150. return 0;
  151. }
  152. public override int ReadByte ()
  153. {
  154. return 0;
  155. }
  156. public override long Seek (long offset,
  157. SeekOrigin origin)
  158. {
  159. return 0;
  160. }
  161. public override void SetLength (long value)
  162. {
  163. }
  164. public override void Write (byte[] buffer,
  165. int offset,
  166. int count)
  167. {
  168. }
  169. public override void WriteByte (byte value)
  170. {
  171. }
  172. }
  173. }