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. #if NET_2_1
  112. [CLSCompliantAttribute(false)]
  113. #endif
  114. public unsafe byte* PositionPointer {
  115. get {
  116. if (closed)
  117. throw new ObjectDisposedException("The stream is closed");
  118. if (current_position >= length)
  119. throw new IndexOutOfRangeException ("value");
  120. return (byte *) initial_pointer + current_position;
  121. }
  122. set {
  123. if (closed)
  124. throw new ObjectDisposedException("The stream is closed");
  125. if (value < (byte *)initial_pointer)
  126. throw new IOException ("Address is below the inital address");
  127. Position = value - (byte*) initial_pointer;
  128. }
  129. }
  130. #endregion
  131. #region Methods
  132. public override int Read ([InAttribute] [OutAttribute] byte[] buffer, int offset, int count)
  133. {
  134. if (closed)
  135. throw new ObjectDisposedException("The stream is closed");
  136. if (buffer == null)
  137. throw new ArgumentNullException("buffer");
  138. if (offset < 0)
  139. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  140. if (count < 0)
  141. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  142. if ((buffer.Length - offset) < count)
  143. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  144. if (fileaccess == FileAccess.Write)
  145. throw new NotSupportedException("Stream does not support reading");
  146. else {
  147. if (current_position >= length)
  148. return (0);
  149. else {
  150. int progress = current_position + count < length ? count : (int) (length - current_position);
  151. for (int i = 0; i < progress; i++)
  152. buffer [offset + i] = Marshal.ReadByte (initial_pointer, (int) current_position++);
  153. return progress;
  154. }
  155. }
  156. }
  157. public override int ReadByte ()
  158. {
  159. if (closed)
  160. throw new ObjectDisposedException("The stream is closed");
  161. if (fileaccess== FileAccess.Write)
  162. throw new NotSupportedException("Stream does not support reading");
  163. else {
  164. if (current_position >= length)
  165. return (-1);
  166. return (int) Marshal.ReadByte(initial_pointer, (int) current_position++);
  167. }
  168. }
  169. public override long Seek (long offset, SeekOrigin loc)
  170. {
  171. if (closed)
  172. throw new ObjectDisposedException("The stream is closed");
  173. long refpoint;
  174. switch(loc) {
  175. case SeekOrigin.Begin:
  176. if (offset < 0)
  177. throw new IOException("An attempt was made to seek before the beginning of the stream");
  178. refpoint = initial_position;
  179. break;
  180. case SeekOrigin.Current:
  181. refpoint = current_position;
  182. break;
  183. case SeekOrigin.End:
  184. refpoint = length;
  185. break;
  186. default:
  187. throw new ArgumentException("Invalid SeekOrigin option");
  188. }
  189. refpoint += offset;
  190. if (refpoint < initial_position)
  191. throw new IOException("An attempt was made to seek before the beginning of the stream");
  192. current_position = refpoint;
  193. return(current_position);
  194. }
  195. public override void SetLength (long value)
  196. {
  197. if (closed)
  198. throw new ObjectDisposedException("The stream is closed");
  199. if (value < 0)
  200. throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
  201. if (value > capacity)
  202. throw new IOException ("Unable to expand length of this stream beyond its capacity.");
  203. if (fileaccess == FileAccess.Read)
  204. throw new NotSupportedException ("Stream does not support writing.");
  205. length = value;
  206. if (length < current_position)
  207. current_position = length;
  208. }
  209. public override void Flush ()
  210. {
  211. if (closed)
  212. throw new ObjectDisposedException("The stream is closed");
  213. //This method performs no action for this class
  214. //but is included as part of the Stream base class
  215. }
  216. protected override void Dispose (bool disposing)
  217. {
  218. if (closed)
  219. return;
  220. closed = true;
  221. if (Closed != null)
  222. Closed (this, null);
  223. }
  224. public override void Write (byte[] buffer, int offset, int count)
  225. {
  226. if (closed)
  227. throw new ObjectDisposedException("The stream is closed");
  228. if (buffer == null)
  229. throw new ArgumentNullException("The buffer parameter is a null reference");
  230. if (offset < 0)
  231. throw new ArgumentOutOfRangeException("offset", "Non-negative number required.");
  232. if (count < 0)
  233. throw new ArgumentOutOfRangeException("count", "Non-negative number required.");
  234. if ((buffer.Length - offset) < count)
  235. throw new ArgumentException("The length of the buffer array minus the offset parameter is less than the count parameter");
  236. if (current_position > capacity - count)
  237. throw new NotSupportedException ("Unable to expand length of this stream beyond its capacity.");
  238. if (fileaccess == FileAccess.Read)
  239. throw new NotSupportedException ("Stream does not support writing.");
  240. else {
  241. unsafe {
  242. // use Marshal.WriteByte since that allow us to start writing
  243. // from the current position
  244. for (int i = 0; i < count; i++)
  245. Marshal.WriteByte (initial_pointer, (int) current_position++, buffer [offset + i]);
  246. if (current_position > length)
  247. length = current_position;
  248. }
  249. }
  250. }
  251. public override void WriteByte (byte value)
  252. {
  253. if (closed)
  254. throw new ObjectDisposedException("The stream is closed");
  255. if (current_position == capacity)
  256. throw new NotSupportedException("The current position is at the end of the capacity of the stream");
  257. if (fileaccess == FileAccess.Read)
  258. throw new NotSupportedException("Stream does not support writing.");
  259. else {
  260. unsafe {
  261. Marshal.WriteByte(initial_pointer, (int)current_position, value);
  262. current_position++;
  263. if (current_position > length)
  264. length = current_position;
  265. }
  266. }
  267. }
  268. protected unsafe void Initialize (byte* pointer, long length,
  269. long capacity,
  270. FileAccess access)
  271. {
  272. if (pointer == null)
  273. throw new ArgumentNullException("pointer");
  274. if (length < 0)
  275. throw new ArgumentOutOfRangeException("length", "Non-negative number required.");
  276. if (capacity < 0)
  277. throw new ArgumentOutOfRangeException("capacity", "Non-negative number required.");
  278. if (length > capacity)
  279. throw new ArgumentOutOfRangeException("length", "The length cannot be greater than the capacity.");
  280. if ((access < FileAccess.Read) || (access > FileAccess.ReadWrite))
  281. throw new ArgumentOutOfRangeException ("access", "Enum value was out of legal range.");
  282. fileaccess = access;
  283. this.length = length;
  284. this.capacity = capacity;
  285. initial_position = 0;
  286. current_position = initial_position;
  287. initial_pointer = new IntPtr ((void*)pointer);
  288. closed = false;
  289. }
  290. #endregion
  291. }
  292. }