Stream.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. public virtual IAsyncResult
  82. BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  83. {
  84. if (!CanRead)
  85. throw new NotSupportedException ("This stream does not support reading");
  86. SyncReadResult srr = new SyncReadResult (state);
  87. try
  88. {
  89. srr.Complete (Read (buffer, offset, count));
  90. }
  91. catch (IOException e)
  92. {
  93. srr._exception = e;
  94. }
  95. if (cback != null)
  96. cback (srr);
  97. return srr;
  98. }
  99. public virtual IAsyncResult
  100. BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  101. {
  102. if (!CanWrite)
  103. throw new NotSupportedException ("This stream does not support reading");
  104. SyncWriteResult swr = new SyncWriteResult (state);
  105. try
  106. {
  107. Write (buffer, offset, count);
  108. swr.Complete ();
  109. }
  110. catch (IOException e)
  111. {
  112. swr._exception = e;
  113. }
  114. if (cback != null)
  115. cback (swr);
  116. return swr;
  117. }
  118. public virtual int EndRead (IAsyncResult async_result)
  119. {
  120. if (async_result == null)
  121. throw new ArgumentNullException ("async_result");
  122. SyncReadResult srr = async_result as SyncReadResult;
  123. if (srr == null)
  124. throw new ArgumentException ("async_result is invalid");
  125. if (srr._fEndCalled)
  126. throw new InvalidOperationException ("EndRead called twice");
  127. srr._fEndCalled = true;
  128. if (srr._exception != null)
  129. throw srr._exception;
  130. return srr._cbRead;
  131. }
  132. public virtual void EndWrite (IAsyncResult async_result)
  133. {
  134. if (async_result == null)
  135. throw new ArgumentNullException ("async_result");
  136. SyncWriteResult swr = async_result as SyncWriteResult;
  137. if (swr == null)
  138. throw new ArgumentException ("async_result is invalid");
  139. if (swr._fEndCalled)
  140. throw new InvalidOperationException ("EndRead called twice");
  141. swr._fEndCalled = true;
  142. if (swr._exception != null)
  143. throw swr._exception;
  144. }
  145. // this class implements the synchronous IASyncResult for the obove methods
  146. private class SyncResult : IAsyncResult
  147. {
  148. object _objState; // client-supplied state
  149. bool _fComplete; // if the IO operation completed successfully
  150. ManualResetEvent _hWait; // the wait event
  151. public bool _fEndCalled; // true iff the End method was called already
  152. public Exception _exception; // holds any exception throw during IO operation
  153. public SyncResult (object objState)
  154. {
  155. _objState = objState;
  156. _hWait = new ManualResetEvent (false);
  157. }
  158. public void Complete ()
  159. {
  160. _fComplete = true;
  161. _hWait.Set ();
  162. }
  163. // IAsyncResult members
  164. object IAsyncResult.AsyncState
  165. {
  166. get { return _objState; }
  167. }
  168. WaitHandle IAsyncResult.AsyncWaitHandle
  169. {
  170. get { return _hWait; }
  171. }
  172. bool IAsyncResult.CompletedSynchronously
  173. {
  174. get { return true; }
  175. }
  176. bool IAsyncResult.IsCompleted
  177. {
  178. get { return _fComplete; }
  179. }
  180. }
  181. private class SyncReadResult : SyncResult
  182. {
  183. public int _cbRead; // the number of bytes read
  184. public SyncReadResult (object objState) : base (objState) {}
  185. public void Complete (int cbRead)
  186. {
  187. _cbRead = cbRead;
  188. Complete ();
  189. }
  190. }
  191. private class SyncWriteResult : SyncResult
  192. {
  193. public SyncWriteResult (object objState) : base (objState) {}
  194. }
  195. }
  196. class NullStream : Stream
  197. {
  198. public override bool CanRead
  199. {
  200. get {
  201. return true;
  202. }
  203. }
  204. public override bool CanSeek
  205. {
  206. get {
  207. return true;
  208. }
  209. }
  210. public override bool CanWrite
  211. {
  212. get {
  213. return true;
  214. }
  215. }
  216. public override long Length
  217. {
  218. get {
  219. return 0;
  220. }
  221. }
  222. public override long Position
  223. {
  224. get {
  225. return 0;
  226. }
  227. set {
  228. }
  229. }
  230. public override void Flush ()
  231. {
  232. }
  233. public override int Read (byte[] buffer,
  234. int offset,
  235. int count)
  236. {
  237. return 0;
  238. }
  239. public override int ReadByte ()
  240. {
  241. return -1;
  242. }
  243. public override long Seek (long offset,
  244. SeekOrigin origin)
  245. {
  246. return 0;
  247. }
  248. public override void SetLength (long value)
  249. {
  250. }
  251. public override void Write (byte[] buffer,
  252. int offset,
  253. int count)
  254. {
  255. }
  256. public override void WriteByte (byte value)
  257. {
  258. }
  259. }
  260. }