BufferedStream.Read Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Overload
| Nome | Descrizione |
|---|---|
| Read(Span<Byte>) |
Copia i byte dal flusso memorizzato nel buffer corrente in un intervallo di byte e sposta in avanti la posizione all'interno del flusso memorizzato nel buffer in base al numero di byte letti. |
| Read(Byte[], Int32, Int32) |
Copia i byte dal flusso memorizzato nel buffer corrente in una matrice. |
Read(Span<Byte>)
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
Copia i byte dal flusso memorizzato nel buffer corrente in un intervallo di byte e sposta in avanti la posizione all'interno del flusso memorizzato nel buffer in base al numero di byte letti.
public:
override int Read(Span<System::Byte> destination);
public override int Read(Span<byte> destination);
override this.Read : Span<byte> -> int
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
Parametri
Area di memoria. Quando termina, il contenuto di questa area viene sostituito dai byte letti dall'origine corrente.
Restituisce
Numero totale di byte letti nel buffer. Può essere minore del numero di byte allocati nel buffer se molti byte non sono attualmente disponibili oppure zero (0) se è stata raggiunta la fine del flusso.
Commenti
Utilizzare la CanRead proprietà per determinare se l'istanza corrente supporta la lettura. Usare il ReadAsync metodo per leggere in modo asincrono dal flusso corrente.
Le implementazioni di questo metodo leggono un massimo di buffer.Length byte dal flusso corrente e le archivia in buffer. La posizione corrente all'interno del flusso è avanzata dal numero di byte letti; Tuttavia, se si verifica un'eccezione, la posizione corrente all'interno del flusso rimane invariata. Le implementazioni restituiscono il numero di byte letti. L'implementazione verrà bloccata fino a quando non sarà possibile leggere almeno un byte di dati, nel caso in cui non siano disponibili dati.
Read restituisce 0 solo quando non sono presenti più dati nel flusso e non è più previsto (ad esempio un socket chiuso o la fine del file). Un'implementazione è libera di restituire meno byte di quanto richiesto anche se la fine del flusso non è stata raggiunta.
Usare BinaryReader per la lettura dei tipi di dati primitivi.
Si applica a
Read(Byte[], Int32, Int32)
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
- Origine:
- BufferedStream.cs
Copia i byte dal flusso memorizzato nel buffer corrente in una matrice.
public:
override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public:
override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public override int Read(byte[] buffer, int offset, int count);
public override int Read(byte[] array, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
Parametri
- bufferarray
- Byte[]
- offset
- Int32
Offset di byte nel buffer in corrispondenza del quale iniziare la lettura dei byte.
- count
- Int32
Numero di byte da leggere.
Restituisce
Numero totale di byte letti in array. Può essere minore del numero di byte richiesti se il numero di byte non è attualmente disponibile o 0 se la fine del flusso è stata raggiunta prima che tutti i dati possano essere letti.
Eccezioni
La lunghezza di meno offset è minore di .arraycount
array è null.
offset o count è negativo.
Il flusso non è aperto o è null.
Il flusso non supporta la lettura.
I metodi sono stati chiamati dopo la chiusura del flusso.
Esempio
Questo esempio di codice fa parte di un esempio più ampio fornito per la BufferedStream classe .
// Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.");
bytesReceived = 0;
startTime = DateTime.Now;
int numBytesToRead = receivedData.Length;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = bufStream.Read(receivedData,0, receivedData.Length);
// The end of the file is reached.
if (n == 0)
break;
bytesReceived += n;
numBytesToRead -= n;
}
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",
bytesReceived.ToString(),
bufferedTime.ToString("F1"));
// Receive data using the BufferedStream.
printfn "Receiving data using BufferedStream."
bytesReceived <- 0
let startTime = DateTime.Now
let mutable numBytesToRead = receivedData.Length
let mutable broken = false
while not broken && numBytesToRead > 0 do
// Read may return anything from 0 to numBytesToRead.
let n = bufStream.Read(receivedData,0, receivedData.Length)
// The end of the file is reached.
if n = 0 then
broken <- true
else
bytesReceived <- bytesReceived + n
numBytesToRead <- numBytesToRead - n
let bufferedTime = (DateTime.Now - startTime).TotalSeconds
printfn $"{bytesReceived} bytes received in {bufferedTime:F1} seconds.\n"
' Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.")
bytesReceived = 0
startTime = DateTime.Now
Dim numBytesToRead As Integer = receivedData.Length
Dim n As Integer
Do While numBytesToRead > 0
'Read my return anything from 0 to numBytesToRead
n = bufStream.Read(receivedData, 0, receivedData.Length)
'The end of the file is reached.
If n = 0 Then
Exit Do
End If
bytesReceived += n
numBytesToRead -= n
Loop
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes received in {1} " & _
"seconds." & vbCrLf, _
bytesReceived.ToString(), _
bufferedTime.ToString("F1"))
Commenti
Il Read metodo restituirà 0 solo se viene raggiunta la fine del flusso. In tutti gli altri casi, Read legge sempre almeno un byte dal flusso prima di restituire. Per definizione, se non sono disponibili dati dal flusso in una chiamata a Read, il Read metodo restituisce 0 (la fine del flusso viene raggiunta automaticamente). Un'implementazione è libera di restituire meno byte di quanto richiesto anche se la fine del flusso non è stata raggiunta.
Usare BinaryReader per la lettura dei tipi di dati primitivi.
Vedi anche
- BlockCopy(Array, Int32, Array, Int32, Int32)
- CanRead
- Write(Byte[], Int32, Int32)
- I/O di file e di flussi
- Procedura: Leggere testo da un file
- Procedura: Scrivere testo in un file