IntPtrStream.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. using System.Runtime.InteropServices;
  17. namespace System.IO {
  18. internal class IntPtrStream : Stream {
  19. unsafe byte *base_address;
  20. int size;
  21. int position;
  22. bool closed;
  23. public event EventHandler Closed;
  24. public IntPtrStream (IntPtr base_address, int size)
  25. {
  26. unsafe {
  27. this.base_address = (byte*)((void *)base_address);
  28. }
  29. this.size = size;
  30. position = 0;
  31. }
  32. public override bool CanRead {
  33. get {
  34. return true;
  35. }
  36. }
  37. public override bool CanSeek {
  38. get {
  39. return true;
  40. }
  41. }
  42. public override bool CanWrite {
  43. get {
  44. return false;
  45. }
  46. }
  47. public override long Position {
  48. get {
  49. return position;
  50. }
  51. set {
  52. if (position < 0)
  53. throw new ArgumentOutOfRangeException ("Position", "Can not be negative");
  54. if (position > size)
  55. throw new ArgumentOutOfRangeException ("Position", "Pointer falls out of range");
  56. position = (int) value;
  57. }
  58. }
  59. public override long Length {
  60. get {
  61. return size;
  62. }
  63. }
  64. public override int Read (byte [] buffer, int offset, int count)
  65. {
  66. if (buffer == null)
  67. throw new ArgumentNullException ("buffer");
  68. if (offset < 0 || count < 0)
  69. throw new ArgumentOutOfRangeException ("offset or count less than zero.");
  70. if (buffer.Length - offset < count )
  71. throw new ArgumentException ("offset+count",
  72. "The size of the buffer is less than offset + count.");
  73. if (closed)
  74. throw new ObjectDisposedException ("Stream has been closed");
  75. if (position >= size || count == 0)
  76. return 0;
  77. if (position > size - count)
  78. count = size - position;
  79. unsafe {
  80. Marshal.Copy ((IntPtr) (base_address + position), buffer, offset, count);
  81. }
  82. position += count;
  83. return count;
  84. }
  85. public override int ReadByte ()
  86. {
  87. if (position >= size)
  88. return -1;
  89. if (closed)
  90. throw new ObjectDisposedException ("Stream has been closed");
  91. unsafe {
  92. return base_address [position++];
  93. }
  94. }
  95. public override long Seek (long offset, SeekOrigin loc)
  96. {
  97. // It's funny that they don't throw this exception for < Int32.MinValue
  98. if (offset > (long) Int32.MaxValue)
  99. throw new ArgumentOutOfRangeException ("Offset out of range. " + offset);
  100. if (closed)
  101. throw new ObjectDisposedException ("Stream has been closed");
  102. int ref_point;
  103. switch (loc) {
  104. case SeekOrigin.Begin:
  105. if (offset < 0)
  106. throw new IOException ("Attempted to seek before start of MemoryStream.");
  107. ref_point = 0;
  108. break;
  109. case SeekOrigin.Current:
  110. ref_point = position;
  111. break;
  112. case SeekOrigin.End:
  113. ref_point = size;
  114. break;
  115. default:
  116. throw new ArgumentException ("loc", "Invalid SeekOrigin");
  117. }
  118. checked {
  119. try {
  120. ref_point += (int) offset;
  121. } catch {
  122. throw new ArgumentOutOfRangeException ("Too large seek destination");
  123. }
  124. if (ref_point < 0)
  125. throw new IOException ("Attempted to seek before start of MemoryStream.");
  126. }
  127. position = ref_point;
  128. return position;
  129. }
  130. public override void SetLength (long value)
  131. {
  132. throw new NotSupportedException ("This stream can not change its size");
  133. }
  134. public override void Write (byte [] buffer, int offset, int count)
  135. {
  136. throw new NotSupportedException ("This stream can not change its size");
  137. }
  138. public override void WriteByte (byte value)
  139. {
  140. throw new NotSupportedException ("This stream can not change its size");
  141. }
  142. public override void Flush ()
  143. {
  144. }
  145. public override void Close ()
  146. {
  147. closed = true;
  148. if (Closed != null)
  149. Closed (this, null);
  150. }
  151. }
  152. }