UnmanagedMemoryStream Costruttori

Definizione

Inizializza una nuova istanza della classe UnmanagedMemoryStream.

Overload

UnmanagedMemoryStream()

Inizializza una nuova istanza della classe UnmanagedMemoryStream.

UnmanagedMemoryStream(Byte*, Int64)

Inizializza una nuova istanza della classe UnmanagedMemoryStream usando la posizione e la dimensione di memoria specificate.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

Inizializza una nuova istanza della classe UnmanagedMemoryStream in un buffer sicuro, con un valore specificato di offset e lunghezza.

UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

Inizializza una nuova istanza della classe UnmanagedMemoryStream usando la posizione, la lunghezza e la quantità totale di memoria e i valori di accesso ai file specificati.

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

Inizializza una nuova istanza della classe UnmanagedMemoryStream in un buffer sicuro, con un valore specificato di offset, lunghezza e accesso ai file.

UnmanagedMemoryStream()

Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs

Inizializza una nuova istanza della classe UnmanagedMemoryStream.

protected:
 UnmanagedMemoryStream();
protected UnmanagedMemoryStream ();
Protected Sub New ()

Eccezioni

L'utente non dispone dell'autorizzazione richiesta.

Si applica a

UnmanagedMemoryStream(Byte*, Int64)

Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs

Importante

Questa API non è conforme a CLS.

Inizializza una nuova istanza della classe UnmanagedMemoryStream usando la posizione e la dimensione di memoria specificate.

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

Parametri

pointer
Byte*

Puntatore a una posizione di memoria non gestita.

length
Int64

Lunghezza della memoria da usare.

Attributi

Eccezioni

L'utente non dispone dell'autorizzazione richiesta.

Il valore pointer è null.

Il valore length è minore di zero.

-oppure-

Il valore length è sufficientemente grande da causare un overflow.

Esempio

Nell'esempio di codice seguente viene illustrato come leggere e scrivere in memoria non gestita usando la UnmanagedMemoryStream classe . Un blocco di memoria non gestita viene allocato e de-allocato usando la Marshal classe .

// 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));
    }
}

Commenti

Questo costruttore crea una nuova istanza della UnmanagedMemoryStream classe e per impostazione predefinita imposta la CanWrite proprietà su false e la CanRead proprietà su true. La Length proprietà è impostata sul valore del length parametro e non può essere modificata.

Si applica a

UnmanagedMemoryStream(SafeBuffer, Int64, Int64)

Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs

Inizializza una nuova istanza della classe UnmanagedMemoryStream in un buffer sicuro, con un valore specificato di offset e lunghezza.

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)

Parametri

buffer
SafeBuffer

Buffer che deve contenere il flusso di memoria non gestita.

offset
Int64

Posizione di byte nel buffer in corrispondenza della quale avviare il flusso di memoria non gestita.

length
Int64

Lunghezza del flusso di memoria non gestita.

Si applica a

UnmanagedMemoryStream(Byte*, Int64, Int64, FileAccess)

Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs

Importante

Questa API non è conforme a CLS.

Inizializza una nuova istanza della classe UnmanagedMemoryStream usando la posizione, la lunghezza e la quantità totale di memoria e i valori di accesso ai file specificati.

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

Parametri

pointer
Byte*

Puntatore a una posizione di memoria non gestita.

length
Int64

Lunghezza della memoria da usare.

capacity
Int64

Quantità totale di memoria assegnata al flusso.

access
FileAccess

Uno dei valori di FileAccess.

Attributi

Eccezioni

L'utente non dispone dell'autorizzazione richiesta.

Il valore pointer è null.

Il valore length è minore di zero.

-oppure-

Il valore capacity è minore di zero.

-oppure-

Il valore length è maggiore del valore capacity.

Esempio

Nell'esempio di codice seguente viene illustrato come leggere e scrivere in memoria non gestita usando la UnmanagedMemoryStream classe . Un blocco di memoria non gestita viene allocato e de-allocato usando la Marshal classe .


// 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();
    }
}

Commenti

Il length parametro definisce la quantità corrente di memoria in uso. Se la lettura o l'aggiunta di dati al flusso, il length valore deve essere uguale alla quantità di dati validi nel flusso da cui leggere o conservare. Se si scrive nel flusso, questo valore deve essere zero.

Il capacity parametro indica la quantità di memoria totale disponibile. Questo valore può descrivere un'area più lunga della lunghezza specificata o indicare un'area a cui è possibile accodare. Qualsiasi tentativo di scrittura oltre questo valore avrà esito negativo.

Il access parametro imposta le CanReadproprietà , e CanWrite . Si noti che la specifica Write non garantisce che il flusso sia scrivibile. I parametri di accesso consentono all'implementatore di creare un oggetto la cui implementazione può corrispondere al flusso effettivo esposto.

Si applica a

UnmanagedMemoryStream(SafeBuffer, Int64, Int64, FileAccess)

Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs
Origine:
UnmanagedMemoryStream.cs

Inizializza una nuova istanza della classe UnmanagedMemoryStream in un buffer sicuro, con un valore specificato di offset, lunghezza e accesso ai file.

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)

Parametri

buffer
SafeBuffer

Buffer che deve contenere il flusso di memoria non gestita.

offset
Int64

Posizione di byte nel buffer in corrispondenza della quale avviare il flusso di memoria non gestita.

length
Int64

Lunghezza del flusso di memoria non gestita.

access
FileAccess

La modalità di accesso ai file al flusso di memoria non gestito.

Si applica a