FileStream.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. this.name = name;
  48. // TODO: demand permissions
  49. this.handle = MonoIO.Open (name, mode, access, share);
  50. if (handle == MonoIO.InvalidHandle)
  51. throw MonoIO.GetException (name);
  52. this.access = access;
  53. this.owner = true;
  54. this.async = isAsync;
  55. InitBuffer (bufferSize);
  56. }
  57. // properties
  58. public override bool CanRead {
  59. get {
  60. return access == FileAccess.Read ||
  61. access == FileAccess.ReadWrite;
  62. }
  63. }
  64. public override bool CanWrite {
  65. get {
  66. return access == FileAccess.Write ||
  67. access == FileAccess.ReadWrite;
  68. }
  69. }
  70. public override bool CanSeek {
  71. get {
  72. return true; // FIXME: false for pipes & streams
  73. }
  74. }
  75. public string Name {
  76. get {
  77. return name;
  78. }
  79. }
  80. public override long Length {
  81. get { return MonoIO.GetLength (handle); }
  82. }
  83. public override long Position {
  84. get { return buf_start + buf_offset; }
  85. set { Seek (value, SeekOrigin.Begin); }
  86. }
  87. public virtual IntPtr Handle {
  88. get { return handle; }
  89. }
  90. // methods
  91. public override int ReadByte ()
  92. {
  93. if (buf_offset >= buf_length) {
  94. RefillBuffer ();
  95. if (buf_length == 0)
  96. return -1;
  97. }
  98. return buf [buf_offset ++];
  99. }
  100. public override void WriteByte (byte value)
  101. {
  102. if (buf_offset == buf_size)
  103. FlushBuffer ();
  104. buf [buf_offset ++] = value;
  105. if (buf_offset > buf_length)
  106. buf_length = buf_offset;
  107. buf_dirty = true;
  108. }
  109. public override int Read (byte[] dest, int dest_offset, int count)
  110. {
  111. int copied = 0;
  112. while (count > 0) {
  113. int n = ReadSegment (dest, dest_offset + copied, count);
  114. copied += n;
  115. count -= n;
  116. if (count == 0)
  117. break;
  118. if (count > buf_size) { // shortcut for long reads
  119. FlushBuffer ();
  120. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  121. n = MonoIO.Read (handle, dest, dest_offset + copied, count);
  122. copied += n;
  123. buf_start += n;
  124. break;
  125. }
  126. RefillBuffer ();
  127. if (buf_length == 0)
  128. break;
  129. }
  130. return copied;
  131. }
  132. public override void Write (byte[] src, int src_offset, int count)
  133. {
  134. int copied = 0;
  135. while (count > 0) {
  136. int n = WriteSegment (src, src_offset + copied, count);
  137. copied += n;
  138. count -= n;
  139. if (count == 0)
  140. break;
  141. FlushBuffer ();
  142. if (count > buf_size) { // shortcut for long writes
  143. MonoIO.Write (handle, src, src_offset + copied, count);
  144. buf_start += count;
  145. break;
  146. }
  147. }
  148. }
  149. public override long Seek (long offset, SeekOrigin origin)
  150. {
  151. long pos;
  152. // make absolute
  153. switch (origin) {
  154. case SeekOrigin.End:
  155. pos = Length - offset;
  156. break;
  157. case SeekOrigin.Current:
  158. pos = Position + offset;
  159. break;
  160. case SeekOrigin.Begin: default:
  161. pos = offset;
  162. break;
  163. }
  164. if (pos >= buf_start && pos <= buf_start + buf_length) {
  165. buf_offset = (int) (pos - buf_start);
  166. return pos;
  167. }
  168. FlushBuffer ();
  169. buf_start = MonoIO.Seek (handle, pos, SeekOrigin.Begin);
  170. return buf_start;
  171. }
  172. public override void SetLength (long length)
  173. {
  174. Flush ();
  175. MonoIO.SetLength (handle, length);
  176. }
  177. public override void Flush ()
  178. {
  179. FlushBuffer ();
  180. //
  181. // The flushing is not actually required, in the mono runtime we were
  182. // mapping flush to `fsync' which is not the same.
  183. //
  184. //MonoIO.Flush (handle);
  185. }
  186. public override void Close ()
  187. {
  188. Dispose (true);
  189. GC.SuppressFinalize (this); // remove from finalize queue
  190. }
  191. // protected
  192. ~FileStream ()
  193. {
  194. Dispose (false);
  195. }
  196. protected virtual void Dispose (bool disposing) {
  197. if (handle != MonoIO.InvalidHandle) {
  198. FlushBuffer ();
  199. MonoIO.Close (handle);
  200. handle = MonoIO.InvalidHandle;
  201. }
  202. if (disposing)
  203. buf = null;
  204. }
  205. // private
  206. private int ReadSegment (byte [] dest, int dest_offset, int count)
  207. {
  208. if (count > buf_length - buf_offset)
  209. count = buf_length - buf_offset;
  210. if (count > 0) {
  211. Buffer.BlockCopy (buf, buf_offset, dest, dest_offset, count);
  212. buf_offset += count;
  213. }
  214. return count;
  215. }
  216. private int WriteSegment (byte [] src, int src_offset, int count)
  217. {
  218. if (count > buf_size - buf_offset)
  219. count = buf_size - buf_offset;
  220. if (count > 0) {
  221. Buffer.BlockCopy (src, src_offset, buf, buf_offset, count);
  222. buf_offset += count;
  223. if (buf_offset > buf_length)
  224. buf_length = buf_offset;
  225. buf_dirty = true;
  226. }
  227. return count;
  228. }
  229. private void FlushBuffer ()
  230. {
  231. if (buf_dirty) {
  232. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  233. MonoIO.Write (handle, buf, 0, buf_length);
  234. }
  235. buf_start += buf_length;
  236. buf_offset = buf_length = 0;
  237. buf_dirty = false;
  238. }
  239. private void RefillBuffer ()
  240. {
  241. FlushBuffer ();
  242. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  243. buf_length = MonoIO.Read (handle, buf, 0, buf_size);
  244. }
  245. private void InitBuffer (int size)
  246. {
  247. if (size < 0)
  248. throw new ArgumentOutOfRangeException ("Buffer size cannot be negative.");
  249. if (size < 8)
  250. size = 8;
  251. buf = new byte [size];
  252. buf_size = size;
  253. buf_start = 0;
  254. buf_offset = buf_length = 0;
  255. buf_dirty = false;
  256. }
  257. // fields
  258. private static int DefaultBufferSize = 8192;
  259. private FileAccess access;
  260. private bool owner;
  261. private bool async;
  262. private byte [] buf; // the buffer
  263. private int buf_size; // capacity in bytes
  264. private int buf_length; // number of valid bytes in buffer
  265. private int buf_offset; // position of next byte
  266. private bool buf_dirty; // true if buffer has been written to
  267. private long buf_start; // location of buffer in file
  268. private string name = "[Unknown]"; // name of file.
  269. IntPtr handle; // handle to underlying file
  270. }
  271. }