UnmanagedMemoryStream.Read Metodo

Definizione

Overload

Read(Span<Byte>)

Legge tutti i byte di questo flusso di memoria non gestita nell'intervallo di byte specificato.

Read(Byte[], Int32, Int32)

Legge il numero di byte specificato in una matrice specificata.

Read(Span<Byte>)

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

Legge tutti i byte di questo flusso di memoria non gestita nell'intervallo di byte specificato.

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

Parametri

destinationbuffer
Span<Byte>

Quando questo metodo viene restituito, l'intervallo contiene tutti i byte provenienti dal flusso di memoria non gestita.

Restituisce

Numero complessivo di byte letti nella destinazione.

Si applica a

Read(Byte[], Int32, Int32)

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

Legge il numero di byte specificato in una matrice specificata.

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

Parametri

buffer
Byte[]

Quando questo metodo viene restituito, contiene la matrice di byte specificata con i valori compresi tra offset e (offset + count - 1) sostituiti con i byte letti dall'origine corrente. Questo parametro viene passato non inizializzato.

offset
Int32

Offset dei byte in base zero in buffer in corrispondenza del quale iniziare l'archiviazione dei dati letti dal flusso corrente.

count
Int32

Numero massimo di byte da leggere dal flusso corrente.

Restituisce

Numero complessivo di byte letti nel buffer. È possibile che questo numero sia inferiore a quello dei byte richiesti se la quantità di byte disponibili è minore oppure che corrisponda a zero (0) se è stata raggiunta la fine del flusso.

Eccezioni

Il flusso è chiuso.

La memoria sottostante non supporta la lettura.

-oppure-

La proprietà CanRead è impostata su false.

Il parametro buffer viene impostato su null.

Il parametro offset è minore di zero.

-oppure-

Il parametro count è minore di zero.

La lunghezza della matrice del buffer meno il parametro offset è minore del parametro count.

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 offset parametro fornisce l'offset del byte nel array parametro (indice buffer) in corrispondenza del quale iniziare la lettura e il count parametro fornisce il numero massimo di byte da leggere da questo flusso. Il valore restituito è il numero effettivo di byte letti o zero se viene raggiunta la fine del flusso. Se l'operazione di lettura ha esito positivo, la posizione corrente del flusso è avanzata dal numero di byte letti. Se si verifica un'eccezione, la posizione corrente del flusso è invariata.

Il Read metodo restituisce zero solo dopo aver raggiunto la fine del flusso. In caso contrario, Read legge sempre almeno un byte dal flusso prima di restituire. Se non sono disponibili dati dal flusso dopo una chiamata a , il metodo blocca fino a Readquando non è possibile restituire almeno un byte di dati. Un'implementazione è gratuita per restituire meno byte rispetto alla richiesta anche se la fine del flusso non è stata raggiunta.

Si applica a