FileStream.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. Flush ();
  167. FileSetLength (handle, length);
  168. }
  169. public override void Flush ()
  170. {
  171. FlushBuffer ();
  172. FileFlush (handle);
  173. }
  174. public override void Close ()
  175. {
  176. Dispose (true);
  177. GC.SuppressFinalize (this); // remove from finalize queue
  178. }
  179. public override void Dispose ()
  180. {
  181. Close ();
  182. }
  183. // protected
  184. ~FileStream ()
  185. {
  186. Dispose (false);
  187. }
  188. protected override void Dispose (bool disposing) {
  189. if (handle != IntPtr.Zero) {
  190. Flush ();
  191. FileClose (handle);
  192. handle = IntPtr.Zero;
  193. }
  194. if (disposing)
  195. buf = null;
  196. }
  197. // private
  198. private int ReadSegment (byte [] dest, int dest_offset, int count)
  199. {
  200. if (count > buf_length - buf_offset)
  201. count = buf_length - buf_offset;
  202. if (count > 0) {
  203. Buffer.BlockCopy (buf, buf_offset, dest, dest_offset, count);
  204. buf_offset += count;
  205. }
  206. return count;
  207. }
  208. private int WriteSegment (byte [] src, int src_offset, int count)
  209. {
  210. if (count > buf_size - buf_offset)
  211. count = buf_size - buf_offset;
  212. if (count > 0) {
  213. Buffer.BlockCopy (src, src_offset, buf, buf_offset, count);
  214. buf_offset += count;
  215. if (buf_offset > buf_length)
  216. buf_length = buf_offset;
  217. buf_dirty = true;
  218. }
  219. return count;
  220. }
  221. private void FlushBuffer ()
  222. {
  223. if (buf_dirty) {
  224. FileSeek (handle, buf_start, SeekOrigin.Begin);
  225. FileWrite (handle, buf, 0, buf_length);
  226. }
  227. buf_start += buf_length;
  228. buf_offset = buf_length = 0;
  229. buf_dirty = false;
  230. }
  231. private void RefillBuffer ()
  232. {
  233. FlushBuffer ();
  234. FileSeek (handle, buf_start, SeekOrigin.Begin);
  235. buf_length = FileRead (handle, buf, 0, buf_size);
  236. }
  237. private void InitBuffer (int size)
  238. {
  239. buf = new byte [size];
  240. buf_size = size;
  241. buf_start = 0;
  242. buf_offset = buf_length = 0;
  243. buf_dirty = false;
  244. }
  245. // internal calls
  246. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  247. private extern static IntPtr FileOpen (string filename, FileMode mode, FileAccess access, FileShare share);
  248. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  249. private extern static void FileClose (IntPtr handle);
  250. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  251. private extern static int FileRead (IntPtr handle, byte [] dest, int dest_offset, int count);
  252. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  253. private extern static int FileWrite (IntPtr handle, byte [] src, int src_offset, int count);
  254. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  255. private extern static long FileSeek (IntPtr handle, long offset, SeekOrigin origin);
  256. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  257. private extern static long FileGetLength (IntPtr handle);
  258. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  259. private extern static void FileSetLength (IntPtr handle, long length);
  260. [MethodImplAttribute (MethodImplOptions.InternalCall)]
  261. private extern static void FileFlush (IntPtr handle);
  262. // fields
  263. private static int DefaultBufferSize = 8192;
  264. private FileAccess access;
  265. private bool owner;
  266. private bool async;
  267. private byte [] buf; // the buffer
  268. private int buf_size; // capacity in bytes
  269. private int buf_length; // number of valid bytes in buffer
  270. private int buf_offset; // position of next byte
  271. private bool buf_dirty; // true if buffer has been written to
  272. private long buf_start; // location of buffer in file
  273. IntPtr handle; // handle to underlying file
  274. }
  275. }