mscorlib(4.0.0.0) API with additions
UnmanagedMemoryStream.cs
3 using System.Security;
5 using System.Threading;
7 
8 namespace System.IO
9 {
12  {
13  private const long UnmanagedMemStreamMaxLength = long.MaxValue;
14 
15  [SecurityCritical]
16  private SafeBuffer _buffer;
17 
18  [SecurityCritical]
19  private unsafe byte* _mem;
20 
21  private long _length;
22 
23  private long _capacity;
24 
25  private long _position;
26 
27  private long _offset;
28 
29  private FileAccess _access;
30 
31  internal bool _isOpen;
32 
33  [NonSerialized]
34  private Task<int> _lastReadTask;
35 
39  public override bool CanRead
40  {
41  get
42  {
43  if (_isOpen)
44  {
45  return (_access & FileAccess.Read) != (FileAccess)0;
46  }
47  return false;
48  }
49  }
50 
54  public override bool CanSeek => _isOpen;
55 
59  public override bool CanWrite
60  {
61  get
62  {
63  if (_isOpen)
64  {
65  return (_access & FileAccess.Write) != (FileAccess)0;
66  }
67  return false;
68  }
69  }
70 
74  public override long Length
75  {
76  get
77  {
78  if (!_isOpen)
79  {
80  __Error.StreamIsClosed();
81  }
82  return Interlocked.Read(ref _length);
83  }
84  }
85 
89  public long Capacity
90  {
91  get
92  {
93  if (!_isOpen)
94  {
95  __Error.StreamIsClosed();
96  }
97  return _capacity;
98  }
99  }
100 
105  public override long Position
106  {
107  get
108  {
109  if (!CanSeek)
110  {
111  __Error.StreamIsClosed();
112  }
113  return Interlocked.Read(ref _position);
114  }
115  [SecuritySafeCritical]
116  set
117  {
118  if (value < 0)
119  {
120  throw new ArgumentOutOfRangeException("value", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
121  }
122  if (!CanSeek)
123  {
124  __Error.StreamIsClosed();
125  }
126  Interlocked.Exchange(ref _position, value);
127  }
128  }
129 
136  [CLSCompliant(false)]
137  public unsafe byte* PositionPointer
138  {
139  [SecurityCritical]
140  get
141  {
142  if (_buffer != null)
143  {
144  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer"));
145  }
146  long num = Interlocked.Read(ref _position);
147  if (num > _capacity)
148  {
149  throw new IndexOutOfRangeException(Environment.GetResourceString("IndexOutOfRange_UMSPosition"));
150  }
151  byte* result = _mem + num;
152  if (!_isOpen)
153  {
154  __Error.StreamIsClosed();
155  }
156  return result;
157  }
158  [SecurityCritical]
159  set
160  {
161  if (_buffer != null)
162  {
163  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer"));
164  }
165  if (!_isOpen)
166  {
167  __Error.StreamIsClosed();
168  }
169  if (new IntPtr(value - _mem).ToInt64() > long.MaxValue)
170  {
171  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamLength"));
172  }
173  if (value < _mem)
174  {
175  throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin"));
176  }
177  Interlocked.Exchange(ref _position, value - _mem);
178  }
179  }
180 
181  internal unsafe byte* Pointer
182  {
183  [SecurityCritical]
184  get
185  {
186  if (_buffer != null)
187  {
188  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer"));
189  }
190  return _mem;
191  }
192  }
193 
196  [SecuritySafeCritical]
197  protected unsafe UnmanagedMemoryStream()
198  {
199  _mem = null;
200  _isOpen = false;
201  }
202 
207  [SecuritySafeCritical]
208  public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length)
209  {
210  Initialize(buffer, offset, length, FileAccess.Read, skipSecurityCheck: false);
211  }
212 
218  [SecuritySafeCritical]
219  public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access)
220  {
221  Initialize(buffer, offset, length, access, skipSecurityCheck: false);
222  }
223 
224  [SecurityCritical]
225  internal UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access, bool skipSecurityCheck)
226  {
227  Initialize(buffer, offset, length, access, skipSecurityCheck);
228  }
229 
235  [SecuritySafeCritical]
236  protected void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access)
237  {
238  Initialize(buffer, offset, length, access, skipSecurityCheck: false);
239  }
240 
241  [SecurityCritical]
242  internal unsafe void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access, bool skipSecurityCheck)
243  {
244  if (buffer == null)
245  {
246  throw new ArgumentNullException("buffer");
247  }
248  if (offset < 0)
249  {
250  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
251  }
252  if (length < 0)
253  {
254  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
255  }
256  if (buffer.ByteLength < (ulong)(offset + length))
257  {
258  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSafeBufferOffLen"));
259  }
260  if (access < FileAccess.Read || access > FileAccess.ReadWrite)
261  {
262  throw new ArgumentOutOfRangeException("access");
263  }
264  if (_isOpen)
265  {
266  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice"));
267  }
268  if (!skipSecurityCheck)
269  {
270  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
271  }
272  byte* pointer = null;
274  try
275  {
276  buffer.AcquirePointer(ref pointer);
277  if (pointer + offset + length < pointer)
278  {
279  throw new ArgumentException(Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
280  }
281  }
282  finally
283  {
284  if (pointer != null)
285  {
286  buffer.ReleasePointer();
287  }
288  }
289  _offset = offset;
290  _buffer = buffer;
291  _length = length;
292  _capacity = length;
293  _access = access;
294  _isOpen = true;
295  }
296 
303  [SecurityCritical]
304  [CLSCompliant(false)]
305  public unsafe UnmanagedMemoryStream(byte* pointer, long length)
306  {
307  Initialize(pointer, length, length, FileAccess.Read, skipSecurityCheck: false);
308  }
309 
318  [SecurityCritical]
319  [CLSCompliant(false)]
320  public unsafe UnmanagedMemoryStream(byte* pointer, long length, long capacity, FileAccess access)
321  {
322  Initialize(pointer, length, capacity, access, skipSecurityCheck: false);
323  }
324 
325  [SecurityCritical]
326  internal unsafe UnmanagedMemoryStream(byte* pointer, long length, long capacity, FileAccess access, bool skipSecurityCheck)
327  {
328  Initialize(pointer, length, capacity, access, skipSecurityCheck);
329  }
330 
339  [SecurityCritical]
340  [CLSCompliant(false)]
341  protected unsafe void Initialize(byte* pointer, long length, long capacity, FileAccess access)
342  {
343  Initialize(pointer, length, capacity, access, skipSecurityCheck: false);
344  }
345 
346  [SecurityCritical]
347  internal unsafe void Initialize(byte* pointer, long length, long capacity, FileAccess access, bool skipSecurityCheck)
348  {
349  if (pointer == null)
350  {
351  throw new ArgumentNullException("pointer");
352  }
353  if (length < 0 || capacity < 0)
354  {
355  throw new ArgumentOutOfRangeException((length < 0) ? "length" : "capacity", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
356  }
357  if (length > capacity)
358  {
359  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_LengthGreaterThanCapacity"));
360  }
361  if ((ulong)(UIntPtr)(void*)((long)pointer + capacity) < (ulong)pointer)
362  {
363  throw new ArgumentOutOfRangeException("capacity", Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
364  }
365  if (access < FileAccess.Read || access > FileAccess.ReadWrite)
366  {
367  throw new ArgumentOutOfRangeException("access", Environment.GetResourceString("ArgumentOutOfRange_Enum"));
368  }
369  if (_isOpen)
370  {
371  throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CalledTwice"));
372  }
373  if (!skipSecurityCheck)
374  {
375  new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
376  }
377  _mem = pointer;
378  _offset = 0L;
379  _length = length;
380  _capacity = capacity;
381  _access = access;
382  _isOpen = true;
383  }
384 
388  [SecuritySafeCritical]
389  protected unsafe override void Dispose(bool disposing)
390  {
391  _isOpen = false;
392  _mem = null;
393  base.Dispose(disposing);
394  }
395 
398  public override void Flush()
399  {
400  if (!_isOpen)
401  {
402  __Error.StreamIsClosed();
403  }
404  }
405 
409  [ComVisible(false)]
410  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
411  public override Task FlushAsync(CancellationToken cancellationToken)
412  {
413  if (cancellationToken.IsCancellationRequested)
414  {
415  return Task.FromCancellation(cancellationToken);
416  }
417  try
418  {
419  Flush();
420  return Task.CompletedTask;
421  }
422  catch (Exception exception)
423  {
424  return Task.FromException(exception);
425  }
426  }
427 
438  [SecuritySafeCritical]
439  public unsafe override int Read([In] [Out] byte[] buffer, int offset, int count)
440  {
441  if (buffer == null)
442  {
443  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
444  }
445  if (offset < 0)
446  {
447  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
448  }
449  if (count < 0)
450  {
451  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
452  }
453  if (buffer.Length - offset < count)
454  {
455  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
456  }
457  if (!_isOpen)
458  {
459  __Error.StreamIsClosed();
460  }
461  if (!CanRead)
462  {
463  __Error.ReadNotSupported();
464  }
465  long num = Interlocked.Read(ref _position);
466  long num2 = Interlocked.Read(ref _length);
467  long num3 = num2 - num;
468  if (num3 > count)
469  {
470  num3 = count;
471  }
472  if (num3 <= 0)
473  {
474  return 0;
475  }
476  int num4 = (int)num3;
477  if (num4 < 0)
478  {
479  num4 = 0;
480  }
481  if (_buffer != null)
482  {
483  byte* pointer = null;
485  try
486  {
487  _buffer.AcquirePointer(ref pointer);
488  Buffer.Memcpy(buffer, offset, pointer + num + _offset, 0, num4);
489  }
490  finally
491  {
492  if (pointer != null)
493  {
494  _buffer.ReleasePointer();
495  }
496  }
497  }
498  else
499  {
500  Buffer.Memcpy(buffer, offset, _mem + num, 0, num4);
501  }
502  Interlocked.Exchange(ref _position, num + num3);
503  return num4;
504  }
505 
512  [ComVisible(false)]
513  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
514  public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
515  {
516  if (buffer == null)
517  {
518  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
519  }
520  if (offset < 0)
521  {
522  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
523  }
524  if (count < 0)
525  {
526  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
527  }
528  if (buffer.Length - offset < count)
529  {
530  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
531  }
532  if (cancellationToken.IsCancellationRequested)
533  {
534  return Task.FromCancellation<int>(cancellationToken);
535  }
536  try
537  {
538  int num = Read(buffer, offset, count);
539  Task<int> lastReadTask = _lastReadTask;
540  return (lastReadTask != null && lastReadTask.Result == num) ? lastReadTask : (_lastReadTask = Task.FromResult(num));
541  }
542  catch (Exception exception)
543  {
544  return Task.FromException<int>(exception);
545  }
546  }
547 
552  [SecuritySafeCritical]
553  public unsafe override int ReadByte()
554  {
555  if (!_isOpen)
556  {
557  __Error.StreamIsClosed();
558  }
559  if (!CanRead)
560  {
561  __Error.ReadNotSupported();
562  }
563  long num = Interlocked.Read(ref _position);
564  long num2 = Interlocked.Read(ref _length);
565  if (num >= num2)
566  {
567  return -1;
568  }
569  Interlocked.Exchange(ref _position, num + 1);
570  if (_buffer != null)
571  {
572  byte* pointer = null;
574  try
575  {
576  _buffer.AcquirePointer(ref pointer);
577  return (pointer + num)[_offset];
578  }
579  finally
580  {
581  if (pointer != null)
582  {
583  _buffer.ReleasePointer();
584  }
585  }
586  }
587  return _mem[num];
588  }
589 
599  public override long Seek(long offset, SeekOrigin loc)
600  {
601  if (!_isOpen)
602  {
603  __Error.StreamIsClosed();
604  }
605  if (offset > long.MaxValue)
606  {
607  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_UnmanagedMemStreamLength"));
608  }
609  switch (loc)
610  {
611  case SeekOrigin.Begin:
612  if (offset < 0)
613  {
614  throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin"));
615  }
616  Interlocked.Exchange(ref _position, offset);
617  break;
618  case SeekOrigin.Current:
619  {
620  long num2 = Interlocked.Read(ref _position);
621  if (offset + num2 < 0)
622  {
623  throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin"));
624  }
625  Interlocked.Exchange(ref _position, offset + num2);
626  break;
627  }
628  case SeekOrigin.End:
629  {
630  long num = Interlocked.Read(ref _length);
631  if (num + offset < 0)
632  {
633  throw new IOException(Environment.GetResourceString("IO.IO_SeekBeforeBegin"));
634  }
635  Interlocked.Exchange(ref _position, num + offset);
636  break;
637  }
638  default:
639  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidSeekOrigin"));
640  }
641  return Interlocked.Read(ref _position);
642  }
643 
650  [SecuritySafeCritical]
651  public unsafe override void SetLength(long value)
652  {
653  if (value < 0)
654  {
655  throw new ArgumentOutOfRangeException("length", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
656  }
657  if (_buffer != null)
658  {
659  throw new NotSupportedException(Environment.GetResourceString("NotSupported_UmsSafeBuffer"));
660  }
661  if (!_isOpen)
662  {
663  __Error.StreamIsClosed();
664  }
665  if (!CanWrite)
666  {
667  __Error.WriteNotSupported();
668  }
669  if (value > _capacity)
670  {
671  throw new IOException(Environment.GetResourceString("IO.IO_FixedCapacity"));
672  }
673  long num = Interlocked.Read(ref _position);
674  long num2 = Interlocked.Read(ref _length);
675  if (value > num2)
676  {
677  Buffer.ZeroMemory(_mem + num2, value - num2);
678  }
679  Interlocked.Exchange(ref _length, value);
680  if (num > value)
681  {
682  Interlocked.Exchange(ref _position, value);
683  }
684  }
685 
696  [SecuritySafeCritical]
697  public unsafe override void Write(byte[] buffer, int offset, int count)
698  {
699  if (buffer == null)
700  {
701  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
702  }
703  if (offset < 0)
704  {
705  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
706  }
707  if (count < 0)
708  {
709  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
710  }
711  if (buffer.Length - offset < count)
712  {
713  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
714  }
715  if (!_isOpen)
716  {
717  __Error.StreamIsClosed();
718  }
719  if (!CanWrite)
720  {
721  __Error.WriteNotSupported();
722  }
723  long num = Interlocked.Read(ref _position);
724  long num2 = Interlocked.Read(ref _length);
725  long num3 = num + count;
726  if (num3 < 0)
727  {
728  throw new IOException(Environment.GetResourceString("IO.IO_StreamTooLong"));
729  }
730  if (num3 > _capacity)
731  {
732  throw new NotSupportedException(Environment.GetResourceString("IO.IO_FixedCapacity"));
733  }
734  if (_buffer == null)
735  {
736  if (num > num2)
737  {
738  Buffer.ZeroMemory(_mem + num2, num - num2);
739  }
740  if (num3 > num2)
741  {
742  Interlocked.Exchange(ref _length, num3);
743  }
744  }
745  if (_buffer != null)
746  {
747  long num4 = _capacity - num;
748  if (num4 < count)
749  {
750  throw new ArgumentException(Environment.GetResourceString("Arg_BufferTooSmall"));
751  }
752  byte* pointer = null;
754  try
755  {
756  _buffer.AcquirePointer(ref pointer);
757  Buffer.Memcpy(pointer + num + _offset, 0, buffer, offset, count);
758  }
759  finally
760  {
761  if (pointer != null)
762  {
763  _buffer.ReleasePointer();
764  }
765  }
766  }
767  else
768  {
769  Buffer.Memcpy(_mem + num, 0, buffer, offset, count);
770  }
771  Interlocked.Exchange(ref _position, num3);
772  }
773 
780  [ComVisible(false)]
781  [HostProtection(SecurityAction.LinkDemand, ExternalThreading = true)]
782  public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
783  {
784  if (buffer == null)
785  {
786  throw new ArgumentNullException("buffer", Environment.GetResourceString("ArgumentNull_Buffer"));
787  }
788  if (offset < 0)
789  {
790  throw new ArgumentOutOfRangeException("offset", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
791  }
792  if (count < 0)
793  {
794  throw new ArgumentOutOfRangeException("count", Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
795  }
796  if (buffer.Length - offset < count)
797  {
798  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidOffLen"));
799  }
800  if (cancellationToken.IsCancellationRequested)
801  {
802  return Task.FromCancellation(cancellationToken);
803  }
804  try
805  {
806  Write(buffer, offset, count);
807  return Task.CompletedTask;
808  }
809  catch (Exception exception)
810  {
811  return Task.FromException<int>(exception);
812  }
813  }
814 
820  [SecuritySafeCritical]
821  public unsafe override void WriteByte(byte value)
822  {
823  if (!_isOpen)
824  {
825  __Error.StreamIsClosed();
826  }
827  if (!CanWrite)
828  {
829  __Error.WriteNotSupported();
830  }
831  long num = Interlocked.Read(ref _position);
832  long num2 = Interlocked.Read(ref _length);
833  long num3 = num + 1;
834  if (num >= num2)
835  {
836  if (num3 < 0)
837  {
838  throw new IOException(Environment.GetResourceString("IO.IO_StreamTooLong"));
839  }
840  if (num3 > _capacity)
841  {
842  throw new NotSupportedException(Environment.GetResourceString("IO.IO_FixedCapacity"));
843  }
844  if (_buffer == null)
845  {
846  if (num > num2)
847  {
848  Buffer.ZeroMemory(_mem + num2, num - num2);
849  }
850  Interlocked.Exchange(ref _length, num3);
851  }
852  }
853  if (_buffer != null)
854  {
855  byte* pointer = null;
857  try
858  {
859  _buffer.AcquirePointer(ref pointer);
860  (pointer + num)[_offset] = value;
861  }
862  finally
863  {
864  if (pointer != null)
865  {
866  _buffer.ReleasePointer();
867  }
868  }
869  }
870  else
871  {
872  _mem[num] = value;
873  }
874  Interlocked.Exchange(ref _position, num3);
875  }
876  }
877 }
UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class in a safe buffer with a spe...
static long Read(ref long location)
Returns a 64-bit value, loaded as an atomic operation.
Definition: Interlocked.cs:268
override long Length
Gets the length of the data in a stream.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
Propagates notification that operations should be canceled.
override long Position
Gets or sets the current position in a stream.
TResult Result
Gets the result value of this T:System.Threading.Tasks.Task`1.
Definition: Task.cs:57
unsafe UnmanagedMemoryStream()
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class.
unsafe override void Write(byte[] buffer, int offset, int count)
Writes a block of bytes to the current stream using data from a buffer.
Definition: __Canon.cs:3
void ReleasePointer()
Releases a pointer that was obtained by the M:System.Runtime.InteropServices.SafeBuffer....
Definition: SafeBuffer.cs:141
override bool CanRead
Gets a value indicating whether a stream supports reading.
The exception that is thrown when the value of an argument is outside the allowable range of values a...
unsafe override void SetLength(long value)
Sets the length of a stream to a specified value.
override bool CanSeek
Gets a value indicating whether a stream supports seeking.
unsafe override int Read([In] [Out] byte[] buffer, int offset, int count)
Reads the specified number of bytes into the specified array.
unsafe void Initialize(byte *pointer, long length, long capacity, FileAccess access)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class by using a pointer to an un...
unsafe override void WriteByte(byte value)
Writes a byte to the current position in the file stream.
bool IsCancellationRequested
Gets whether cancellation has been requested for this token.
ulong ByteLength
Gets the size of the buffer, in bytes.
Definition: SafeBuffer.cs:23
static int Exchange(ref int location1, int value)
Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operat...
SeekOrigin
Specifies the position in a stream to use for seeking.
Definition: SeekOrigin.cs:9
unsafe override void Dispose(bool disposing)
Releases the unmanaged resources used by the T:System.IO.UnmanagedMemoryStream and optionally release...
SecurityAction
Specifies the security actions that can be performed using declarative security.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class in a safe buffer with a spe...
unsafe byte * PositionPointer
Gets or sets a byte pointer to a stream based on the current position in the stream.
The exception that is thrown when an attempt is made to access an element of an array or collection w...
unsafe void AcquirePointer(ref byte *pointer)
Obtains a pointer from a T:System.Runtime.InteropServices.SafeBuffer object for a block of memory.
Definition: SafeBuffer.cs:118
Provides a controlled memory buffer that can be used for reading and writing. Attempts to access memo...
Definition: SafeBuffer.cs:11
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously reads the specified number of bytes into the specified array.Available starting in ....
static Task FromException(Exception exception)
Creates a T:System.Threading.Tasks.Task that has completed with a specified exception.
Definition: Task.cs:4130
The exception that is thrown when an I/O error occurs.
Definition: IOException.cs:10
override Task FlushAsync(CancellationToken cancellationToken)
Overrides the M:System.IO.Stream.FlushAsync(System.Threading.CancellationToken) method so that the op...
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
unsafe UnmanagedMemoryStream(byte *pointer, long length)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class using the specified locatio...
static Task CompletedTask
Gets a task that has already completed successfully.
Definition: Task.cs:1453
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
override void Flush()
Overrides the M:System.IO.Stream.Flush method so that no action is performed.
override long Seek(long offset, SeekOrigin loc)
Sets the current position of the current stream to the given value.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
unsafe override int ReadByte()
Reads a byte from a stream and advances the position within the stream by one byte,...
long Capacity
Gets the stream length (size) or the total amount of memory assigned to a stream (capacity).
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
FileAccess
Defines constants for read, write, or read/write access to a file.
Definition: FileAccess.cs:9
unsafe UnmanagedMemoryStream(byte *pointer, long length, long capacity, FileAccess access)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class using the specified locatio...
Manipulates arrays of primitive types.
Definition: Buffer.cs:11
Provides access to unmanaged blocks of memory from managed code.
The exception that is thrown when an invoked method is not supported, or when there is an attempt to ...
SecurityPermissionFlag
Specifies access flags for the security permission object.
Provides atomic operations for variables that are shared by multiple threads.
Definition: Interlocked.cs:10
void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access)
Initializes a new instance of the T:System.IO.UnmanagedMemoryStream class in a safe buffer with a spe...
Provides a set of static methods and properties that provide support for compilers....
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16
override bool CanWrite
Gets a value indicating whether a stream supports writing.