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 값이 0보다 작은 경우

또는

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 새 instance 만들고 기본적으로 속성을 로 설정하고 속성을 falseCanReadtrue로 설정합니다.CanWrite 속성은 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 값이 0보다 작은 경우

또는

capacity 값이 0보다 작은 경우

또는

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 읽거나 보존할 스트림의 유효한 데이터 양과 같아야 합니다. 스트림에 쓰는 경우 이 값은 0이어야 합니다.

매개 변수는 capacity 사용 가능한 총 메모리 양을 나타냅니다. 이 값은 지정된 길이보다 긴 지역을 설명하거나 추가할 수 있는 지역을 나타낼 수 있습니다. 이 값을 초과하여 쓰려는 시도는 실패합니다.

매개 변수는 access , 및 CanWrite 속성을 설정합니다CanRead. 를 지정해 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

관리되지 않는 메모리 스트림에 대한 파일 액세스 모드입니다.

적용 대상