UnmanagedMemoryStream.Write Method (Byte[], Int32, Int32)
Writes a block of bytes to the current stream using data from a buffer.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- buffer
-
Type:
System.Byte[]
The byte array from which to copy bytes to the current stream.
- offset
-
Type:
System.Int32
The offset in the buffer at which to begin copying bytes to the current stream.
- count
-
Type:
System.Int32
The number of bytes to write to the current stream.
| Exception | Condition |
|---|---|
| ObjectDisposedException | The stream is closed. |
| NotSupportedException | 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 count value is greater than the capacity of the stream. - or - The position is at the end of the stream capacity. |
| IOException | An I/O error occurs. |
| ArgumentOutOfRangeException | One of the specified parameters is less than zero. |
| ArgumentException | The offset parameter minus the length of the buffer parameter is less than the count parameter. |
| ArgumentNullException | The buffer parameter is null. |
Writing occurs at the current position in the stream.
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(); } }
Available since 10
.NET Framework
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0