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 類別的新執行個體。

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

例外狀況

使用者沒有所需要的使用權限。

適用於

UnmanagedMemoryStream(Byte*, Int64)

重要

此 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*

Unmanaged 記憶體位置的指標。

length
Int64

要使用的記憶體長度。

屬性

例外狀況

使用者沒有所需要的使用權限。

pointer 值為 null

length 值小於 0。

-或- length 的大小足以造成溢位。

範例

下列程式碼範例示範如何使用 類別讀取和寫入 Unmanaged 記憶體 UnmanagedMemoryStream 。 Unmanaged 記憶體區塊會使用 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 ,而且預設會將 CanWrite 屬性設定為 false ,並將 CanRead 屬性設定為 true 。 屬性 Length 會設定為 參數的值 length ,而且無法變更。

適用於

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

在安全緩衝區中使用指定的位移和長度,初始化 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

包含 Unmanaged 記憶體資料流的緩衝區。

offset
Int64

緩衝區中的位元組位置,Unmanaged 記憶體資料流會在此處開始。

length
Int64

Unmanaged 記憶體資料流的長度。

適用於

UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

重要

此 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*

Unmanaged 記憶體位置的指標。

length
Int64

要使用的記憶體長度。

capacity
Int64

指派給資料流的記憶體總量。

access
FileAccess

其中一個 FileAccess 值。

屬性

例外狀況

使用者沒有所需要的使用權限。

pointer 值為 null

length 值小於 0。

-或- capacity 值小於 0。

-或- length 值大於 capacity 值。

範例

下列程式碼範例示範如何使用 類別讀取和寫入 Unmanaged 記憶體 UnmanagedMemoryStream 。 Unmanaged 記憶體區塊會使用 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 此值應該等於要讀取或保留之資料流程中有效的資料量。 如果寫入資料流程,這個值應該是零。

參數 capacity 會指出可用的記憶體總數。 這個值可以描述長度超過指定長度的區域,或表示可以附加至的區域。 任何超過此值的寫入嘗試都會失敗。

參數會 access CanRead 設定 、 和 CanWrite 屬性。 請注意,指定 Write 不保證資料流程可寫入。 存取參數可讓實作者建立物件,其實作可以符合公開的實際資料流程。

適用於

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

在安全緩衝區中使用指定的位移、長度和檔案存取,初始化 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

包含 Unmanaged 記憶體資料流的緩衝區。

offset
Int64

緩衝區中的位元組位置,Unmanaged 記憶體資料流會在此處開始。

length
Int64

Unmanaged 記憶體資料流的長度。

access
FileAccess

Unmanaged 記憶體資料流的檔案存取模式。

適用於