FileStream.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. MonoIOError error;
  31. if(MonoIO.GetFileType (handle, out error) ==
  32. MonoFileType.Disk) {
  33. this.canseek = true;
  34. } else {
  35. this.canseek = false;
  36. }
  37. InitBuffer (bufferSize);
  38. /* Can't set append mode */
  39. this.append_startpos=0;
  40. }
  41. // construct from filename
  42. public FileStream (string name, FileMode mode)
  43. : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, DefaultBufferSize, false) { }
  44. public FileStream (string name, FileMode mode, FileAccess access)
  45. : this (name, mode, access, FileShare.ReadWrite, DefaultBufferSize, false) { }
  46. public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
  47. : this (name, mode, access, share, DefaultBufferSize, false) { }
  48. public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize)
  49. : this (name, mode, access, share, bufferSize, false) { }
  50. public FileStream (string name, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool isAsync)
  51. {
  52. if (name == null) {
  53. throw new ArgumentNullException ("Name is null");
  54. }
  55. if (name == "") {
  56. throw new ArgumentException ("Name is empty");
  57. }
  58. if (name.IndexOfAny (Path.InvalidPathChars) != -1) {
  59. throw new ArgumentException ("Name has invalid chars");
  60. }
  61. if (Directory.Exists (name)) {
  62. throw new UnauthorizedAccessException ("Access to the path '" + Path.GetFullPath (name) + "' is denied.");
  63. }
  64. /* Append streams can't be read (see FileMode
  65. * docs)
  66. */
  67. if (mode==FileMode.Append &&
  68. (access&FileAccess.Read)==FileAccess.Read) {
  69. throw new ArgumentException("Append streams can not be read");
  70. }
  71. this.name = name;
  72. // TODO: demand permissions
  73. MonoIOError error;
  74. this.handle = MonoIO.Open (name, mode, access, share,
  75. out error);
  76. if (handle == MonoIO.InvalidHandle) {
  77. throw MonoIO.GetException (name, error);
  78. }
  79. this.access = access;
  80. this.owner = true;
  81. this.async = isAsync;
  82. /* Can we open non-files by name? */
  83. if (MonoIO.GetFileType (handle, out error) ==
  84. MonoFileType.Disk) {
  85. this.canseek = true;
  86. } else {
  87. this.canseek = false;
  88. }
  89. InitBuffer (bufferSize);
  90. if (mode==FileMode.Append) {
  91. this.Seek (0, SeekOrigin.End);
  92. this.append_startpos=this.Position;
  93. } else {
  94. this.append_startpos=0;
  95. }
  96. }
  97. // properties
  98. public override bool CanRead {
  99. get {
  100. return access == FileAccess.Read ||
  101. access == FileAccess.ReadWrite;
  102. }
  103. }
  104. public override bool CanWrite {
  105. get {
  106. return access == FileAccess.Write ||
  107. access == FileAccess.ReadWrite;
  108. }
  109. }
  110. public override bool CanSeek {
  111. get {
  112. return(canseek);
  113. }
  114. }
  115. public virtual bool IsAsync {
  116. get {
  117. return (async);
  118. }
  119. }
  120. public string Name {
  121. get {
  122. return name;
  123. }
  124. }
  125. public override long Length {
  126. get {
  127. MonoIOError error;
  128. return MonoIO.GetLength (handle, out error);
  129. }
  130. }
  131. public override long Position {
  132. get {
  133. if(CanSeek == false) {
  134. throw new NotSupportedException("The stream does not support seeking");
  135. }
  136. lock(this) {
  137. return(buf_start + buf_offset);
  138. }
  139. }
  140. set {
  141. if(CanSeek == false) {
  142. throw new NotSupportedException("The stream does not support seeking");
  143. }
  144. if(value < 0) {
  145. throw new ArgumentOutOfRangeException("Attempt to set the position to a negative value");
  146. }
  147. Seek (value, SeekOrigin.Begin);
  148. }
  149. }
  150. public virtual IntPtr Handle {
  151. get {
  152. return handle;
  153. }
  154. }
  155. // methods
  156. public override int ReadByte ()
  157. {
  158. if (handle == MonoIO.InvalidHandle)
  159. throw new ObjectDisposedException ("Stream has been closed");
  160. lock(this) {
  161. if (buf_offset >= buf_length) {
  162. RefillBuffer ();
  163. if (buf_length == 0) {
  164. return -1;
  165. }
  166. }
  167. return(buf [buf_offset ++]);
  168. }
  169. }
  170. public override void WriteByte (byte value)
  171. {
  172. lock(this) {
  173. if (buf_offset == buf_size) {
  174. FlushBuffer ();
  175. }
  176. buf [buf_offset ++] = value;
  177. if (buf_offset > buf_length) {
  178. buf_length = buf_offset;
  179. }
  180. buf_dirty = true;
  181. }
  182. }
  183. public override int Read (byte[] dest, int dest_offset, int count)
  184. {
  185. if (handle == MonoIO.InvalidHandle)
  186. throw new ObjectDisposedException ("Stream has been closed");
  187. if (dest == null)
  188. throw new ArgumentNullException ("destination array is null");
  189. if (!CanRead)
  190. throw new NotSupportedException ("Stream does not support reading");
  191. int len = dest.Length;
  192. if (dest_offset < 0 || count < 0)
  193. throw new ArgumentException ("dest or count is negative");
  194. if (dest_offset > len)
  195. throw new ArgumentException ("destination offset is beyond array size");
  196. if ((dest_offset + count) > len)
  197. throw new ArgumentException ("Reading would overrun buffer");
  198. int copied = 0;
  199. lock(this) {
  200. int n = ReadSegment (dest, dest_offset, count);
  201. copied += n;
  202. count -= n;
  203. if (count == 0) {
  204. /* If there was already enough
  205. * buffered, no need to read
  206. * more from the file.
  207. */
  208. return (copied);
  209. }
  210. if (count > buf_size) {
  211. /* Read as much as we can, up
  212. * to count bytes
  213. */
  214. FlushBuffer();
  215. n = ReadData (handle, dest,
  216. dest_offset+copied,
  217. count);
  218. /* Make the next buffer read
  219. * start from the right place
  220. */
  221. buf_start += n;
  222. } else {
  223. RefillBuffer ();
  224. n = ReadSegment (dest,
  225. dest_offset+copied,
  226. count);
  227. }
  228. copied += n;
  229. return(copied);
  230. }
  231. }
  232. public override void Write (byte[] src, int src_offset, int count)
  233. {
  234. int copied = 0;
  235. while (count > 0) {
  236. int n = WriteSegment (src, src_offset + copied, count);
  237. copied += n;
  238. count -= n;
  239. FlushBuffer ();
  240. if (count == 0) {
  241. break;
  242. }
  243. if (count > buf_size) {
  244. // shortcut for long writes
  245. MonoIOError error;
  246. MonoIO.Write (handle, src, src_offset + copied, count, out error);
  247. buf_start += count;
  248. break;
  249. }
  250. }
  251. }
  252. public override long Seek (long offset, SeekOrigin origin)
  253. {
  254. long pos;
  255. if (handle == MonoIO.InvalidHandle)
  256. throw new ObjectDisposedException ("Stream has been closed");
  257. // make absolute
  258. if(CanSeek == false) {
  259. throw new NotSupportedException("The stream does not support seeking");
  260. }
  261. lock(this) {
  262. switch (origin) {
  263. case SeekOrigin.End:
  264. pos = Length + offset;
  265. break;
  266. case SeekOrigin.Current:
  267. pos = Position + offset;
  268. break;
  269. case SeekOrigin.Begin: default:
  270. pos = offset;
  271. break;
  272. }
  273. if (pos < 0) {
  274. /* LAMESPEC: shouldn't this be
  275. * ArgumentOutOfRangeException?
  276. */
  277. throw new ArgumentException("Attempted to Seek before the beginning of the stream");
  278. }
  279. if(pos < this.append_startpos) {
  280. /* More undocumented crap */
  281. throw new IOException("Can't seek back over pre-existing data in append mode");
  282. }
  283. if (pos >= buf_start &&
  284. pos <= buf_start + buf_length) {
  285. buf_offset = (int) (pos - buf_start);
  286. return pos;
  287. }
  288. FlushBuffer ();
  289. MonoIOError error;
  290. buf_start = MonoIO.Seek (handle, pos,
  291. SeekOrigin.Begin,
  292. out error);
  293. return(buf_start);
  294. }
  295. }
  296. public override void SetLength (long length)
  297. {
  298. if(CanSeek == false) {
  299. throw new NotSupportedException("The stream does not support seeking");
  300. }
  301. if(CanWrite == false) {
  302. throw new NotSupportedException("The stream does not support writing");
  303. }
  304. if(length < 0) {
  305. throw new ArgumentOutOfRangeException("Length is less than 0");
  306. }
  307. Flush ();
  308. MonoIOError error;
  309. MonoIO.SetLength (handle, length, out error);
  310. }
  311. public override void Flush ()
  312. {
  313. lock(this) {
  314. FlushBuffer ();
  315. }
  316. // The flushing is not actually required, in
  317. //the mono runtime we were mapping flush to
  318. //`fsync' which is not the same.
  319. //
  320. //MonoIO.Flush (handle);
  321. }
  322. public override void Close ()
  323. {
  324. Dispose (true);
  325. GC.SuppressFinalize (this); // remove from finalize queue
  326. }
  327. // protected
  328. ~FileStream ()
  329. {
  330. Dispose (false);
  331. }
  332. protected virtual void Dispose (bool disposing) {
  333. if (owner && handle != MonoIO.InvalidHandle) {
  334. lock(this) {
  335. FlushBuffer ();
  336. }
  337. MonoIOError error;
  338. MonoIO.Close (handle, out error);
  339. handle = MonoIO.InvalidHandle;
  340. }
  341. if (disposing) {
  342. buf = null;
  343. }
  344. }
  345. // private.
  346. // ReadSegment, WriteSegment, FlushBuffer,
  347. // RefillBuffer and ReadData should only be called
  348. // when the Monitor lock is held, but these methods
  349. // grab it again just to be safe.
  350. private int ReadSegment (byte [] dest, int dest_offset,
  351. int count)
  352. {
  353. if (count > buf_length - buf_offset) {
  354. count = buf_length - buf_offset;
  355. }
  356. if (count > 0) {
  357. Buffer.BlockCopy (buf, buf_offset,
  358. dest, dest_offset,
  359. count);
  360. buf_offset += count;
  361. }
  362. return(count);
  363. }
  364. private int WriteSegment (byte [] src, int src_offset,
  365. int count)
  366. {
  367. if (count > buf_size - buf_offset) {
  368. count = buf_size - buf_offset;
  369. }
  370. if (count > 0) {
  371. Buffer.BlockCopy (src, src_offset,
  372. buf, buf_offset,
  373. count);
  374. buf_offset += count;
  375. if (buf_offset > buf_length) {
  376. buf_length = buf_offset;
  377. }
  378. buf_dirty = true;
  379. }
  380. return(count);
  381. }
  382. private void FlushBuffer ()
  383. {
  384. if (buf_dirty) {
  385. MonoIOError error;
  386. if (CanSeek == true) {
  387. MonoIO.Seek (handle, buf_start,
  388. SeekOrigin.Begin,
  389. out error);
  390. }
  391. MonoIO.Write (handle, buf, 0,
  392. buf_length, out error);
  393. }
  394. buf_start += buf_length;
  395. buf_offset = buf_length = 0;
  396. buf_dirty = false;
  397. }
  398. private void RefillBuffer ()
  399. {
  400. FlushBuffer();
  401. buf_length = ReadData (handle, buf, 0,
  402. buf_size);
  403. }
  404. private int ReadData (IntPtr handle, byte[] buf, int offset,
  405. int count)
  406. {
  407. MonoIOError error;
  408. int amount = MonoIO.Read (handle, buf, offset,
  409. count, out error);
  410. /* Check for read error */
  411. if(amount == -1) {
  412. /* Kludge around broken pipes */
  413. if(error == MonoIOError.ERROR_BROKEN_PIPE) {
  414. amount = 0;
  415. } else {
  416. throw new IOException ();
  417. }
  418. }
  419. return(amount);
  420. }
  421. private void InitBuffer (int size)
  422. {
  423. if (size < 0)
  424. throw new ArgumentOutOfRangeException ("Buffer size cannot be negative.");
  425. if (size < 8)
  426. size = 8;
  427. buf = new byte [size];
  428. buf_size = size;
  429. buf_start = 0;
  430. buf_offset = buf_length = 0;
  431. buf_dirty = false;
  432. }
  433. // fields
  434. private static int DefaultBufferSize = 8192;
  435. private FileAccess access;
  436. private bool owner;
  437. private bool async;
  438. private bool canseek;
  439. private long append_startpos;
  440. private byte [] buf; // the buffer
  441. private int buf_size; // capacity in bytes
  442. private int buf_length; // number of valid bytes in buffer
  443. private int buf_offset; // position of next byte
  444. private bool buf_dirty; // true if buffer has been written to
  445. private long buf_start; // location of buffer in file
  446. private string name = "[Unknown]"; // name of file.
  447. IntPtr handle; // handle to underlying file
  448. }
  449. }