FileStream.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // System.IO/FileStream.cs
  3. //
  4. // Authors:
  5. // Dietmar Maurer ([email protected])
  6. // Dan Lewis ([email protected])
  7. //
  8. // (C) 2001 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Runtime.CompilerServices;
  12. // FIXME: emit the correct exceptions everywhere. add error handling.
  13. namespace System.IO
  14. {
  15. public class FileStream : Stream
  16. {
  17. // construct from handle
  18. public FileStream (IntPtr handle, FileAccess access)
  19. : this (handle, access, true, DefaultBufferSize, false) {}
  20. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle)
  21. : this (handle, access, ownsHandle, DefaultBufferSize, false) {}
  22. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
  23. : this (handle, access, ownsHandle, bufferSize, false) {}
  24. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize, bool isAsync)
  25. {
  26. this.handle = handle;
  27. this.access = access;
  28. this.owner = ownsHandle;
  29. this.async = isAsync;
  30. InitBuffer (bufferSize);
  31. }
  32. // construct from filename
  33. public FileStream (string name, FileMode mode)
  34. : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, DefaultBufferSize, false) { }
  35. public FileStream (string name, FileMode mode, FileAccess access)
  36. : this (name, mode, access, FileShare.ReadWrite, DefaultBufferSize, false) { }
  37. public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
  38. : this (name, mode, access, share, DefaultBufferSize, false) { }
  39. public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  40. : this (name, mode, access, share, bufferSize, false) { }
  41. public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool isAsync)
  42. {
  43. if (name == null)
  44. throw new ArgumentNullException ();
  45. if (name == "" || name.IndexOfAny (Path.InvalidPathChars) != -1)
  46. throw new ArgumentException ();
  47. // TODO: demand permissions
  48. this.handle = MonoIO.Open (name, mode, access, share);
  49. if (handle == MonoIO.InvalidHandle)
  50. throw MonoIO.GetException ();
  51. this.access = access;
  52. this.owner = true;
  53. this.async = isAsync;
  54. InitBuffer (bufferSize);
  55. }
  56. // properties
  57. public override bool CanRead {
  58. get {
  59. return access == FileAccess.Read ||
  60. access == FileAccess.ReadWrite;
  61. }
  62. }
  63. public override bool CanWrite {
  64. get {
  65. return access == FileAccess.Write ||
  66. access == FileAccess.ReadWrite;
  67. }
  68. }
  69. public override bool CanSeek {
  70. get {
  71. return true; // FIXME: false for pipes & streams
  72. }
  73. }
  74. public override long Length {
  75. get { return MonoIO.GetLength (handle); }
  76. }
  77. public override long Position {
  78. get { return buf_start + buf_offset; }
  79. set { Seek (value, SeekOrigin.Begin); }
  80. }
  81. public virtual IntPtr Handle {
  82. get { return handle; }
  83. }
  84. // methods
  85. public override int ReadByte ()
  86. {
  87. if (buf_offset >= buf_length) {
  88. RefillBuffer ();
  89. if (buf_length == 0)
  90. return -1;
  91. }
  92. return buf [buf_offset ++];
  93. }
  94. public override void WriteByte (byte value)
  95. {
  96. if (buf_offset == buf_size)
  97. FlushBuffer ();
  98. buf [buf_offset ++] = value;
  99. if (buf_offset > buf_length)
  100. buf_length = buf_offset;
  101. buf_dirty = true;
  102. }
  103. public override int Read (byte[] dest, int dest_offset, int count)
  104. {
  105. int copied = 0;
  106. while (count > 0) {
  107. int n = ReadSegment (dest, dest_offset + copied, count);
  108. copied += n;
  109. count -= n;
  110. if (count == 0)
  111. break;
  112. if (count > buf_size) { // shortcut for long reads
  113. FlushBuffer ();
  114. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  115. n = MonoIO.Read (handle, dest, dest_offset + copied, count);
  116. copied += n;
  117. buf_start += n;
  118. break;
  119. }
  120. RefillBuffer ();
  121. if (buf_length == 0)
  122. break;
  123. }
  124. return copied;
  125. }
  126. public override void Write (byte[] src, int src_offset, int count)
  127. {
  128. int copied = 0;
  129. while (count > 0) {
  130. int n = WriteSegment (src, src_offset + copied, count);
  131. copied += n;
  132. count -= n;
  133. if (count == 0)
  134. break;
  135. FlushBuffer ();
  136. if (count > buf_size) { // shortcut for long writes
  137. MonoIO.Write (handle, src, src_offset + copied, count);
  138. buf_start += count;
  139. break;
  140. }
  141. }
  142. }
  143. public override long Seek (long offset, SeekOrigin origin)
  144. {
  145. long pos;
  146. // make absolute
  147. switch (origin) {
  148. case SeekOrigin.End:
  149. pos = Length - offset;
  150. break;
  151. case SeekOrigin.Current:
  152. pos = Position + offset;
  153. break;
  154. case SeekOrigin.Begin: default:
  155. pos = offset;
  156. break;
  157. }
  158. if (pos >= buf_start && pos <= buf_start + buf_length) {
  159. buf_offset = (int) (pos - buf_start);
  160. return pos;
  161. }
  162. FlushBuffer ();
  163. buf_start = MonoIO.Seek (handle, pos, SeekOrigin.Begin);
  164. return buf_start;
  165. }
  166. public override void SetLength (long length)
  167. {
  168. Flush ();
  169. MonoIO.SetLength (handle, length);
  170. }
  171. public override void Flush ()
  172. {
  173. FlushBuffer ();
  174. MonoIO.Flush (handle);
  175. }
  176. public override void Close ()
  177. {
  178. Dispose (true);
  179. GC.SuppressFinalize (this); // remove from finalize queue
  180. }
  181. public override void Dispose ()
  182. {
  183. Close ();
  184. }
  185. // protected
  186. ~FileStream ()
  187. {
  188. Dispose (false);
  189. }
  190. protected override void Dispose (bool disposing) {
  191. if (handle != MonoIO.InvalidHandle) {
  192. Flush ();
  193. MonoIO.Close (handle);
  194. handle = MonoIO.InvalidHandle;
  195. }
  196. if (disposing)
  197. buf = null;
  198. }
  199. // private
  200. private int ReadSegment (byte [] dest, int dest_offset, int count)
  201. {
  202. if (count > buf_length - buf_offset)
  203. count = buf_length - buf_offset;
  204. if (count > 0) {
  205. Buffer.BlockCopy (buf, buf_offset, dest, dest_offset, count);
  206. buf_offset += count;
  207. }
  208. return count;
  209. }
  210. private int WriteSegment (byte [] src, int src_offset, int count)
  211. {
  212. if (count > buf_size - buf_offset)
  213. count = buf_size - buf_offset;
  214. if (count > 0) {
  215. Buffer.BlockCopy (src, src_offset, buf, buf_offset, count);
  216. buf_offset += count;
  217. if (buf_offset > buf_length)
  218. buf_length = buf_offset;
  219. buf_dirty = true;
  220. }
  221. return count;
  222. }
  223. private void FlushBuffer ()
  224. {
  225. if (buf_dirty) {
  226. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  227. MonoIO.Write (handle, buf, 0, buf_length);
  228. }
  229. buf_start += buf_length;
  230. buf_offset = buf_length = 0;
  231. buf_dirty = false;
  232. }
  233. private void RefillBuffer ()
  234. {
  235. FlushBuffer ();
  236. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  237. buf_length = MonoIO.Read (handle, buf, 0, buf_size);
  238. }
  239. private void InitBuffer (int size)
  240. {
  241. buf = new byte [size];
  242. buf_size = size;
  243. buf_start = 0;
  244. buf_offset = buf_length = 0;
  245. buf_dirty = false;
  246. }
  247. // fields
  248. private static int DefaultBufferSize = 8192;
  249. private FileAccess access;
  250. private bool owner;
  251. private bool async;
  252. private byte [] buf; // the buffer
  253. private int buf_size; // capacity in bytes
  254. private int buf_length; // number of valid bytes in buffer
  255. private int buf_offset; // position of next byte
  256. private bool buf_dirty; // true if buffer has been written to
  257. private long buf_start; // location of buffer in file
  258. IntPtr handle; // handle to underlying file
  259. }
  260. }