Stream.Read Method (Byte(), Int32, Int32)


When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

Namespace:   System.IO
Assembly:  mscorlib (in mscorlib.dll)

Public MustOverride Function Read (
	buffer As Byte(),
	offset As Integer,
	count As Integer
) As Integer


Type: System.Byte()

An array of bytes. When this method returns, the buffer contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source.

Type: System.Int32

The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

Type: System.Int32

The maximum number of bytes to be read from the current stream.

Return Value

Type: System.Int32

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

Exception Condition

The sum of offset and count is larger than the buffer length.


buffer is null.


offset or count is negative.


An I/O error occurs.


The stream does not support reading.


Methods were called after the stream was closed.

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 count bytes from the current stream and store them in buffer beginning at offset. 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. Readreturns 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.

The following example shows how to use Read to read a block of data.

Imports System
Imports System.IO

Public Class Block
    Public Shared Sub Main()
        Dim s As Stream = New MemoryStream()
        For i As Integer = 0 To 121
            s.WriteByte(CType(i, Byte))
        Next i
        s.Position = 0

        ' Now read s into a byte buffer that is padded slightly.
        Dim bytes(s.Length + 10) As Byte
        Dim numBytesToRead As Integer = s.Length
        Dim numBytesRead As Integer = 0
        Dim n As Integer
            ' Read may return anything from 0 to 10.
            n = s.Read(bytes, numBytesRead, 10)
            ' The end of the file is reached.
            numBytesRead += n
            numBytesToRead -= n
        Loop While numBytesToRead > 0


        Console.WriteLine("number of bytes read: {0:d}", numBytesRead)
    End Sub
End Class

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft