UnmanagedMemoryStream.Write メソッド

定義

オーバーロード

Write(ReadOnlySpan<Byte>)

指定されたバイトの範囲のデータを使用して、現在のアンマネージ メモリ ストリームにバイトのブロックを書き込みます。

Write(Byte[], Int32, Int32)

バッファーのデータを使用して、現在のストリームにバイトのブロックを書き込みます。

Write(ReadOnlySpan<Byte>)

ソース:
UnmanagedMemoryStream.cs
ソース:
UnmanagedMemoryStream.cs
ソース:
UnmanagedMemoryStream.cs

指定されたバイトの範囲のデータを使用して、現在のアンマネージ メモリ ストリームにバイトのブロックを書き込みます。

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))

パラメーター

sourcebuffer
ReadOnlySpan<Byte>

現在のアンマネージ メモリ ストリームへのバイトのコピー元となるバイトの範囲。

適用対象

Write(Byte[], Int32, Int32)

ソース:
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 エラーが発生します。

指定したパラメーターのいずれかが 0 未満です。

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

注釈

書き込みは、ストリーム内の現在の位置で行われます。

適用対象