Stream.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // System.IO/Stream.cs
  3. //
  4. // Authors:
  5. // Dietmar Maurer ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (C) 2001, 2002 Ximian, Inc. http://www.ximian.com
  10. // (c) 2004 Novell, Inc. (http://www.novell.com)
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Threading;
  35. using System.Runtime.Remoting.Messaging;
  36. using System.Runtime.InteropServices;
  37. namespace System.IO
  38. {
  39. [Serializable]
  40. public abstract class Stream : MarshalByRefObject, IDisposable
  41. {
  42. public static readonly Stream Null = new NullStream ();
  43. protected Stream ()
  44. {
  45. }
  46. public abstract bool CanRead
  47. {
  48. get;
  49. }
  50. public abstract bool CanSeek
  51. {
  52. get;
  53. }
  54. public abstract bool CanWrite
  55. {
  56. get;
  57. }
  58. public abstract long Length
  59. {
  60. get;
  61. }
  62. public abstract long Position
  63. {
  64. get;
  65. set;
  66. }
  67. public virtual void Close ()
  68. {
  69. Flush ();
  70. }
  71. void IDisposable.Dispose ()
  72. {
  73. Close ();
  74. }
  75. protected virtual WaitHandle CreateWaitHandle()
  76. {
  77. return new ManualResetEvent (false);
  78. }
  79. public abstract void Flush ();
  80. public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
  81. public virtual int ReadByte ()
  82. {
  83. byte[] buffer = new byte [1];
  84. if (Read (buffer, 0, 1) == 1)
  85. return buffer [0];
  86. return -1;
  87. }
  88. public abstract long Seek (long offset, SeekOrigin origin);
  89. public abstract void SetLength (long value);
  90. public abstract void Write (byte[] buffer, int offset, int count);
  91. public virtual void WriteByte (byte value)
  92. {
  93. byte[] buffer = new byte [1];
  94. buffer [0] = value;
  95. Write (buffer, 0, 1);
  96. }
  97. public virtual IAsyncResult
  98. BeginRead (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  99. {
  100. if (!CanRead)
  101. throw new NotSupportedException ("This stream does not support reading");
  102. // Creating a class derived from Stream that doesn't override BeginRead
  103. // shows that it actually calls Read and does everything synchronously.
  104. // Just put this in the Read override:
  105. // Console.WriteLine ("Read");
  106. // Console.WriteLine (Environment.StackTrace);
  107. // Thread.Sleep (10000);
  108. // return 10;
  109. StreamAsyncResult result = new StreamAsyncResult (state);
  110. try {
  111. int nbytes = Read (buffer, offset, count);
  112. result.SetComplete (null, nbytes);
  113. } catch (Exception e) {
  114. result.SetComplete (e, 0);
  115. }
  116. if (cback != null)
  117. cback (result);
  118. return result;
  119. }
  120. delegate void WriteDelegate (byte [] buffer, int offset, int count);
  121. public virtual IAsyncResult
  122. BeginWrite (byte [] buffer, int offset, int count, AsyncCallback cback, object state)
  123. {
  124. if (!CanWrite)
  125. throw new NotSupportedException ("This stream does not support writing");
  126. // Creating a class derived from Stream that doesn't override BeginWrite
  127. // shows that it actually calls Write and does everything synchronously except
  128. // when invoking the callback, which is done from the ThreadPool.
  129. // Just put this in the Write override:
  130. // Console.WriteLine ("Write");
  131. // Console.WriteLine (Environment.StackTrace);
  132. // Thread.Sleep (10000);
  133. StreamAsyncResult result = new StreamAsyncResult (state);
  134. try {
  135. Write (buffer, offset, count);
  136. result.SetComplete (null);
  137. } catch (Exception e) {
  138. result.SetComplete (e);
  139. }
  140. if (cback != null)
  141. cback.BeginInvoke (result, null, null);
  142. return result;
  143. }
  144. public virtual int EndRead (IAsyncResult async_result)
  145. {
  146. if (async_result == null)
  147. throw new ArgumentNullException ("async_result");
  148. StreamAsyncResult result = async_result as StreamAsyncResult;
  149. if (result == null || result.NBytes == -1)
  150. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  151. if (result.Done)
  152. throw new InvalidOperationException ("EndRead already called.");
  153. result.Done = true;
  154. if (result.Exception != null)
  155. throw result.Exception;
  156. return result.NBytes;
  157. }
  158. public virtual void EndWrite (IAsyncResult async_result)
  159. {
  160. if (async_result == null)
  161. throw new ArgumentNullException ("async_result");
  162. StreamAsyncResult result = async_result as StreamAsyncResult;
  163. if (result == null || result.NBytes != -1)
  164. throw new ArgumentException ("Invalid IAsyncResult", "async_result");
  165. if (result.Done)
  166. throw new InvalidOperationException ("EndWrite already called.");
  167. result.Done = true;
  168. if (result.Exception != null)
  169. throw result.Exception;
  170. }
  171. }
  172. class NullStream : Stream
  173. {
  174. public override bool CanRead
  175. {
  176. get {
  177. return true;
  178. }
  179. }
  180. public override bool CanSeek
  181. {
  182. get {
  183. return true;
  184. }
  185. }
  186. public override bool CanWrite
  187. {
  188. get {
  189. return true;
  190. }
  191. }
  192. public override long Length
  193. {
  194. get {
  195. return 0;
  196. }
  197. }
  198. public override long Position
  199. {
  200. get {
  201. return 0;
  202. }
  203. set {
  204. }
  205. }
  206. public override void Flush ()
  207. {
  208. }
  209. public override int Read (byte[] buffer,
  210. int offset,
  211. int count)
  212. {
  213. return 0;
  214. }
  215. public override int ReadByte ()
  216. {
  217. return -1;
  218. }
  219. public override long Seek (long offset,
  220. SeekOrigin origin)
  221. {
  222. return 0;
  223. }
  224. public override void SetLength (long value)
  225. {
  226. }
  227. public override void Write (byte[] buffer,
  228. int offset,
  229. int count)
  230. {
  231. }
  232. public override void WriteByte (byte value)
  233. {
  234. }
  235. }
  236. }