Uredi

Deli z drugimi prek


UnmanagedMemoryStream.Write Method

Definition

Overloads

Write(ReadOnlySpan<Byte>)

Writes a block of bytes to the current unmanaged memory stream using data from the provided span of bytes.

Write(Byte[], Int32, Int32)

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

Write(ReadOnlySpan<Byte>)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs

Writes a block of bytes to the current unmanaged memory stream using data from the provided span of bytes.

public:
 override void Write(ReadOnlySpan<System::Byte> source);
public:
 override void Write(ReadOnlySpan<System::Byte> buffer);
public override void Write (ReadOnlySpan<byte> source);
public override void Write (ReadOnlySpan<byte> buffer);
override this.Write : ReadOnlySpan<byte> -> unit
override this.Write : ReadOnlySpan<byte> -> unit
Public Overrides Sub Write (source As ReadOnlySpan(Of Byte))
Public Overrides Sub Write (buffer As ReadOnlySpan(Of Byte))

Parameters

sourcebuffer
ReadOnlySpan<Byte>

The span of bytes from which to copy bytes to the current unmanaged memory stream.

Applies to

Write(Byte[], Int32, Int32)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs

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

public:
 override void Write(cli::array <System::Byte> ^ buffer, int offset, int count);
public override void Write (byte[] buffer, int offset, int count);
override this.Write : byte[] * int * int -> unit
Public Overrides Sub Write (buffer As Byte(), offset As Integer, count As Integer)

Parameters

buffer
Byte[]

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

offset
Int32

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

count
Int32

The number of bytes to write to the current 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 count value is greater than the capacity of the stream.

-or-

The position is at the end of the stream capacity.

An I/O error occurs.

One of the specified parameters is less than zero.

The offset parameter minus the length of the buffer parameter is less than the count parameter.

The buffer parameter is null.

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

Writing occurs at the current position in the stream.

Applies to