Stream.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. [ComVisible (true)]
  41. #if NET_2_1
  42. public abstract class Stream : IDisposable
  43. #else
  44. public abstract class Stream : MarshalByRefObject, IDisposable
  45. #endif
  46. {
  47. public static readonly Stream Null = new NullStream ();
  48. protected Stream ()
  49. {
  50. }
  51. public abstract bool CanRead
  52. {
  53. get;
  54. }
  55. public abstract bool CanSeek
  56. {
  57. get;
  58. }
  59. public abstract bool CanWrite
  60. {
  61. get;
  62. }
  63. [ComVisible (false)]
  64. public virtual bool CanTimeout {
  65. get {
  66. return false;
  67. }
  68. }
  69. public abstract long Length
  70. {
  71. get;
  72. }
  73. public abstract long Position
  74. {
  75. get;
  76. set;
  77. }
  78. // 2.0 version of Dispose.
  79. public void Dispose ()
  80. {
  81. Close ();
  82. }
  83. // 2.0 version of Dispose.
  84. protected virtual void Dispose (bool disposing)
  85. {
  86. // nothing.
  87. }
  88. //
  89. // 2.0 version of Close (): calls Dispose (true)
  90. //
  91. public virtual void Close ()
  92. {
  93. Dispose (true);
  94. }
  95. [ComVisible (false)]
  96. public virtual int ReadTimeout {
  97. get {
  98. throw new InvalidOperationException ("Timeouts are not supported on this stream.");
  99. }
  100. set {
  101. throw new InvalidOperationException ("Timeouts are not supported on this stream.");
  102. }
  103. }
  104. [ComVisible (false)]
  105. public virtual int WriteTimeout {
  106. get {
  107. throw new InvalidOperationException ("Timeouts are not supported on this stream.");
  108. }
  109. set {
  110. throw new InvalidOperationException ("Timeouts are not supported on this stream.");
  111. }
  112. }
  113. public static Stream Synchronized (Stream stream)
  114. {
  115. throw new NotImplementedException ();
  116. }
  117. [Obsolete ("CreateWaitHandle is due for removal. Use \"new ManualResetEvent(false)\" instead.")]
  118. protected virtual WaitHandle CreateWaitHandle()
  119. {
  120. return new ManualResetEvent (false);
  121. }
  122. public abstract void Flush ();
  123. public abstract int Read ([In,Out] byte[] buffer, int offset, int count);
  124. public virtual int ReadByte ()
  125. {
  126. byte[] buffer = new byte [1];
  127. if (Read (buffer, 0, 1) == 1)
  128. return buffer [0];
  129. return -1;
  130. }
  131. public abstract long Seek (long offset, SeekOrigin origin);
  132. public abstract void SetLength (long value);
  133. public abstract void Write (byte[] buffer, int offset, int count);
  134. public virtual void WriteByte (byte value)
  135. {
  136. byte[] buffer = new byte [1];
  137. buffer [0] = value;
  138. Write (buffer, 0, 1);
  139. }
  140. public virtual IAsyncResult
  141. BeginRead (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
  142. {
  143. if (!CanRead)
  144. throw new NotSupportedException ("This stream does not support reading");
  145. // Creating a class derived from Stream that doesn't override BeginRead
  146. // shows that it actually calls Read and does everything synchronously.
  147. // Just put this in the Read override:
  148. // Console.WriteLine ("Read");
  149. // Console.WriteLine (Environment.StackTrace);
  150. // Thread.Sleep (10000);
  151. // return 10;
  152. StreamAsyncResult result = new StreamAsyncResult (state);
  153. try {
  154. int nbytes = Read (buffer, offset, count);
  155. result.SetComplete (null, nbytes);
  156. } catch (Exception e) {
  157. result.SetComplete (e, 0);
  158. }
  159. if (callback != null)
  160. callback (result);
  161. return result;
  162. }
  163. // delegate void WriteDelegate (byte [] buffer, int offset, int count);
  164. public virtual IAsyncResult
  165. BeginWrite (byte [] buffer, int offset, int count, AsyncCallback callback, object state)
  166. {
  167. if (!CanWrite)
  168. throw new NotSupportedException ("This stream does not support writing");
  169. // Creating a class derived from Stream that doesn't override BeginWrite
  170. // shows that it actually calls Write and does everything synchronously except
  171. // when invoking the callback, which is done from the ThreadPool.
  172. // Just put this in the Write override:
  173. // Console.WriteLine ("Write");
  174. // Console.WriteLine (Environment.StackTrace);
  175. // Thread.Sleep (10000);
  176. StreamAsyncResult result = new StreamAsyncResult (state);
  177. try {
  178. Write (buffer, offset, count);
  179. result.SetComplete (null);
  180. } catch (Exception e) {
  181. result.SetComplete (e);
  182. }
  183. if (callback != null)
  184. callback.BeginInvoke (result, null, null);
  185. return result;
  186. }
  187. public virtual int EndRead (IAsyncResult asyncResult)
  188. {
  189. if (asyncResult == null)
  190. throw new ArgumentNullException ("asyncResult");
  191. StreamAsyncResult result = asyncResult as StreamAsyncResult;
  192. if (result == null || result.NBytes == -1)
  193. throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
  194. if (result.Done)
  195. throw new InvalidOperationException ("EndRead already called.");
  196. result.Done = true;
  197. if (result.Exception != null)
  198. throw result.Exception;
  199. return result.NBytes;
  200. }
  201. public virtual void EndWrite (IAsyncResult asyncResult)
  202. {
  203. if (asyncResult == null)
  204. throw new ArgumentNullException ("asyncResult");
  205. StreamAsyncResult result = asyncResult as StreamAsyncResult;
  206. if (result == null || result.NBytes != -1)
  207. throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
  208. if (result.Done)
  209. throw new InvalidOperationException ("EndWrite already called.");
  210. result.Done = true;
  211. if (result.Exception != null)
  212. throw result.Exception;
  213. }
  214. }
  215. class NullStream : Stream
  216. {
  217. public override bool CanRead
  218. {
  219. get {
  220. return true;
  221. }
  222. }
  223. public override bool CanSeek
  224. {
  225. get {
  226. return true;
  227. }
  228. }
  229. public override bool CanWrite
  230. {
  231. get {
  232. return true;
  233. }
  234. }
  235. public override long Length
  236. {
  237. get {
  238. return 0;
  239. }
  240. }
  241. public override long Position
  242. {
  243. get {
  244. return 0;
  245. }
  246. set {
  247. }
  248. }
  249. public override void Flush ()
  250. {
  251. }
  252. public override int Read (byte[] buffer, int offset, int count)
  253. {
  254. return 0;
  255. }
  256. public override int ReadByte ()
  257. {
  258. return -1;
  259. }
  260. public override long Seek (long offset, SeekOrigin origin)
  261. {
  262. return 0;
  263. }
  264. public override void SetLength (long value)
  265. {
  266. }
  267. public override void Write (byte[] buffer, int offset, int count)
  268. {
  269. }
  270. public override void WriteByte (byte value)
  271. {
  272. }
  273. }
  274. }