語言

UnmanagedMemoryStream.Write 方法

定義

多載

名稱 Description
Write(ReadOnlySpan<Byte>)

利用提供的位元組區間資料,將一個位元組區塊寫入當前未管理的記憶體串流。

Write(Byte[], Int32, Int32)

利用緩衝區的資料寫入一個位元組區塊到目前的串流。

Write(ReadOnlySpan<Byte>)

來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs

利用提供的位元組區間資料,將一個位元組區塊寫入當前未管理的記憶體串流。

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

參數

sourcebuffer
ReadOnlySpan<Byte>

是將位元組複製到目前未管理記憶體串流的位元組範圍。

適用於

Write(Byte[], Int32, Int32)

來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs
來源:
UnmanagedMemoryStream.cs

利用緩衝區的資料寫入一個位元組區塊到目前的串流。

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)

參數

buffer
Byte[]

用來複製位元組到目前串流的位元組陣列。

offset
Int32

緩衝區中開始將位元組複製到當前串流的偏移量。

count
Int32

寫入當前串流的位元組數。

例外狀況

溪流已經關閉。

底層記憶體不支援寫入。

-或-

嘗試寫入串流,屬性 CanWrite 為 false。

-或-

其 count 價值大於溪流的容量。

-或-

這個職位位於溪流容量的末端。

發生 I/O 錯誤。

其中一個指定的參數小於零。

offset參數減去參數長度buffer的比參數小count。

參數 buffer 為 null。

範例

以下程式碼範例示範如何使用類別讀取與寫入未管理記憶體 UnmanagedMemoryStream 。 使用類別 Marshal 分配與取消管理的記憶體區塊。


// 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();
    }
}

備註

寫入發生在串流中的當前位置。

適用於