Class MemoryStream
A stream that is backed by a provided memory buffer
public sealed class MemoryStream : Stream, IDisposable
- Inheritance
-
MemoryStream
- Implements
- Inherited Members
- Extension Methods
Constructors
MemoryStream(NativeMemory)
Creates a new MemoryStream from a specified allocated memory buffer
public MemoryStream(NativeMemory nativeMemory)
Parameters
nativeMemoryNativeMemoryThe allocated memory buffer to use for the stream
Remarks
This pins the specified allocated memory buffer for the lifetime of the MemoryStream.
Exceptions
- ArgumentException
nativeMemoryis invalid
MemoryStream(Memory<byte>)
Creates a new MemoryStream from a specified Memory<T>
public MemoryStream(Memory<byte> memory)
Parameters
Remarks
This pins the specified Memory<T> for the lifetime of the MemoryStream.
Exceptions
- InvalidOperationException
memorycould not be pinned
MemoryStream(void*, nuint, MemoryStreamFreeFunc?)
Creates a new MemoryStream from a specified unmanaged memory and size
public MemoryStream(void* mem, nuint size, MemoryStreamFreeFunc? freeFunc = null)
Parameters
memvoid*A pointer to the unmanaged memory to use for the stream
sizenuintThe size of the unmanaged memory
freeFuncMemoryStreamFreeFuncAn optional delegate that is called to free the unmanaged memory when the stream is closed or disposed
Remarks
If freeFunc is unset or null, the unmanaged memory will not be freed when the stream is closed or disposed.
This parameter is reflected by the FreeFunc property. Changing the value of the FreeFunc property after construction also changes the automatic freeing behavior.
Properties
FreeFunc
Gets or sets a delegate that will be called to free the memory buffer when the MemoryStream is closed or disposed
public MemoryStreamFreeFunc? FreeFunc { get; set; }
Property Value
- MemoryStreamFreeFunc
A delegate that will be called to free the memory buffer when the MemoryStream is closed or disposed
Remarks
If the value of this property is null, the memory buffer will not be freed when the MemoryStream is closed or disposed.
Changing the value of this property will also change the automatic freeing behavior.
Memory
Gets a pointer to the memory buffer that the MemoryStream was initialized with
public nint Memory { get; }
Property Value
- nint
A pointer to the memory buffer that the MemoryStream was initialized with
- See Also
Size
Gets the size, in bytes, of the memory buffer that the MemoryStream was initialized with
public nuint Size { get; }
Property Value
- nuint
The size, in bytes, of the memory buffer that the MemoryStream was initialized with
- See Also
Methods
Dispose(bool, bool)
Disposes the stream
protected override void Dispose(bool disposing, bool close)
Parameters
disposingboolA value indicating whether the call came from a call to Dispose() or from the finalizer
closeboolA value indicating whether the stream should be closed
Remarks
This method is not threadsafe.
- See Also
-
TryClose()