FileStream.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // fixme: I do not know how to handle errno when calling PInvoke functions
  13. // fixme: emit the correct exceptions everywhere
  14. namespace System.IO
  15. {
  16. public class FileStream : Stream
  17. {
  18. private OpSys _os = Platform.OS;
  19. private IntPtr fd;
  20. private FileAccess acc;
  21. private bool owner;
  22. public FileStream (IntPtr fd, FileAccess access)
  23. : this (fd, access, true, 0, false) {}
  24. public FileStream (IntPtr fd, FileAccess access, bool ownsHandle)
  25. : this (fd, access, ownsHandle, 0, false) {}
  26. public FileStream (IntPtr fd, FileAccess access, bool ownsHandle, int bufferSize)
  27. : this (fd, access, ownsHandle, bufferSize, false) {}
  28. public FileStream (IntPtr fd, FileAccess access, bool ownsHandle,
  29. int bufferSize, bool isAsync)
  30. {
  31. fd = fd;
  32. acc = access;
  33. owner = ownsHandle;
  34. }
  35. public FileStream (string name, FileMode mode)
  36. : this (name, mode, FileAccess.ReadWrite, FileShare.ReadWrite, 0, false) {}
  37. public FileStream (string name, FileMode mode, FileAccess access)
  38. : this (name, mode, access, FileShare.ReadWrite, 0, false) {}
  39. public FileStream (string name, FileMode mode, FileAccess access, FileShare share)
  40. : this (name, mode, access, share, 0, false) {}
  41. public FileStream (string name, FileMode mode, FileAccess access,
  42. FileShare share, int buferSize)
  43. : this (name, mode, access, share, 0, false) {}
  44. // fixme: implement all share, buffer, async
  45. public FileStream (string name, FileMode mode, FileAccess access, FileShare share,
  46. int buferSize, bool useAsync)
  47. {
  48. if ((int)(fd = _os.OpenFile (name, mode, access, share)) == -1)
  49. throw new IOException();
  50. acc = access;
  51. owner = true;
  52. }
  53. public override bool CanRead
  54. {
  55. get {
  56. switch (acc) {
  57. case FileAccess.Read:
  58. case FileAccess.ReadWrite:
  59. return true;
  60. case FileAccess.Write:
  61. default:
  62. return false;
  63. }
  64. }
  65. }
  66. public override bool CanSeek
  67. {
  68. get {
  69. // fixme: not alway true
  70. return true;
  71. }
  72. }
  73. public override bool CanWrite
  74. {
  75. get {
  76. switch (acc) {
  77. case FileAccess.Write:
  78. case FileAccess.ReadWrite:
  79. return true;
  80. default:
  81. return false;
  82. }
  83. }
  84. }
  85. unsafe public override long Length
  86. {
  87. get {
  88. return _os.FileLength (fd);
  89. }
  90. }
  91. public override long Position
  92. {
  93. get {
  94. return _os.SeekFile (fd, 0, SeekOrigin.Current);
  95. }
  96. set {
  97. _os.SeekFile (fd, value, SeekOrigin.Begin);
  98. }
  99. }
  100. public override void Flush ()
  101. {
  102. }
  103. public override void Close ()
  104. {
  105. if (owner) {
  106. _os.CloseFile (fd);
  107. }
  108. }
  109. public unsafe override int Read (byte[] buffer,
  110. int offset,
  111. int count)
  112. {
  113. return _os.ReadFile (fd, buffer, offset, count);
  114. }
  115. public unsafe override int ReadByte ()
  116. {
  117. byte[] val = new byte[1];
  118. if (Read (val, 0, 1) != 1)
  119. throw new IOException();
  120. return val[0];
  121. }
  122. public override long Seek (long offset,
  123. SeekOrigin origin)
  124. {
  125. return _os.SeekFile (fd, offset, origin);
  126. }
  127. public override void SetLength (long value)
  128. {
  129. int res;
  130. if ((res = _os.SetLengthFile (fd, value)) == -1)
  131. throw new IOException();
  132. }
  133. public unsafe override void Write (byte[] buffer,
  134. int offset,
  135. int count)
  136. {
  137. int res = _os.WriteFile (fd, buffer, offset, count);
  138. if (res != count)
  139. throw new IOException();
  140. }
  141. public unsafe override void WriteByte (byte value)
  142. {
  143. byte[] buf = new byte[1];
  144. buf[0] = value;
  145. Write (buf, 0, 1);
  146. }
  147. }
  148. }