UnmanagedMemoryStream.Write 메서드

정의

오버로드

Write(ReadOnlySpan<Byte>)

제공된 바이트 범위에서 데이터를 사용하여 현재 비관리형 메모리 스트림에 바이트 블록을 씁니다.

Write(Byte[], Int32, Int32)

버퍼의 데이터를 사용하여 현재 스트림에 바이트 블록을 씁니다.

Write(ReadOnlySpan<Byte>)

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

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

현재 스트림에 쓸 바이트 수입니다.

예외

스트림이 닫혔습니다.

내부 메모리가 쓰기를 지원하지 않는 경우

또는

스트림에 쓰려고 하는데 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();
    }
}

설명

쓰기는 스트림의 현재 위치에서 발생합니다.

적용 대상