UnmanagedMemoryStream コンストラクター

定義

UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

オーバーロード

UnmanagedMemoryStream()

UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

UnmanagedMemoryStream(Byte*, Int64)

指定した位置とメモリ長を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

指定したオフセットおよび長さを使用して、セーフ バッファーに UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

指定した位置、メモリ長、メモリ総量、およびファイル アクセス値を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

指定したオフセット、長さ、およびファイル アクセスを使用して、セーフ バッファーに UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

UnmanagedMemoryStream()

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

UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

protected:
 UnmanagedMemoryStream();
protected UnmanagedMemoryStream ();
Protected Sub New ()

例外

ユーザーに必要なアクセス許可がありません。

適用対象

UnmanagedMemoryStream(Byte*, Int64)

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

重要

この API は CLS 準拠ではありません。

指定した位置とメモリ長を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

public:
 UnmanagedMemoryStream(System::Byte* pointer, long length);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public UnmanagedMemoryStream (byte* pointer, long length);
[System.CLSCompliant(false)]
public UnmanagedMemoryStream (byte* pointer, long length);
public UnmanagedMemoryStream (byte* pointer, long length);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 -> System.IO.UnmanagedMemoryStream
[<System.CLSCompliant(false)>]
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 -> System.IO.UnmanagedMemoryStream
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 -> System.IO.UnmanagedMemoryStream

パラメーター

pointer
Byte*

アンマネージ メモリ位置へのポインター。

length
Int64

使用するメモリの長さ。

属性

例外

ユーザーに必要なアクセス許可がありません。

pointer 値は null です。

length 値が 0 未満です。

または

length がオーバーフローの原因となりうる長さです。

次のコード例では、 クラスを使用してアンマネージド メモリの読み取りと書き込みを行う方法を 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.Runtime.InteropServices;
using System.Text;

unsafe class Program
{
    static void Main()
    {
        // Create some data to write.
        byte[] text = UnicodeEncoding.Unicode.GetBytes("Data to write.");

        // Allocate a block of unmanaged memory.
        IntPtr memIntPtr = Marshal.AllocHGlobal(text.Length);

        // Get a byte pointer from the unmanaged memory block.
        byte* memBytePtr = (byte*)memIntPtr.ToPointer();

        UnmanagedMemoryStream writeStream =
            new UnmanagedMemoryStream(
            memBytePtr, text.Length, text.Length, FileAccess.Write);

        // Write the data.
        WriteToStream(writeStream, text);

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

        // Create another UnmanagedMemoryStream for reading.
        UnmanagedMemoryStream readStream =
            new UnmanagedMemoryStream(memBytePtr, text.Length);

        // Display the contents of the stream to the console.
        PrintStream(readStream);

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

        // Free up the unmanaged memory.
        Marshal.FreeHGlobal(memIntPtr);
    }

    public static void WriteToStream(UnmanagedMemoryStream writeStream, byte[] text)
    {
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (writeStream.CanWrite)
        {
            // Write the data, byte by byte
            for (int i = 0; i < writeStream.Length; i++)
            {
                writeStream.WriteByte(text[i]);
            }
        }
    }

    public static void PrintStream(UnmanagedMemoryStream readStream)
    {
        byte[] text = new byte[readStream.Length];
        // Verify that the stream is writable:
        // By default, UnmanagedMemoryStream objects do not have write access,
        // write access must be set explicitly.
        if (readStream.CanRead)
        {
            // Write the data, byte by byte
            for (int i = 0; i < readStream.Length; i++)
            {
                text[i] = (byte)readStream.ReadByte();
            }
        }

        Console.WriteLine(UnicodeEncoding.Unicode.GetString(text));
    }
}

注釈

このコンストラクターは、 クラスの新しいインスタンスを UnmanagedMemoryStream 作成し、既定では プロパティを CanWritefalse 、プロパティを CanReadtrue設定します。 プロパティは Length パラメーターの length 値に設定されており、変更できません。

適用対象

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

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

指定したオフセットおよび長さを使用して、セーフ バッファーに UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

public:
 UnmanagedMemoryStream(System::Runtime::InteropServices::SafeBuffer ^ buffer, long offset, long length);
