UnmanagedMemoryStream.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. [CLSCompliantAttribute(false)]
  38. public class UnmanagedMemoryStream : Stream
  39. {
  40. long length;
  41. bool closed;
  42. long capacity;
  43. FileAccess fileaccess;
  44. IntPtr initial_pointer;
  45. long initial_position;
  46. long current_position;
  47. internal event EventHandler Closed;
  48. #region Constructor
  49. protected UnmanagedMemoryStream()
  50. {
  51. closed = true;
  52. }
  53. public unsafe UnmanagedMemoryStream (byte *pointer, long length)
  54. {
  55. Initialize (pointer, length, length, FileAccess.Read);
  56. }
  57. public unsafe UnmanagedMemoryStream (byte *pointer, long length, long capacity, FileAccess access)
  58. {
  59. Initialize (pointer, length, capacity, access);
  60. }
  61. #endregion
  62. #region Properties
  63. public override bool CanRead {
  64. get {
  65. return (!closed && (fileaccess != FileAccess.Write));
  66. }
  67. }
  68. public override bool CanSeek {
  69. get {
  70. return !closed;
  71. }
  72. }
  73. public override bool CanWrite {
  74. get {
  75. return (!closed && (fileaccess != FileAccess.Read));
  76. }
  77. }
  78. public long Capacity {
  79. get {
  80. if (closed)
  81. throw new ObjectDisposedException("The stream is closed");
  82. else
  83. return (capacity);
  84. }
  85. }
  86. public override long Length {
  87. get {
  88. if (closed)
  89. throw new ObjectDisposedException("The stream is closed");
  90. else
  91. return (length);
  92. }
  93. }
  94. public override long Position {
  95. get {
  96. if (closed)
  97. throw new ObjectDisposedException("The stream is closed");
  98. return (current_position);
  99. }
  100. set {
  101. if (closed)
  102. throw new ObjectDisposedException("The stream is closed");
  103. if (value < 0)
  104. throw new ArgumentOutOfRangeException("value", "Non-negative number required.");
  105. if (value > (long)Int32.MaxValue)
  106. throw new ArgumentOutOfRangeException("value", "The position is larger than Int32.MaxValue.");
  107. current_position = value;
  108. }
  109. }
  110. #if NET_2_1
  111. [CLSCompliantAttribute(false)]
  112. #endif
  113. public unsafe byte* PositionPointer {
  114. get {
  115. if (closed)
  116. throw new ObjectDisposedException("The stream is closed");
  117. if (current_position >= length)
  118. throw new IndexOutOfRangeException ("value");
  119. return (byte *) initial_pointer + current_position;
  120. }
  121. set {
  122. if (closed)
  123. throw new ObjectDisposedException("The stream is closed");
  124. if (value < (byte *)initial_pointer)
  125. throw new IOException ("Address is below the inital address");
  126. Position = value - (byte*) initial_pointer;
  127. }
  128. }
  129. #endregion
  130. #region Methods
  131. public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
  132. {
  133. if (closed)
  134. throw new ObjectDisposedException("The stream is closed");
  135. if (buffer == null)
  136. throw new ArgumentNullException("buffer");
  137. if (offset < 0)
  138. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  139. if (count < 0)
  140. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  141. if ((buffer.Length - offset) < count)
  142. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  143. if (fileaccess == FileAccess.Write)
  144. throw new NotSupportedException("Stream does not support reading");
  145. else {
  146. if (current_position >= length)
  147. return (0);
  148. else {
  149. int progress = current_position + count < length ? count : (int) (length - current_position);
  150. for (int i = 0; i < progress; i++)
  151. buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
  152. return progress;
  153. }
  154. }
  155. }
  156. public override int ReadByte ()
  157. {
  158. if (closed)
  159. throw new ObjectDisposedException("The stream is closed");
  160. if (fileaccess== FileAccess.Write)
  161. throw new NotSupportedException("Stream does not support reading");
  162. else {
  163. if (current_position >= length)
  164. return (-1);
  165. return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
  166. }
  167. }
  168. public override long Seek (long offset, SeekOrigin loc)
  169. {
  170. if (closed)
  171. throw new ObjectDisposedException("The stream is closed");
  172. long refpoint;
  173. switch(loc) {
  174. case SeekOrigin.Begin:
  175. if (offset < 0)
  176. throw new IOException("An attempt was made to seek before the beginning of the stream");
  177. refpoint = initial_position;
  178. break;
  179. case SeekOrigin.Current:
  180. refpoint = current_position;
  181. break;
  182. case SeekOrigin.End:
  183. refpoint = length;
  184. break;
  185. default:
  186. throw new ArgumentException("Invalid SeekOrigin option");
  187. }
  188. refpoint += offset;
  189. if (refpoint < initial_position)
  190. throw new IOException("An attempt was made to seek before the beginning of the stream");
  191. current_position = refpoint;
  192. return(current_position);
  193. }
  194. public override void SetLength (long value)
  195. {
  196. if (closed)
  197. throw new ObjectDisposedException("The stream is closed");
  198. if (value < 0)
  199. throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
  200. if (value > capacity)
  201. throw new IOException ("Unable to expand length of this stream beyond its capacity.");
  202. if (fileaccess == FileAccess.Read)
  203. throw new NotSupportedException ("Stream does not support writing.");
  204. length = value;
  205. if (length < current_position)
  206. current_position = length;
  207. }
  208. public override void Flush ()
  209. {
  210. if (closed)
  211. throw new ObjectDisposedException("The stream is closed");
  212. //This method performs no action for this class
  213. //but is included as part of the Stream base class
  214. }
  215. protected override void Dispose (bool disposing)
  216. {
  217. if (closed)
  218. return;
  219. closed = true;
  220. if (Closed != null)
  221. Closed (this, null);
  222. }
  223. public override void Write (byte[] buffer, int offset, int count)
  224. {
  225. if (closed)
  226. throw new ObjectDisposedException("The stream is closed");
  227. if (buffer == null)
  228. throw new ArgumentNullException("The buffer parameter is a null reference");
  229. if (offset < 0)
  230. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  231. if (count < 0)
  232. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  233. if ((buffer.Length - offset) < count)
  234. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  235. if (current_position > capacity - count)
  236. throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
  237. if (fileaccess == FileAccess.Read)
  238. throw new NotSupportedException ("Stream does not support writing.");
  239. else {
  240. unsafe {
  241. // use Marshal.WriteByte since that allow us to start writing
  242. // from the current position
  243. for (int i = 0; i < count; i++)
  244. Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
  245. if (current_position > length)
  246. length = current_position;
  247. }
  248. }
  249. }
  250. public override void WriteByte (byte value)
  251. {
  252. if (closed)
  253. throw new ObjectDisposedException("The stream is closed");
  254. if (current_position == capacity)
  255. throw new NotSupportedException("The current position is at the end of the capacity of the stream");
  256. if (fileaccess == FileAccess.Read)
  257. throw new NotSupportedException("Stream does not support writing.");
  258. else {
  259. unsafe {
  260. Marshal.WriteByte(initial_pointer, (int)current_position, value);
  261. current_position++;
  262. if (current_position > length)
  263. length = current_position;
  264. }
  265. }
  266. }
  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. }