UnmanagedMemoryStream.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. //
  2. // System.IO.UnmanagedMemoryStream.cs
  3. //
  4. // Copyright (C) 2006 Sridhar Kulkarni, All Rights Reserved
  5. //
  6. // Authors:
  7. // Sridhar Kulkarni ([email protected])
  8. // Gert Driesen ([email protected])
  9. // Sebastien Pouliot <[email protected]>
  10. //
  11. // Copyright (C) 2005-2006, 2009 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.IO;
  34. using System.Runtime.InteropServices;
  35. namespace System.IO
  36. {
  37. public class UnmanagedMemoryStream : Stream
  38. {
  39. long length;
  40. bool closed;
  41. long capacity;
  42. FileAccess fileaccess;
  43. IntPtr initial_pointer;
  44. long initial_position;
  45. long current_position;
  46. internal event EventHandler Closed;
  47. #region Constructor
  48. protected UnmanagedMemoryStream()
  49. {
  50. closed = true;
  51. }
  52. [CLSCompliantAttribute(false)]
  53. public unsafe UnmanagedMemoryStream (byte *pointer, long length)
  54. {
  55. Initialize (pointer, length, length, FileAccess.Read);
  56. }
  57. [CLSCompliantAttribute(false)]
  58. public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
  59. {
  60. Initialize (pointer, length, capacity, access);
  61. }
  62. #endregion
  63. #region Properties
  64. public override bool CanRead {
  65. get {
  66. return (!closed && (fileaccess != FileAccess.Write));
  67. }
  68. }
  69. public override bool CanSeek {
  70. get {
  71. return !closed;
  72. }
  73. }
  74. public override bool CanWrite {
  75. get {
  76. return (!closed && (fileaccess != FileAccess.Read));
  77. }
  78. }
  79. public long Capacity {
  80. get {
  81. if (closed)
  82. throw new ObjectDisposedException("The stream is closed");
  83. else
  84. return (capacity);
  85. }
  86. }
  87. public override long Length {
  88. get {
  89. if (closed)
  90. throw new ObjectDisposedException("The stream is closed");
  91. else
  92. return (length);
  93. }
  94. }
  95. public override long Position {
  96. get {
  97. if (closed)
  98. throw new ObjectDisposedException("The stream is closed");
  99. return (current_position);
  100. }
  101. set {
  102. if (closed)
  103. throw new ObjectDisposedException("The stream is closed");
  104. if (value < 0)
  105. throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
  106. if (value > (long)Int32.MaxValue)
  107. throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
  108. current_position = value;
  109. }
  110. }
  111. [CLSCompliantAttribute (false)]
  112. public unsafe byte* PositionPointer {
  113. get {
  114. if (closed)
  115. throw new ObjectDisposedException("The stream is closed");
  116. if (current_position >= length)
  117. throw new IndexOutOfRangeException ("value");
  118. return (byte *) initial_pointer + current_position;
  119. }
  120. set {
  121. if (closed)
  122. throw new ObjectDisposedException("The stream is closed");
  123. if (value < (byte *)initial_pointer)
  124. throw new IOException ("Address is below the inital address");
  125. Position = value - (byte*) initial_pointer;
  126. }
  127. }
  128. #endregion
  129. #region Methods
  130. public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
  131. {
  132. if (closed)
  133. throw new ObjectDisposedException("The stream is closed");
  134. if (buffer == null)
  135. throw new ArgumentNullException("buffer");
  136. if (offset < 0)
  137. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  138. if (count < 0)
  139. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  140. if ((buffer.Length - offset) < count)
  141. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  142. if (fileaccess == FileAccess.Write)
  143. throw new NotSupportedException("Stream does not support reading");
  144. else {
  145. if (current_position >= length)
  146. return (0);
  147. else {
  148. int progress = current_position + count < length ? count : (int) (length - current_position);
  149. for (int i = 0; i < progress; i++)
  150. buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
  151. return progress;
  152. }
  153. }
  154. }
  155. public override int ReadByte ()
  156. {
  157. if (closed)
  158. throw new ObjectDisposedException("The stream is closed");
  159. if (fileaccess== FileAccess.Write)
  160. throw new NotSupportedException("Stream does not support reading");
  161. else {
  162. if (current_position >= length)
  163. return (-1);
  164. return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
  165. }
  166. }
  167. public override long Seek (long offset, SeekOrigin loc)
  168. {
  169. if (closed)
  170. throw new ObjectDisposedException("The stream is closed");
  171. long refpoint;
  172. switch(loc) {
  173. case SeekOrigin.Begin:
  174. if (offset < 0)
  175. throw new IOException("An attempt was made to seek before the beginning of the stream");
  176. refpoint = initial_position;
  177. break;
  178. case SeekOrigin.Current:
  179. refpoint = current_position;
  180. break;
  181. case SeekOrigin.End:
  182. refpoint = length;
  183. break;
  184. default:
  185. throw new ArgumentException("Invalid SeekOrigin option");
  186. }
  187. refpoint += offset;
  188. if (refpoint < initial_position)
  189. throw new IOException("An attempt was made to seek before the beginning of the stream");
  190. current_position = refpoint;
  191. return(current_position);
  192. }
  193. public override void SetLength (long value)
  194. {
  195. if (closed)
  196. throw new ObjectDisposedException("The stream is closed");
  197. if (value < 0)
  198. throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
  199. if (value > capacity)
  200. throw new IOException ("Unable to expand length of this stream beyond its capacity.");
  201. if (fileaccess == FileAccess.Read)
  202. throw new NotSupportedException ("Stream does not support writing.");
  203. length = value;
  204. if (length < current_position)
  205. current_position = length;
  206. }
  207. public override void Flush ()
  208. {
  209. if (closed)
  210. throw new ObjectDisposedException("The stream is closed");
  211. //This method performs no action for this class
  212. //but is included as part of the Stream base class
  213. }
  214. protected override void Dispose (bool disposing)
  215. {
  216. if (closed)
  217. return;
  218. closed = true;
  219. if (Closed != null)
  220. Closed (this, null);
  221. }
  222. public override void Write (byte[] buffer, int offset, int count)
  223. {
  224. if (closed)
  225. throw new ObjectDisposedException("The stream is closed");
  226. if (buffer == null)
  227. throw new ArgumentNullException("The buffer parameter is a null reference");
  228. if (offset < 0)
  229. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  230. if (count < 0)
  231. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  232. if ((buffer.Length - offset) < count)
  233. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  234. if (current_position > capacity - count)
  235. throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
  236. if (fileaccess == FileAccess.Read)
  237. throw new NotSupportedException ("Stream does not support writing.");
  238. else {
  239. unsafe {
  240. // use Marshal.WriteByte since that allow us to start writing
  241. // from the current position
  242. for (int i = 0; i < count; i++)
  243. Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
  244. if (current_position > length)
  245. length = current_position;
  246. }
  247. }
  248. }
  249. public override void WriteByte (byte value)
  250. {
  251. if (closed)
  252. throw new ObjectDisposedException("The stream is closed");
  253. if (current_position == capacity)
  254. throw new NotSupportedException("The current position is at the end of the capacity of the stream");
  255. if (fileaccess == FileAccess.Read)
  256. throw new NotSupportedException("Stream does not support writing.");
  257. else {
  258. unsafe {
  259. Marshal.WriteByte(initial_pointer, (int)current_position, value);
  260. current_position++;
  261. if (current_position > length)
  262. length = current_position;
  263. }
  264. }
  265. }
  266. [CLSCompliant (false)]
  267. protected unsafe void Initialize (byte* pointer, long length,
  268. long capacity,
  269. FileAccess access)
  270. {
  271. if (pointer == null)
  272. throw new ArgumentNullException("pointer");
  273. if (length < 0)
  274. throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
  275. if (capacity < 0)
  276. throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
  277. if (length > capacity)
  278. throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
  279. if ((access < FileAccess.Read) || (access > FileAccess.ReadWrite))
  280. throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
  281. fileaccess = access;
  282. this.length = length;
  283. this.capacity = capacity;
  284. initial_position = 0;
  285. current_position = initial_position;
  286. initial_pointer = new IntPtr ((void*)pointer);
  287. closed = false;
  288. }
  289. #endregion
  290. }
  291. }