13 private const long UnmanagedMemStreamMaxLength =
long.MaxValue;
19 private unsafe
byte* _mem;
23 private long _capacity;
25 private long _position;
31 internal bool _isOpen;
74 public override long Length 80 __Error.StreamIsClosed();
95 __Error.StreamIsClosed();
111 __Error.StreamIsClosed();
115 [SecuritySafeCritical]
124 __Error.StreamIsClosed();
136 [CLSCompliant(
false)]
151 byte* result = _mem + num;
154 __Error.StreamIsClosed();
167 __Error.StreamIsClosed();
169 if (
new IntPtr(value - _mem).ToInt64() >
long.MaxValue)
181 internal unsafe
byte* Pointer
196 [SecuritySafeCritical]
207 [SecuritySafeCritical]
218 [SecuritySafeCritical]
221 Initialize(buffer, offset, length, access, skipSecurityCheck:
false);
227 Initialize(buffer, offset, length, access, skipSecurityCheck);
235 [SecuritySafeCritical]
238 Initialize(buffer, offset, length, access, skipSecurityCheck:
false);
254 throw new ArgumentOutOfRangeException(
"length", Environment.GetResourceString(
"ArgumentOutOfRange_NeedNonNegNum"));
256 if (buffer.
ByteLength < (ulong)(offset + length))
258 throw new ArgumentException(Environment.GetResourceString(
"Argument_InvalidSafeBufferOffLen"));
260 if (access < FileAccess.Read || access >
FileAccess.ReadWrite)
262 throw new ArgumentOutOfRangeException(
"access");
266 throw new InvalidOperationException(Environment.GetResourceString(
"InvalidOperation_CalledTwice"));
268 if (!skipSecurityCheck)
272 byte* pointer =
null;
277 if (pointer + offset + length < pointer)
279 throw new ArgumentException(Environment.GetResourceString(
"ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
304 [CLSCompliant(
false)]
319 [CLSCompliant(
false)]
322 Initialize(pointer, length, capacity, access, skipSecurityCheck:
false);
328 Initialize(pointer, length, capacity, access, skipSecurityCheck);
340 [CLSCompliant(
false)]
343 Initialize(pointer, length, capacity, access, skipSecurityCheck:
false);
347 internal unsafe
void Initialize(
byte* pointer,
long length,
long capacity,
FileAccess access,
bool skipSecurityCheck)
353 if (length < 0 || capacity < 0)
357 if (length > capacity)
359 throw new ArgumentOutOfRangeException(
"length", Environment.GetResourceString(
"ArgumentOutOfRange_LengthGreaterThanCapacity"));
361 if ((ulong)(UIntPtr)(
void*)((long)pointer + capacity) < (ulong)pointer)
363 throw new ArgumentOutOfRangeException(
"capacity", Environment.GetResourceString(
"ArgumentOutOfRange_UnmanagedMemStreamWrapAround"));
365 if (access < FileAccess.Read || access >
FileAccess.ReadWrite)
367 throw new ArgumentOutOfRangeException(
"access", Environment.GetResourceString(
"ArgumentOutOfRange_Enum"));
371 throw new InvalidOperationException(Environment.GetResourceString(
"InvalidOperation_CalledTwice"));
373 if (!skipSecurityCheck)
380 _capacity = capacity;
388 [SecuritySafeCritical]
389 protected unsafe
override void Dispose(
bool disposing)
393 base.Dispose(disposing);
402 __Error.StreamIsClosed();
410 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
415 return Task.FromCancellation(cancellationToken);
438 [SecuritySafeCritical]
439 public unsafe
override int Read([In] [Out]
byte[] buffer,
int offset,
int count)
453 if (buffer.Length - offset < count)
459 __Error.StreamIsClosed();
463 __Error.ReadNotSupported();
467 long num3 = num2 - num;
476 int num4 = (int)num3;
483 byte* pointer =
null;
488 Buffer.Memcpy(buffer, offset, pointer + num + _offset, 0, num4);
500 Buffer.Memcpy(buffer, offset, _mem + num, 0, num4);
513 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
528 if (buffer.Length - offset < count)
534 return Task.FromCancellation<
int>(cancellationToken);
538 int num =
Read(buffer, offset, count);
540 return (lastReadTask !=
null && lastReadTask.
Result == num) ? lastReadTask : (_lastReadTask =
Task.FromResult(num));
552 [SecuritySafeCritical]
557 __Error.StreamIsClosed();
561 __Error.ReadNotSupported();
572 byte* pointer =
null;
577 return (pointer + num)[_offset];
603 __Error.StreamIsClosed();
605 if (offset >
long.MaxValue)
621 if (offset + num2 < 0)
631 if (num + offset < 0)
650 [SecuritySafeCritical]
663 __Error.StreamIsClosed();
667 __Error.WriteNotSupported();
669 if (value > _capacity)
677 Buffer.ZeroMemory(_mem + num2, value - num2);
696 [SecuritySafeCritical]
697 public unsafe
override void Write(
byte[] buffer,
int offset,
int count)
711 if (buffer.Length - offset < count)
717 __Error.StreamIsClosed();
721 __Error.WriteNotSupported();
725 long num3 = num + count;
730 if (num3 > _capacity)
738 Buffer.ZeroMemory(_mem + num2, num - num2);
747 long num4 = _capacity - num;
752 byte* pointer =
null;
757 Buffer.Memcpy(pointer + num + _offset, 0, buffer, offset, count);
769 Buffer.Memcpy(_mem + num, 0, buffer, offset, count);
781 [HostProtection(
SecurityAction.LinkDemand, ExternalThreading =
true)]
796 if (buffer.Length - offset < count)
802 return Task.FromCancellation(cancellationToken);
806 Write(buffer, offset, count);
820 [SecuritySafeCritical]
825 __Error.StreamIsClosed();
829 __Error.WriteNotSupported();
840 if (num3 > _capacity)
848 Buffer.ZeroMemory(_mem + num2, num - num2);
855 byte* pointer =
null;
860 (pointer + num)[_offset] = value;
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.
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.
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.
void ReleasePointer()
Releases a pointer that was obtained by the M:System.Runtime.InteropServices.SafeBuffer....
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.
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.
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....
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.
Provides a controlled memory buffer that can be used for reading and writing. Attempts to access memo...
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.
The exception that is thrown when an I/O error occurs.
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.
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.
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...
FileAccess
Defines constants for read, write, or read/write access to a file.
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.
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.
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 ....
override bool CanWrite
Gets a value indicating whether a stream supports writing.