FileStream.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // System.IO/FileStream.cs
  3. //
  4. // Author:
  5. // Dietmar Maurer ([email protected])
  6. //
  7. // (C) 2001 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System;
  10. using System.PAL;
  11. using System.Runtime.InteropServices;
  12. using System.Threading;
  13. // fixme: I do not know how to handle errno when calling PInvoke functions
  14. // fixme: emit the correct exceptions everywhere
  15. namespace System.IO
  16. {
  17. public class FileStream : Stream
  18. {
  19. private OpSys _os = Platform.OS;
  20. private IntPtr fdhandle;
  21. private FileAccess acc;
  22. private bool owner;
  23. public FileStream (IntPtr handle, FileAccess access)
  24. : this (handle, access, true, 0, false) {}
  25. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle)
  26. : this (handle, access, ownsHandle, 0, false) {}
  27. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle, int bufferSize)
  28. : this (handle, access, ownsHandle, bufferSize, false) {}
  29. public FileStream (IntPtr handle, FileAccess access, bool ownsHandle,
  30. int bufferSize, bool isAsync)
  31. {
  32. fdhandle = handle;
  33. //acc = access;
  34. //owner = ownsHandle;
  35. }
  36. public FileStream (string name, FileMode mode)
  37. : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, 0, false) {}
  38. public FileStream (string name, FileMode mode, FileAccess access)
  39. : this (name, mode, access, FileShare.ReadWrite, 0, false) {}
  40. public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
  41. : this (name, mode, access, share, 0, false) {}
  42. public FileStream (string name, FileMode mode, FileAccess access,
  43. FileShare share, int buferSize)
  44. : this (name, mode, access, share, 0, false) {}
  45. // fixme: implement all share, buffer, async
  46. public FileStream (string name, FileMode mode, FileAccess access, FileShare share,
  47. int buferSize, bool useAsync)
  48. {
  49. if (name == null)
  50. throw new ArgumentNullException ();
  51. if (name == "" || name.IndexOfAny (Path.InvalidPathChars) != -1)
  52. throw new ArgumentException ();
  53. fdhandle = _os.OpenFile (name, mode, access, share);
  54. /* Implement error checking, with some sort of access
  55. to the errno error reason
  56. if(fdhandle == error) {
  57. throw new IOException();
  58. }
  59. */
  60. acc = access;
  61. owner = true;
  62. }
  63. public override bool CanRead
  64. {
  65. get {
  66. switch (acc) {
  67. case FileAccess.Read:
  68. case FileAccess.ReadWrite:
  69. return true;
  70. case FileAccess.Write:
  71. default:
  72. return false;
  73. }
  74. }
  75. }
  76. public override bool CanSeek
  77. {
  78. get {
  79. // fixme: not alway true
  80. return true;
  81. }
  82. }
  83. public override bool CanWrite
  84. {
  85. get {
  86. switch (acc) {
  87. case FileAccess.Write:
  88. case FileAccess.ReadWrite:
  89. return true;
  90. default:
  91. return false;
  92. }
  93. }
  94. }
  95. unsafe public override long Length
  96. {
  97. get {
  98. return _os.FileLength (fdhandle);
  99. }
  100. }
  101. public override long Position
  102. {
  103. get {
  104. return _os.SeekFile (fdhandle, 0, SeekOrigin.Current);
  105. }
  106. set {
  107. _os.SeekFile (fdhandle, value, SeekOrigin.Begin);
  108. }
  109. }
  110. public override void Flush ()
  111. {
  112. }
  113. public override void Close ()
  114. {
  115. if (owner) {
  116. _os.CloseFile (fdhandle);
  117. }
  118. }
  119. public unsafe override int Read (byte[] buffer,
  120. int offset,
  121. int count)
  122. {
  123. return _os.ReadFile (fdhandle, buffer, offset, count);
  124. }
  125. public unsafe override int ReadByte ()
  126. {
  127. byte[] val = new byte[1];
  128. int res = Read (val, 0, 1);
  129. if (res == -1)
  130. throw new IOException();
  131. if (res == 0)
  132. return -1;
  133. return val[0];
  134. }
  135. public override long Seek (long offset,
  136. SeekOrigin origin)
  137. {
  138. return _os.SeekFile (fdhandle, offset, origin);
  139. }
  140. public override void SetLength (long value)
  141. {
  142. _os.SetLengthFile (fdhandle, value);
  143. }
  144. public unsafe override void Write (byte[] buffer,
  145. int offset,
  146. int count)
  147. {
  148. int res = _os.WriteFile (fdhandle, buffer, offset, count);
  149. if (res != count)
  150. throw new IOException();
  151. }
  152. public unsafe override void WriteByte (byte value)
  153. {
  154. byte[] buf = new byte[1];
  155. buf[0] = value;
  156. Write (buf, 0, 1);
  157. }
  158. public virtual IntPtr Handle
  159. {
  160. get {
  161. return(fdhandle);
  162. }
  163. }
  164. }
  165. }