UnmanagedMemoryStream Class

Provides access to unmanaged blocks of memory from managed code.

Syntax

Declaration

public class UnmanagedMemoryStream : Stream, IDisposable

Inheritance Hierarchy

Constructors summary

Initializes a new instance of the UnmanagedMemoryStream class.

Initializes a new instance of the UnmanagedMemoryStream class using the specified location and memory length.

Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset and length.

Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

Properties summary

Gets a value indicating whether a stream supports reading.

Gets a value indicating whether a stream supports seeking.

Gets a value indicating whether a stream supports writing.

Gets the stream length (size) or the total amount of memory assigned to a stream (capacity).

Gets the length of the data in a stream.

Gets or sets the current position in a stream.

Gets or sets a byte pointer to a stream based on the current position in the stream.

Methods summary

Releases the unmanaged resources used by the UnmanagedMemoryStream and optionally releases the managed resources.

Overrides the Flush() method so that no action is performed.

Overrides the FlushAsync(CancellationToken) method so that the operation is cancelled if specified, but no other action is performed.

Available starting in .NET Framework 4.6

Initializes a new instance of the UnmanagedMemoryStream class by using a pointer to an unmanaged memory location.

Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

Reads the specified number of bytes into the specified array.

Asynchronously reads the specified number of bytes into the specified array.

Available starting in .NET Framework 4.6

Reads a byte from a stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

Sets the current position of the current stream to the given value.

Sets the length of a stream to a specified value.

Writes a block of bytes to the current stream using data from a buffer.

Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

Available starting in .NET Framework 4.6

Writes a byte to the current position in the file stream.

Constructors

  • UnmanagedMemoryStream()

    Initializes a new instance of the UnmanagedMemoryStream class.

    protected UnmanagedMemoryStream()

    Exceptions

  • UnmanagedMemoryStream(Byte*, Int64)

    Initializes a new instance of the UnmanagedMemoryStream class using the specified location and memory length.

    [SecurityCritical]
    [CLSCompliant(false)]
    public UnmanagedMemoryStream(byte *pointer, long length)

    Parameters

    • pointer

      A pointer to an unmanaged memory location.

    • length

      The length of the memory to use.

    Exceptions

  • UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

    Initializes a new instance of the UnmanagedMemoryStream class using the specified location, memory length, total amount of memory, and file access values.

    [SecurityCritical]
    [CLSCompliant(false)]
    public UnmanagedMemoryStream(byte *pointer, long length, long capacity, FileAccess access)

    Parameters

    • pointer

      A pointer to an unmanaged memory location.

    • length

      The length of the memory to use.

    • capacity

      The total amount of memory assigned to the stream.

    • access

      One of the FileAccess values.

    Exceptions

  • UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

    Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset and length.

    public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length)

    Parameters

    • buffer

      The buffer to contain the unmanaged memory stream.

    • offset

      The byte position in the buffer at which to start the unmanaged memory stream.

    • length

      The length of the unmanaged memory stream.

  • UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

    Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

    public UnmanagedMemoryStream(SafeBuffer buffer, long offset, long length, FileAccess access)

    Parameters

    • buffer

      The buffer to contain the unmanaged memory stream.

    • offset

      The byte position in the buffer at which to start the unmanaged memory stream.

    • length

      The length of the unmanaged memory stream.

    • access

      The mode of file access to the unmanaged memory stream.

Properties

  • CanRead

    Gets a value indicating whether a stream supports reading.

    public override bool CanRead { get; }

    Property Value

    • false if the object was created by a constructor with an access parameter that did not include reading the stream and if the stream is closed; otherwise, true.

    Overrides

  • CanSeek

    Gets a value indicating whether a stream supports seeking.

    public override bool CanSeek { get; }

    Property Value

    • false if the stream is closed; otherwise, true.

    Overrides

  • CanWrite

    Gets a value indicating whether a stream supports writing.

    public override bool CanWrite { get; }

    Property Value

    • false if the object was created by a constructor with an access parameter value that supports writing or was created by a constructor that had no parameters, or if the stream is closed; otherwise, true.

    Overrides

  • Capacity

    Gets the stream length (size) or the total amount of memory assigned to a stream (capacity).

    public long Capacity { get; }

    Property Value

    • The size or capacity of the stream.

    Exceptions

  • Length

    Gets the length of the data in a stream.

    public override long Length { get; }

    Property Value

    • The length of the data in the stream.

    Exceptions

    Overrides

  • Position

    Gets or sets the current position in a stream.

    public override long Position { get; set; }

    Property Value

    • The current position in the stream.

    Exceptions

    Overrides

  • PositionPointer

    Gets or sets a byte pointer to a stream based on the current position in the stream.

    [CLSCompliant(false)]
    public byte *PositionPointer
    {
        [SecurityCritical]
        get;
        [SecurityCritical]
        set;
    }

    Property Value

    • A byte pointer.

    Exceptions

