UnmanagedMemoryStream.Write 方法

定义

重载

名称 说明
Write(ReadOnlySpan<Byte>)

使用提供的字节范围中的数据将字节块写入当前非托管内存流。

Write(Byte[], Int32, Int32)

使用缓冲区中的数据将字节块写入当前流。

Write(ReadOnlySpan<Byte>)

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

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

要写入当前流的字节数。

例外

流已关闭。

基础内存不支持写入。

-或-

尝试写入流,并且属性 CanWritefalse.

-或-

该值 count 大于流的容量。

-或-

该位置位于流容量的末尾。

出现 I/O 错误。

指定的参数之一小于零。

参数 offset 减去参数的 buffer 长度小于 count 参数。

参数 buffernull.

示例

下面的代码示例演示如何使用 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();
    }
}

注解

写入发生在流中的当前位置。

适用于