public UnmanagedMemoryStream (System.Runtime.InteropServices.SafeBuffer buffer, long offset, long length);
new System.IO.UnmanagedMemoryStream : System.Runtime.InteropServices.SafeBuffer * int64 * int64 -> System.IO.UnmanagedMemoryStream
Public Sub New (buffer As SafeBuffer, offset As Long, length As Long)

パラメーター

buffer
SafeBuffer

アンマネージ メモリ ストリームを格納するバッファー。

offset
Int64

バッファー内のアンマネージ メモリ ストリームの開始バイト位置。

length
Int64

アンマネージ メモリ ストリームの長さ。

適用対象

UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

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

重要

この API は CLS 準拠ではありません。

指定した位置、メモリ長、メモリ総量、およびファイル アクセス値を使用して、UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

public:
 UnmanagedMemoryStream(System::Byte* pointer, long length, long capacity, System::IO::FileAccess access);
[System.CLSCompliant(false)]
[System.Security.SecurityCritical]
public UnmanagedMemoryStream (byte* pointer, long length, long capacity, System.IO.FileAccess access);
[System.CLSCompliant(false)]
public UnmanagedMemoryStream (byte* pointer, long length, long capacity, System.IO.FileAccess access);
public UnmanagedMemoryStream (byte* pointer, long length, long capacity, System.IO.FileAccess access);
[<System.CLSCompliant(false)>]
[<System.Security.SecurityCritical>]
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream
[<System.CLSCompliant(false)>]
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream
new System.IO.UnmanagedMemoryStream : nativeptr<byte> * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream

パラメーター

pointer
Byte*

アンマネージ メモリ位置へのポインター。

length
Int64

使用するメモリの長さ。

capacity
Int64

ストリームに割り当てられたメモリの総量。

access
FileAccess

FileAccess 値のいずれか 1 つ。

属性

例外

ユーザーに必要なアクセス許可がありません。

pointer 値は null です。

length 値が 0 未満です。

または

capacity 値が 0 未満です。

または

length 値が capacity 値を超えています。

次のコード例では、 クラスを使用してアンマネージド メモリの読み取りと書き込みを行う方法を 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();
    }
}

注釈

パラメーターは length 、使用中のメモリの現在の量を定義します。 ストリームにデータを読み取ったり追加したりする場合、値は、 length 読み取り元または保持されるストリーム内の有効なデータの量と同じである必要があります。 ストリームに書き込む場合、この値は 0 にする必要があります。

パラメーターは capacity 、使用可能なメモリの合計量を示します。 この値は、指定された長さより長い領域を記述することも、追加できる領域を示す場合があります。 この値を超えて書き込もうとすると失敗します。

パラメーターはaccess、 プロパティ、および プロパティをCanReadCanWrite設定します。 を Write 指定しても、ストリームが書き込み可能になることは保証されないことに注意してください。 アクセス パラメーターを使用すると、実装者は、実装が公開されている実際のストリームと一致するオブジェクトを作成できます。

適用対象

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

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

指定したオフセット、長さ、およびファイル アクセスを使用して、セーフ バッファーに UnmanagedMemoryStream クラスの新しいインスタンスを初期化します。

public:
 UnmanagedMemoryStream(System::Runtime::InteropServices::SafeBuffer ^ buffer, long offset, long length, System::IO::FileAccess access);
public UnmanagedMemoryStream (System.Runtime.InteropServices.SafeBuffer buffer, long offset, long length, System.IO.FileAccess access);
new System.IO.UnmanagedMemoryStream : System.Runtime.InteropServices.SafeBuffer * int64 * int64 * System.IO.FileAccess -> System.IO.UnmanagedMemoryStream
Public Sub New (buffer As SafeBuffer, offset As Long, length As Long, access As FileAccess)

パラメーター

buffer
SafeBuffer

アンマネージ メモリ ストリームを格納するバッファー。

offset
Int64

バッファー内のアンマネージ メモリ ストリームの開始バイト位置。

length
Int64

アンマネージ メモリ ストリームの長さ。

access
FileAccess

アンマネージ メモリ ストリームへのファイル アクセスのモード。

適用対象