Stream.Read 方法

定义

重载

Read(Span<Byte>)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。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.

Read(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.

Read(Span<Byte>)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。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.

public:
 virtual int Read(Span<System::Byte> buffer);
public virtual int Read (Span<byte> buffer);
abstract member Read : Span<byte> -> int
override this.Read : Span<byte> -> int
Public Overridable Function Read (buffer As Span(Of Byte)) As Integer

参数

buffer
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.

返回

读入缓冲区中的总字节数。The total number of bytes read into the buffer. 如果很多字节当前不可用,则这可小于在缓冲区中分配的字节数;如果已到达流结尾,则为零 (0)。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.

注解

CanRead使用属性可确定当前实例是否支持读取。Use the CanRead property to determine whether the current instance supports reading. ReadAsync使用方法从当前流中异步读取。Use the ReadAsync method to read asynchronously from the current stream.

此方法的实现从当前流中buffer.Length读取最多字节, 并将它们存储buffer在中。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仅当流中没有更多的数据, 但不需要更多的数据时 (如闭合套接字或文件尾), 才返回0。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.

BinaryReader用于读取基元数据类型。Use BinaryReader for reading primitive data types.

Read(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.

public:
 abstract int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public abstract int Read (byte[] buffer, int offset, int count);
abstract member Read : byte[] * int * int -> int
Public MustOverride Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer

参数

buffer
Byte[]

字节数组。An array of bytes. 当此方法返回时,此缓冲区包含指定的字符数组,此数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节所替换。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
Int32

buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count
Int32

要从当前流中最多读取的字节数。The maximum number of bytes to be read from the current stream.

返回

读入缓冲区中的总字节数。The total number of bytes read into the buffer. 如果很多字节当前不可用,则总字节数可能小于请求的字节数;如果已到达流结尾,则为零 (0)。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.

异常

offsetcount 的总和大于缓冲区长度。The sum of offset and count is larger than the buffer length.

buffernullbuffer is null.

offsetcount 为负数。offset or count is negative.

出现 I/O 错误。An I/O error occurs.

流不支持读取。The stream does not support reading.

在流关闭后调用方法。Methods were called after the stream was closed.

示例

下面的示例演示如何使用Read读取数据块。The following example shows how to use Read to read a block of data.

using namespace System;
using namespace System::IO;

public ref class Block
{
public:
    static void Main()
    {
        Stream^ s = gcnew MemoryStream();
        for (int i = 0; i < 100; i++)
        {
            s->WriteByte((Byte)i);
        }
        s->Position = 0;

        // Now read s into a byte buffer.
        array<Byte>^ bytes = gcnew array<Byte>(s->Length);
        int numBytesToRead = (int) s->Length;
        int numBytesRead = 0;
        while (numBytesToRead > 0)
        {
            // Read may return anything from 0 to 10.
            int n = s->Read(bytes, numBytesRead, 10);
            // 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: {0:d}", numBytesRead);
    }
};

int main()
{
    Block::Main();
}
using System;
using System.IO;

public class Block
{
    public static void Main()
    {
        Stream s = new MemoryStream();
        for (int i = 0; i < 122; i++)
        {
            s.WriteByte((byte)i);
        }
        s.Position = 0;

        // Now read s into a byte buffer with a little padding.
        byte[] bytes = new byte[s.Length + 10];
        int numBytesToRead = (int)s.Length;
        int numBytesRead = 0;
        do
        {
            // Read may return anything from 0 to 10.
            int n = s.Read(bytes, numBytesRead, 10);
            numBytesRead += n;
            numBytesToRead -= n;
        } while (numBytesToRead > 0);
        s.Close();
       
        Console.WriteLine("number of bytes read: {0:d}", numBytesRead);
    }
}
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
        Do
            ' 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

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

注解

CanRead使用属性可确定当前实例是否支持读取。Use the CanRead property to determine whether the current instance supports reading. ReadAsync使用方法从当前流中异步读取。Use the ReadAsync method to read asynchronously from the current stream.

此方法的实现从当前流中count读取最多字节,并将它们存储buffer在开始offset处。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. Read仅当流中没有更多的数据, 但不需要更多的数据时 (如闭合套接字或文件尾), 才返回0。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.

BinaryReader用于读取基元数据类型。Use BinaryReader for reading primitive data types.

另请参阅

适用于