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

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

初始化 UnmanagedMemoryStream 类的新实例。

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

例外

用户没有所必需的权限。

适用于

UnmanagedMemoryStream(Byte*, Int64)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
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 值小于零。

- 或 -

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 ,默认情况下将 CanWrite 属性设置为 false ,将 CanRead 属性设置为 true。 属性 Length 设置为 参数的值 length ,无法更改。

适用于

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
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)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
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 值之一。

属性

例外

用户没有所必需的权限。

pointer 值为 null

length 值小于零。

- 或 -

capacity 值小于零。

- 或 -

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 应等于流中要读取或保留的有效数据量。 如果写入流,此值应为零。

参数 capacity 指示可用总内存量。 此值可以描述长于指定长度的区域,或指示可追加到的区域。 超出此值的任何写入尝试都将失败。

参数 access 设置 CanRead、 和 CanWrite 属性。 请注意,指定 Write 并不能保证流是可写的。 访问参数允许实现者创建一个对象,其实现可以匹配公开的实际流。

适用于

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
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

非托管内存流的文件访问模式。

适用于