IntPtrStream.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.IO.IntPtrStream: A stream that is backed up by unmanaged memory
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Based on the code for MemoryStream.cs:
  8. //
  9. // Authors: Marcin Szczepanski ([email protected])
  10. // Patrik Torstensson
  11. // Gonzalo Paniagua Javier ([email protected])
  12. //
  13. // (c) 2001,2002 Marcin Szczepanski, Patrik Torstensson
  14. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  15. //
  16. //
  17. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  18. //
  19. // Permission is hereby granted, free of charge, to any person obtaining
  20. // a copy of this software and associated documentation files (the
  21. // "Software"), to deal in the Software without restriction, including
  22. // without limitation the rights to use, copy, modify, merge, publish,
  23. // distribute, sublicense, and/or sell copies of the Software, and to
  24. // permit persons to whom the Software is furnished to do so, subject to
  25. // the following conditions:
  26. //
  27. // The above copyright notice and this permission notice shall be
  28. // included in all copies or substantial portions of the Software.
  29. //
  30. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. //
  38. using System.Runtime.InteropServices;
  39. namespace System.IO {
  40. internal class IntPtrStream : Stream {
  41. unsafe byte *base_address;
  42. int size;
  43. int position;
  44. bool closed;
  45. public event EventHandler Closed;
  46. public IntPtrStream (IntPtr base_address, int size)
  47. {
  48. unsafe {
  49. this.base_address = (byte*)((void *)base_address);
  50. }
  51. this.size = size;
  52. position = 0;
  53. }
  54. internal IntPtr BaseAddress {
  55. get {
  56. unsafe {
  57. return new IntPtr ((void*) base_address);
  58. }
  59. }
  60. }
  61. public override bool CanRead {
  62. get {
  63. return true;
  64. }
  65. }
  66. public override bool CanSeek {
  67. get {
  68. return true;
  69. }
  70. }
  71. public override bool CanWrite {
  72. get {
  73. return false;
  74. }
  75. }
  76. public override long Position {
  77. get {
  78. return position;
  79. }
  80. set {
  81. if (position < 0)
  82. throw new ArgumentOutOfRangeException ("Position", "Can not be negative");
  83. if (position > size)
  84. throw new ArgumentOutOfRangeException ("Position", "Pointer falls out of range");
  85. position = (int) value;
  86. }
  87. }
  88. public override long Length {
  89. get {
  90. return size;
  91. }
  92. }
  93. public override int Read (byte [] buffer, int offset, int count)
  94. {
  95. if (buffer == null)
  96. throw new ArgumentNullException ("buffer");
  97. if (offset < 0 || count < 0)
  98. throw new ArgumentOutOfRangeException ("offset or count less than zero.");
  99. if (buffer.Length - offset < count )
  100. throw new ArgumentException ("offset+count",
  101. "The size of the buffer is less than offset + count.");
  102. if (closed)
  103. throw new ObjectDisposedException ("Stream has been closed");
  104. if (position >= size || count == 0)
  105. return 0;
  106. if (position > size - count)
  107. count = size - position;
  108. unsafe {
  109. Marshal.Copy ((IntPtr) (base_address + position), buffer, offset, count);
  110. }
  111. position += count;
  112. return count;
  113. }
  114. public override int ReadByte ()
  115. {
  116. if (position >= size)
  117. return -1;
  118. if (closed)
  119. throw new ObjectDisposedException ("Stream has been closed");
  120. unsafe {
  121. return base_address [position++];
  122. }
  123. }
  124. public override long Seek (long offset, SeekOrigin loc)
  125. {
  126. // It's funny that they don't throw this exception for < Int32.MinValue
  127. if (offset > (long) Int32.MaxValue)
  128. throw new ArgumentOutOfRangeException ("Offset out of range. " + offset);
  129. if (closed)
  130. throw new ObjectDisposedException ("Stream has been closed");
  131. int ref_point;
  132. switch (loc) {
  133. case SeekOrigin.Begin:
  134. if (offset < 0)
  135. throw new IOException ("Attempted to seek before start of MemoryStream.");
  136. ref_point = 0;
  137. break;
  138. case SeekOrigin.Current:
  139. ref_point = position;
  140. break;
  141. case SeekOrigin.End:
  142. ref_point = size;
  143. break;
  144. default:
  145. throw new ArgumentException ("loc", "Invalid SeekOrigin");
  146. }
  147. checked {
  148. try {
  149. ref_point += (int) offset;
  150. } catch {
  151. throw new ArgumentOutOfRangeException ("Too large seek destination");
  152. }
  153. if (ref_point < 0)
  154. throw new IOException ("Attempted to seek before start of MemoryStream.");
  155. }
  156. position = ref_point;
  157. return position;
  158. }
  159. public override void SetLength (long value)
  160. {
  161. throw new NotSupportedException ("This stream can not change its size");
  162. }
  163. public override void Write (byte [] buffer, int offset, int count)
  164. {
  165. throw new NotSupportedException ("This stream can not change its size");
  166. }
  167. public override void WriteByte (byte value)
  168. {
  169. throw new NotSupportedException ("This stream can not change its size");
  170. }
  171. public override void Flush ()
  172. {
  173. }
  174. public override void Close ()
  175. {
  176. closed = true;
  177. if (Closed != null)
  178. Closed (this, null);
  179. }
  180. }
  181. }