UnmanagedMemoryStream.Read 메서드

정의

오버로드

Read(Span<Byte>)

이 비관리형 메모리 스트림의 모든 바이트를 지정된 바이트 범위로 읽어들입니다.

Read(Byte[], Int32, Int32)

지정된 바이트 수를 지정된 배열로 읽어 들입니다.

Read(Span<Byte>)

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

이 비관리형 메모리 스트림의 모든 바이트를 지정된 바이트 범위로 읽어들입니다.

public:
 override int Read(Span<System::Byte> destination);
public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> destination);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
override this.Read : Span<byte> -> int
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

매개 변수

destinationbuffer
Span<Byte>

이 메서드가 반환될 때 이 범위에는 비관리형 메모리 스트림의 모든 바이트가 포함됩니다.

반환

대상으로 읽어들인 총 바이트 수입니다.

적용 대상

Read(Byte[], Int32, Int32)

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

지정된 바이트 수를 지정된 배열로 읽어 들입니다.

public:
 override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

매개 변수

buffer
Byte[]

이 메서드는 지정된 바이트 배열의 값이 offset과 (offset + count - 1) 사이에서 현재 원본으로부터 읽어온 바이트로 교체된 상태로 반환됩니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

offset
Int32

현재 스트림에서 읽은 데이터를 저장하기 시작하는 buffer의 바이트 오프셋(0부터 시작)입니다.

count
Int32

현재 스트림에서 읽을 최대 바이트 수입니다.

반환

버퍼로 읽어온 총 바이트 수입니다. 이 바이트 수는 현재 바이트가 충분하지 않은 경우 요청된 바이트 수보다 작을 수 있으며 스트림의 끝에 도달하면 0이 됩니다.

예외

스트림이 닫혔습니다.

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

또는

CanRead 속성은 false로 설정됩니다.

buffer 매개 변수가 null로 설정된 경우

offset 매개 변수가 0보다 작습니다.

또는

count 매개 변수가 0보다 작습니다.

버퍼 배열 길이에서 offset 매개 변수를 뺀 값이 count 매개 변수보다 작은 경우

예제

다음 코드 예제에서 읽고 클래스를 사용 하 여 관리 되지 않는 메모리에 쓰는 방법을 보여 줍니다 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();
    }
}

설명

매개 변수는 offset 읽기를 시작할 매개 변수(버퍼 인덱스)의 바이트 array 오프셋을 제공하고 매개 count 변수는 이 스트림에서 읽을 최대 바이트 수를 제공합니다. 반환된 값은 읽은 실제 바이트 수이거나 스트림의 끝에 도달하면 0입니다. 읽기 작업이 성공하면 스트림의 현재 위치는 읽은 바이트 수만큼 진행됩니다. 예외가 발생하면 스트림의 현재 위치는 변경되지 않습니다.

메서드는 Read 스트림의 끝에 도달한 후에만 0을 반환합니다. 그렇지 않으면 Read 반환하기 전에 항상 스트림에서 하나 이상의 바이트를 읽습니다. 를 호출 Read할 때 스트림에서 사용할 수 있는 데이터가 없는 경우 메서드는 하나 이상의 데이터 바이트를 반환할 때까지 차단됩니다. 구현은 스트림의 끝에 도달하지 않은 경우에도 요청된 것보다 적은 바이트를 반환할 수 있습니다.

적용 대상