Unmanaged​Memory​Stream Class

Definition

Provides access to unmanaged blocks of memory from managed code.

public class UnmanagedMemoryStream : System.IO.Stream
Inheritance
UnmanagedMemoryStream
Derived

Inherited Members

System.IO.Stream

System.MarshalByRefObject

System.Object

Examples

The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. A block of unmanaged memory is allocated and de-allocated using the Marshal class.


// Note: you must compile this sample using the unsafe flag.
// From the command line, type the following: csc sample.cs /unsafe

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

unsafe class TestWriter
{
    
    static void Main()
	{
		
            // Create some data to read and write.
            byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data.");

	    // Allocate a block of unmanaged memory and return an IntPtr object.	
            IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length);

            // Get a byte pointer from the IntPtr object.
            byte* memBytePtr = (byte*) memIntPtr.ToPointer();

            // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write);

            // Write the data.
            writeStream.Write(message, 0, message.Length);

            // Close the stream.
            writeStream.Close();

            // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory.
            UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read);

	    // Create a byte array to hold data from unmanaged memory.
            byte[] outMessage = new byte[message.Length];

            // Read from unmanaged memory to the byte array.
            readStream.Read(outMessage, 0, message.Length);

            // Close the stream.
            readStream.Close();

            // Display the data to the console.
            Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage));

            // Free the block of unmanaged memory.
            Marshal.FreeHGlobal(memIntPtr);

            Console.ReadLine();
    }
}

Remarks

This class supports access to unmanaged memory using the existing stream-based model and does not require that the contents in the unmanaged memory be copied to the heap.

Note

This type implements the IDisposable interface, but does not actually have any resources to dispose. This means that disposing it by directly calling Dispose() or by using a language construct such as using (in C#) or Using (in Visual Basic) is not necessary.

Constructors

Unmanaged​Memory​Stream()

Initializes a new instance of the UnmanagedMemoryStream class.

Unmanaged​Memory​Stream(​Byte*, ​Int64)

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

Unmanaged​Memory​Stream(​Safe​Buffer, ​Int64, ​Int64)

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

Unmanaged​Memory​Stream(​Byte*, ​Int64, ​Int64, ​File​Access)

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

Unmanaged​Memory​Stream(​Safe​Buffer, ​Int64, ​Int64, ​File​Access)

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

Properties

Can​Read

Gets a value indicating whether a stream supports reading.

Can​Seek

Gets a value indicating whether a stream supports seeking.

Can​Write

Gets a value indicating whether a stream supports writing.

Capacity

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

Length

Gets the length of the data in a stream.

Position

Gets or sets the current position in a stream.

Position​Pointer

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

Methods

Dispose(​Boolean)

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

Flush()

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

Flush​Async(​Cancellation​Token)

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

Initialize(​Safe​Buffer, ​Int64, ​Int64, ​File​Access)

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

Initialize(​Byte*, ​Int64, ​Int64, ​File​Access)

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

Read(​Byte[], ​Int32, ​Int32)

Reads the specified number of bytes into the specified array.

Read​Async(​Byte[], ​Int32, ​Int32, ​Cancellation​Token)

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

Available starting in .NET Framework 4.6

Read​Byte()

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.

Seek(​Int64, ​Seek​Origin)

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

Set​Length(​Int64)

Sets the length of a stream to a specified value.

Write(​Byte[], ​Int32, ​Int32)

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

Write​Async(​Byte[], ​Int32, ​Int32, ​Cancellation​Token)

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

Write​Byte(​Byte)

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