FileStream.cs 8.6 KB

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