FileStream.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. MonoIO.Flush (handle);
  181. }
  182. public override void Close ()
  183. {
  184. Dispose (true);
  185. GC.SuppressFinalize (this); // remove from finalize queue
  186. }
  187. // protected
  188. ~FileStream ()
  189. {
  190. Dispose (false);
  191. }
  192. protected virtual void Dispose (bool disposing) {
  193. if (handle != MonoIO.InvalidHandle) {
  194. Flush ();
  195. MonoIO.Close (handle);
  196. handle = MonoIO.InvalidHandle;
  197. }
  198. if (disposing)
  199. buf = null;
  200. }
  201. // private
  202. private int ReadSegment (byte [] dest, int dest_offset, int count)
  203. {
  204. if (count > buf_length - buf_offset)
  205. count = buf_length - buf_offset;
  206. if (count > 0) {
  207. Buffer.BlockCopy (buf, buf_offset, dest, dest_offset, count);
  208. buf_offset += count;
  209. }
  210. return count;
  211. }
  212. private int WriteSegment (byte [] src, int src_offset, int count)
  213. {
  214. if (count > buf_size - buf_offset)
  215. count = buf_size - buf_offset;
  216. if (count > 0) {
  217. Buffer.BlockCopy (src, src_offset, buf, buf_offset, count);
  218. buf_offset += count;
  219. if (buf_offset > buf_length)
  220. buf_length = buf_offset;
  221. buf_dirty = true;
  222. }
  223. return count;
  224. }
  225. private void FlushBuffer ()
  226. {
  227. if (buf_dirty) {
  228. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  229. MonoIO.Write (handle, buf, 0, buf_length);
  230. }
  231. buf_start += buf_length;
  232. buf_offset = buf_length = 0;
  233. buf_dirty = false;
  234. }
  235. private void RefillBuffer ()
  236. {
  237. FlushBuffer ();
  238. MonoIO.Seek (handle, buf_start, SeekOrigin.Begin);
  239. buf_length = MonoIO.Read (handle, buf, 0, buf_size);
  240. }
  241. private void InitBuffer (int size)
  242. {
  243. if (size < 0)
  244. throw new ArgumentOutOfRangeException ("Buffer size cannot be negative.");
  245. if (size < 8)
  246. size = 8;
  247. buf = new byte [size];
  248. buf_size = size;
  249. buf_start = 0;
  250. buf_offset = buf_length = 0;
  251. buf_dirty = false;
  252. }
  253. // fields
  254. private static int DefaultBufferSize = 8192;
  255. private FileAccess access;
  256. private bool owner;
  257. private bool async;
  258. private byte [] buf; // the buffer
  259. private int buf_size; // capacity in bytes
  260. private int buf_length; // number of valid bytes in buffer
  261. private int buf_offset; // position of next byte
  262. private bool buf_dirty; // true if buffer has been written to
  263. private long buf_start; // location of buffer in file
  264. private string name = "[Unknown]"; // name of file.
  265. IntPtr handle; // handle to underlying file
  266. }
  267. }