BufferedStream.Read Method

Definition

Overloads

Read(Span<Byte>)

Copies bytes from the current buffered stream to a byte span and advances the position within the buffered stream by the number of bytes read.

Read(Byte[], Int32, Int32)

Copies bytes from the current buffered stream to an array.

Read(Span<Byte>)

Copies bytes from the current buffered stream to a byte span and advances the position within the buffered stream by the number of bytes read.

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

Parameters

destination
Span<Byte>

A region of memory. When this method returns, the contents of this region are replaced by the bytes read from the current source.

Returns

The total number of bytes read into the buffer. This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

Remarks

Use the CanRead property to determine whether the current instance supports reading. Use the ReadAsync method to read asynchronously from the current stream.

Implementations of this method read a maximum of buffer.Length bytes from the current stream and store them in buffer. The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. Implementations return the number of bytes read. The implementation will block until at least one byte of data can be read, in the event that no data is available. Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

Read(Byte[], Int32, Int32)

Copies bytes from the current buffered stream to an array.

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

Parameters

array
Byte[]

The buffer to which bytes are to be copied.

offset
Int32

The byte offset in the buffer at which to begin reading bytes.

count
Int32

The number of bytes to be read.

Returns

The total number of bytes read into array. This can be less than the number of bytes requested if that many bytes are not currently available, or 0 if the end of the stream has been reached before any data can be read.

Exceptions

Length of array minus offset is less than count.

array is null.

offset or count is negative.

The stream is not open or is null.

The stream does not support reading.

Methods were called after the stream was closed.

Examples

This code example is part of a larger example provided for the BufferedStream class.

// Receive data using the BufferedStream.
Console::WriteLine(  "Receiving data using BufferedStream." );
bytesReceived = 0;
startTime = DateTime::Now;
while ( bytesReceived < numberOfLoops * receivedData->Length )
{
   bytesReceived += bufStream->Read( receivedData, 0, receivedData->Length );
}

bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes received in {1} seconds.\n", bytesReceived.ToString(), bufferedTime.ToString(  "F1" ) );

// 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.
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"))

Remarks

The Read method will return 0 only if the end of the stream is reached. In all other cases, Read always reads at least one byte from the stream before returning. By definition, if no data is available from the stream upon a call to Read, the Read method returns 0 (the end of the stream is reached automatically). An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

Use BinaryReader for reading primitive data types.

See also

Applies to