UnmanagedMemoryStream.Read 메서드

정의

오버로드

Name Description
Read(Span<Byte>)

이 관리되지 않는 메모리 스트림의 모든 바이트를 지정된 바이트 범위로 읽습니다.

Read(Byte[], Int32, Int32)

지정된 바이트 수를 지정된 배열로 읽습니다.

Read(Span<Byte>)

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

이 관리되지 않는 메모리 스트림의 모든 바이트를 지정된 바이트 범위로 읽습니다.

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

매개 변수

destinationbuffer
Span<Byte>

이 메서드가 반환되면 이 범위에는 관리되지 않는 메모리 스트림의 모든 바이트가 포함됩니다.

반품

대상으로 읽은 총 바이트 수입니다.

적용 대상

Read(Byte[], Int32, Int32)

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

이 메서드가 반환될 때 현재 소스에서 읽은 바이트로 대체된 값과 (offsetoffset + - 1) 사이의 count 값을 가진 지정된 바이트 배열을 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

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시 스트림에서 사용할 수 있는 데이터가 없는 경우 적어도 1 바이트의 데이터를 반환할 수 있을 때까지 메서드가 차단됩니다. 구현은 스트림의 끝에 도달하지 않은 경우에도 요청된 것보다 적은 바이트를 반환할 수 있습니다.

적용 대상