BufferedStream.Read(Byte[], Int32, Int32) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Copia bytes de la secuencia almacenada en búfer actual en una matriz.
public:
override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public override int Read(byte[] array, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
Parámetros
- array
- Byte[]
Búfer al que se van a copiar los bytes.
- offset
- Int32
Desplazamiento de bytes en el búfer en el que se van a empezar a leer bytes.
- count
- Int32
Número de bytes que se van a leer.
Devoluciones
Número total de bytes leídos en array. Puede ser menor que el número de bytes solicitado si no hay muchos bytes disponibles actualmente, o 0 si se ha alcanzado el final de la secuencia antes de que se puedan leer los datos.
Excepciones
La longitud de array menos offset es menor que count.
array es null.
offset o count es negativo.
La secuencia no está abierta o es null.
La secuencia no admite la lectura.
Se llamó a los métodos después de cerrar la secuencia.
Ejemplos
Este ejemplo de código forma parte de un ejemplo más grande proporcionado para la BufferedStream clase .
// 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"))
Comentarios
El Read método devolverá 0 solo si se alcanza el final de la secuencia. En todos los demás casos, Read siempre lee al menos un byte de la secuencia antes de devolverlo. Por definición, si no hay datos disponibles desde la secuencia tras una llamada a Read, el Read método devuelve 0 (el final de la secuencia se alcanza automáticamente). Una implementación es libre para devolver menos bytes de los solicitados incluso si no se ha alcanzado el final de la secuencia.
Se usa BinaryReader para leer tipos de datos primitivos.