IntPtrStream.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. public override bool CanRead {
  55. get {
  56. return true;
  57. }
  58. }
  59. public override bool CanSeek {
  60. get {
  61. return true;
  62. }
  63. }
  64. public override bool CanWrite {
  65. get {
  66. return false;
  67. }
  68. }
  69. public override long Position {
  70. get {
  71. return position;
  72. }
  73. set {
  74. if (position < 0)
  75. throw new ArgumentOutOfRangeException ("Position", "Can not be negative");
  76. if (position > size)
  77. throw new ArgumentOutOfRangeException ("Position", "Pointer falls out of range");
  78. position = (int) value;
  79. }
  80. }
  81. public override long Length {
  82. get {
  83. return size;
  84. }
  85. }
  86. public override int Read (byte [] buffer, int offset, int count)
  87. {
  88. if (buffer == null)
  89. throw new ArgumentNullException ("buffer");
  90. if (offset < 0 || count < 0)
  91. throw new ArgumentOutOfRangeException ("offset or count less than zero.");
  92. if (buffer.Length - offset < count )
  93. throw new ArgumentException ("offset+count",
  94. "The size of the buffer is less than offset + count.");
  95. if (closed)
  96. throw new ObjectDisposedException ("Stream has been closed");
  97. if (position >= size || count == 0)
  98. return 0;
  99. if (position > size - count)
  100. count = size - position;
  101. unsafe {
  102. Marshal.Copy ((IntPtr) (base_address + position), buffer, offset, count);
  103. }
  104. position += count;
  105. return count;
  106. }
  107. public override int ReadByte ()
  108. {
  109. if (position >= size)
  110. return -1;
  111. if (closed)
  112. throw new ObjectDisposedException ("Stream has been closed");
  113. unsafe {
  114. return base_address [position++];
  115. }
  116. }
  117. public override long Seek (long offset, SeekOrigin loc)
  118. {
  119. // It's funny that they don't throw this exception for < Int32.MinValue
  120. if (offset > (long) Int32.MaxValue)
  121. throw new ArgumentOutOfRangeException ("Offset out of range. " + offset);
  122. if (closed)
  123. throw new ObjectDisposedException ("Stream has been closed");
  124. int ref_point;
  125. switch (loc) {
  126. case SeekOrigin.Begin:
  127. if (offset < 0)
  128. throw new IOException ("Attempted to seek before start of MemoryStream.");
  129. ref_point = 0;
  130. break;
  131. case SeekOrigin.Current:
  132. ref_point = position;
  133. break;
  134. case SeekOrigin.End:
  135. ref_point = size;
  136. break;
  137. default:
  138. throw new ArgumentException ("loc", "Invalid SeekOrigin");
  139. }
  140. checked {
  141. try {
  142. ref_point += (int) offset;
  143. } catch {
  144. throw new ArgumentOutOfRangeException ("Too large seek destination");
  145. }
  146. if (ref_point < 0)
  147. throw new IOException ("Attempted to seek before start of MemoryStream.");
  148. }
  149. position = ref_point;
  150. return position;
  151. }
  152. public override void SetLength (long value)
  153. {
  154. throw new NotSupportedException ("This stream can not change its size");
  155. }
  156. public override void Write (byte [] buffer, int offset, int count)
  157. {
  158. throw new NotSupportedException ("This stream can not change its size");
  159. }
  160. public override void WriteByte (byte value)
  161. {
  162. throw new NotSupportedException ("This stream can not change its size");
  163. }
  164. public override void Flush ()
  165. {
  166. }
  167. public override void Close ()
  168. {
  169. closed = true;
  170. if (Closed != null)
  171. Closed (this, null);
  172. }
  173. }
  174. }