Stream.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. return new SynchronizedStream (stream);
  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. #if NET_4_0
  215. public void CopyTo (Stream destination)
  216. {
  217. CopyTo (destination, 16*1024);
  218. }
  219. public void CopyTo (Stream destination, int bufferSize)
  220. {
  221. if (destination == null)
  222. throw new ArgumentNullException ("destination");
  223. if (!CanRead)
  224. throw new NotSupportedException ("This stream does not support reading");
  225. if (!destination.CanWrite)
  226. throw new NotSupportedException ("This destination stream does not support writing");
  227. if (bufferSize <= 0)
  228. throw new ArgumentOutOfRangeException ("bufferSize");
  229. var buffer = new byte [bufferSize];
  230. int nread;
  231. while ((nread = Read (buffer, 0, bufferSize)) != 0)
  232. destination.Write (buffer, 0, nread);
  233. }
  234. public virtual void ObjectInvariant ()
  235. {
  236. }
  237. #endif
  238. }
  239. class NullStream : Stream
  240. {
  241. public override bool CanRead
  242. {
  243. get {
  244. return true;
  245. }
  246. }
  247. public override bool CanSeek
  248. {
  249. get {
  250. return true;
  251. }
  252. }
  253. public override bool CanWrite
  254. {
  255. get {
  256. return true;
  257. }
  258. }
  259. public override long Length
  260. {
  261. get {
  262. return 0;
  263. }
  264. }
  265. public override long Position
  266. {
  267. get {
  268. return 0;
  269. }
  270. set {
  271. }
  272. }
  273. public override void Flush ()
  274. {
  275. }
  276. public override int Read (byte[] buffer, int offset, int count)
  277. {
  278. return 0;
  279. }
  280. public override int ReadByte ()
  281. {
  282. return -1;
  283. }
  284. public override long Seek (long offset, SeekOrigin origin)
  285. {
  286. return 0;
  287. }
  288. public override void SetLength (long value)
  289. {
  290. }
  291. public override void Write (byte[] buffer, int offset, int count)
  292. {
  293. }
  294. public override void WriteByte (byte value)
  295. {
  296. }
  297. }
  298. class SynchronizedStream : Stream {
  299. Stream source;
  300. object slock;
  301. internal SynchronizedStream (Stream source)
  302. {
  303. this.source = source;
  304. slock = new object ();
  305. }
  306. public override bool CanRead
  307. {
  308. get {
  309. lock (slock)
  310. return source.CanRead;
  311. }
  312. }
  313. public override bool CanSeek
  314. {
  315. get {
  316. lock (slock)
  317. return source.CanSeek;
  318. }
  319. }
  320. public override bool CanWrite
  321. {
  322. get {
  323. lock (slock)
  324. return source.CanWrite;
  325. }
  326. }
  327. public override long Length
  328. {
  329. get {
  330. lock (slock)
  331. return source.Length;
  332. }
  333. }
  334. public override long Position
  335. {
  336. get {
  337. lock (slock)
  338. return source.Position;
  339. }
  340. set {
  341. lock (slock)
  342. source.Position = value;
  343. }
  344. }
  345. public override void Flush ()
  346. {
  347. lock (slock)
  348. source.Flush ();
  349. }
  350. public override int Read (byte[] buffer, int offset, int count)
  351. {
  352. lock (slock)
  353. return source.Read (buffer, offset, count);
  354. }
  355. public override int ReadByte ()
  356. {
  357. lock (slock)
  358. return source.ReadByte ();
  359. }
  360. public override long Seek (long offset, SeekOrigin origin)
  361. {
  362. lock (slock)
  363. return source.Seek (offset, origin);
  364. }
  365. public override void SetLength (long value)
  366. {
  367. lock (slock)
  368. source.SetLength (value);
  369. }
  370. public override void Write (byte[] buffer, int offset, int count)
  371. {
  372. lock (slock)
  373. source.Write (buffer, offset, count);
  374. }
  375. public override void WriteByte (byte value)
  376. {
  377. lock (slock)
  378. source.WriteByte (value);
  379. }
  380. }
  381. }