Methods

  • Dispose(Boolean)

    Releases the unmanaged resources used by the UnmanagedMemoryStream and optionally releases the managed resources.

    protected override void Dispose(bool disposing)

    Parameters

    • disposing

      true to release both managed and unmanaged resources; false to release only unmanaged resources.

    Overrides

  • Flush()

    Overrides the Flush() method so that no action is performed.

    public override void Flush()

    Exceptions

    Overrides

  • FlushAsync(CancellationToken)

    Overrides the FlushAsync(CancellationToken) method so that the operation is cancelled if specified, but no other action is performed.

    Available starting in .NET Framework 4.6

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task FlushAsync(CancellationToken cancellationToken)

    Parameters

    • cancellationToken

      The token to monitor for cancellation requests. The default value is None.

    Returns

    • A task that represents the asynchronous flush operation.

    Overrides

  • Initialize(Byte*, Int64, Int64, FileAccess)

    Initializes a new instance of the UnmanagedMemoryStream class by using a pointer to an unmanaged memory location.

    [SecurityCritical]
    [CLSCompliant(false)]
    protected void Initialize(byte *pointer, long length, long capacity, FileAccess access)

    Parameters

    • pointer

      A pointer to an unmanaged memory location.

    • length

      The length of the memory to use.

    • capacity

      The total amount of memory assigned to the stream.

    • access

      One of the FileAccess values.

    Exceptions

  • Initialize(SafeBuffer, Int64, Int64, FileAccess)

    Initializes a new instance of the UnmanagedMemoryStream class in a safe buffer with a specified offset, length, and file access.

    protected void Initialize(SafeBuffer buffer, long offset, long length, FileAccess access)

    Parameters

    • buffer

      The buffer to contain the unmanaged memory stream.

    • offset

      The byte position in the buffer at which to start the unmanaged memory stream.

    • length

      The length of the unmanaged memory stream.

    • access

      The mode of file access to the unmanaged memory stream.

  • Read(Byte[], Int32, Int32)

    Reads the specified number of bytes into the specified array.

    public override int Read(byte[] buffer, int offset, int count)

    Parameters

    • buffer

      When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. This parameter is passed uninitialized.

    • offset

      The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

    • count

      The maximum number of bytes to read from the current stream.

    Returns

    • The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

    Exceptions

    Overrides

  • ReadAsync(Byte[], Int32, Int32, CancellationToken)

    Asynchronously reads the specified number of bytes into the specified array.

    Available starting in .NET Framework 4.6

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write the data into.

    • offset

      The byte offset in buffer at which to begin writing data from the stream.

    • count

      The maximum number of bytes to read.

    • cancellationToken

      The token to monitor for cancellation requests. The default value is None.

    Returns

    • A task that represents the asynchronous read operation. The value of the TResult parameter contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

    Overrides

  • ReadByte()

    Reads a byte from a stream and advances the position within the stream by one byte, or returns -1 if at the end of the stream.

    public override int ReadByte()

    Returns

    • The unsigned byte cast to an Int32 object, or -1 if at the end of the stream.

    Exceptions

    Overrides

  • Seek(Int64, SeekOrigin)

    Sets the current position of the current stream to the given value.

    public override long Seek(long offset, SeekOrigin loc)

    Parameters

    • offset

      The point relative to origin to begin seeking from.

    • loc

      Specifies the beginning, the end, or the current position as a reference point for origin, using a value of type SeekOrigin.

    Returns

    • The new position in the stream.

    Exceptions

    Overrides

  • SetLength(Int64)

    Sets the length of a stream to a specified value.

    public override void SetLength(long value)

    Parameters

    • value

      The length of the stream.

    Exceptions

    Overrides

  • Write(Byte[], Int32, Int32)

    Writes a block of bytes to the current stream using data from a buffer.

    public override void Write(byte[] buffer, int offset, int count)

    Parameters

    • buffer

      The byte array from which to copy bytes to the current stream.

    • offset

      The offset in the buffer at which to begin copying bytes to the current stream.

    • count

      The number of bytes to write to the current stream.

    Exceptions

    Overrides

  • WriteAsync(Byte[], Int32, Int32, CancellationToken)

    Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.

    Available starting in .NET Framework 4.6

    [HostProtection(ExternalThreading = true)]
    [ComVisible(false)]
    public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)

    Parameters

    • buffer

      The buffer to write data from.

    • offset

      The zero-based byte offset in buffer from which to begin copying bytes to the stream.

    • count

      The maximum number of bytes to write.

    • cancellationToken

      The token to monitor for cancellation requests. The default value is None.

    Returns

    • A task that represents the asynchronous write operation.

    Overrides

  • WriteByte(Byte)

    Writes a byte to the current position in the file stream.

    public override void WriteByte(byte value)

    Parameters

    • value

      A byte value written to the stream.

    Exceptions

    • The stream is closed.

    • The underlying memory does not support writing.

      - or -

      An attempt is made to write to the stream and the CanWrite property is false.

      - or -

      The current position is at the end of the capacity of the stream.

    • The supplied value causes the stream exceed its maximum capacity.

    Overrides

Details

Namespace

System.IO

Assembly

System.IO.UnmanagedMemoryStream.dll