UnmanagedMemoryStream.cs 9.6 KB

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