Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
This topic has not yet been rated - Rate this topic

Stream.Read Method

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.

[Visual Basic]
Public MustOverride Function Read( _
   <InteropServices.In(), _
   Out()> ByVal buffer() As Byte, _
   ByVal offset As Integer, _
   ByVal count As Integer _
) As Integer
[C#]
public abstract int Read(
   [
   In,
   Out
] byte[] buffer,
 int offset,
 int count
);
[C++]
public: virtual int Read(
   [
   In,
   Out
] unsigned char buffer __gc[],
 int offset,
 int count
) = 0;
[JScript]
public abstract function Read(
   buffer : Byte[],
 offset : int,
 count : int
) : int;

Parameters

buffer
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.
offset
The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
count
The maximum number of bytes to be read from the current stream.

Return Value

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.

Exceptions

Exception Type Condition
ArgumentException The sum of offset and count is larger than the buffer length.
ArgumentNullException buffer is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException offset or count is negative.
IOException An I/O error occurs.
NotSupportedException The stream does not support reading.
ObjectDisposedException Methods were called after the stream was closed.

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

Use the CanRead property to determine whether the current instance supports reading.

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 return value is zero only if the position is currently at the end of the stream. 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.

Example

[Visual Basic, C#, C++] The following example shows how to use Read to read a block of data.

[Visual Basic] 
Imports System
Imports System.IO
Imports Microsoft.VisualBasic

Public Class Block

    Public Shared Sub Main()
        Dim s As New MemoryStream()
        Dim i As Integer
        For i = 0 To 99
            s.WriteByte(CByte(i))
        Next i
        s.Position = 0

        ' Now read in s into a byte buffer.
        Dim bytes(1000) As Byte
        Dim numBytesToRead As Integer = CInt(s.Length)
        Dim numBytesRead As Integer = 0
        While numBytesToRead > 0
            ' Read can return anything from 0 to numBytesToRead.
            Dim n As Integer = s.Read(bytes, numBytesRead, numBytesToRead)
            ' The end of the file has been reached.
            If n = 0 Then
                Exit While
            End If
            numBytesRead += n
            numBytesToRead -= n
        End While
        s.Close()
        ' numBytesToRead should be 0 now, and numBytesRead should
        ' equal 100.
        Console.WriteLine("number of bytes read: " & numBytesRead.ToString())
    End Sub
End Class

[C#] 
using System;
using System.IO;

public class Block
{
    public static void Main()
    {
        Stream s = new MemoryStream();
        for (int i=0; i<100; i++)
            s.WriteByte((byte)i);
        s.Position = 0;
 
        // Now read s into a byte buffer.
        byte[] bytes = new byte[1000];
        int numBytesToRead = (int) s.Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0) 
        {
            // Read may return anything from 0 to numBytesToRead.
            int n = s.Read(bytes, numBytesRead, numBytesToRead);
            // The end of the file is reached.
            if (n==0)
                break;
            numBytesRead += n;
            numBytesToRead -= n;
        }
        s.Close();
        // numBytesToRead should be 0 now, and numBytesRead should
        // equal 100.
        Console.WriteLine("number of bytes read: "+numBytesRead);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int main()
{
    Stream* s = new MemoryStream();
    for (int i=0; i<100; i++)
        s->WriteByte((Byte)i);
    s->Position = 0;

    // Now read s into a byte buffer.
    Byte bytes[] = new Byte[1000];
    int numBytesToRead = (int) s->Length;
    int numBytesRead = 0;
    while (numBytesToRead > 0) 
    {
        // Read may return anything from 0 to numBytesToRead.
        int n = s->Read(bytes, numBytesRead, numBytesToRead);
        // The end of the file is reached.
        if (n==0)
            break;
        numBytesRead += n;
        numBytesToRead -= n;
    }
    s->Close();
    // numBytesToRead should be 0 now, and numBytesRead should
    // equal 100.
    Console::WriteLine(S"number of bytes read: {0}", __box(numBytesRead));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Stream Class | Stream Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.