Stream.Read 方法

定义

重载

Read(Span<Byte>)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。

Read(Byte[], Int32, Int32)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。

Read(Span<Byte>)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。

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>

内存的区域。 当此方法返回时,此区域的内容将替换为从当前源读取的字节。

返回

读入缓冲区中的总字节数。 如果当前有许多字节不可用,则此值可能小于缓冲区的大小;如果缓冲区的长度为零或已到达流的末尾,则此值可能小于缓冲区的大小;如果缓冲区的长度为零,则为 0 (0) 。

注解

CanRead使用 属性确定当前实例是否支持读取。 ReadAsync使用 方法从当前流异步读取。

此方法的实现从当前流中读取最多字节并将其 buffer.Length 存储在 中 buffer。 流中的当前位置按读取的字节数提前;但是,如果发生异常,流中的当前位置保持不变。 实现返回读取的字节数。 如果请求的字节数超过零个,则实现不会完成操作,直到可以读取至少一个字节的数据 (如果请求了零个字节,则某些实现可能类似地不会完成,直到至少有一个字节可用,但在这种情况下,不会从流中使用任何数据) 。 Read 仅当请求了零个字节时,或者当流中没有更多数据并且预期没有更多 ((如关闭的套接字或文件) 结束)时,才返回 0。 实现可以自由返回比请求的字节少,即使尚未到达流的末尾。

用于 BinaryReader 读取基元数据类型。

适用于

Read(Byte[], Int32, Int32)

当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。

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[]

字节数组。 当此方法返回时,此缓冲区包含指定的字符数组,此数组中 offset 和 (offset + count - 1) 之间的值被从当前源中读取的字节所替换。

offset
Int32

buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。

count
Int32

要从当前流中最多读取的字节数。

返回

读入缓冲区中的总字节数。 如果多个字节当前不可用,则此值可能小于请求的字节数;如果 count 为 0 或已到达流的末尾,则为 0 (0) 。

例外

offsetcount 的总和大于缓冲区长度。

buffernull

offsetcount 为负数。

出现 I/O 错误。

流不支持读取。

在流关闭后调用方法。

示例

以下示例演示如何使用 Read 读取数据块。

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使用 属性确定当前实例是否支持读取。 ReadAsync使用 方法从当前流异步读取。

此方法的实现从当前流中读取最多字节, count 并将其存储在 buffer 开头 offset。 流中的当前位置按读取的字节数提前;但是,如果发生异常,流中的当前位置保持不变。 实现返回读取的字节数。 如果请求的字节超过零个,则实现不会完成操作,直到至少可以读取一个字节的数据 (某些实现可能类似地未完成,直到至少一个字节可用,即使请求了零个字节,但在这种情况下,流中不会使用任何数据) 。 Read 仅当请求了零个字节时,或者当流中没有更多数据并且预期没有更多 ((如关闭的套接字或文件) 结束)时,才返回 0。 实现可以自由返回比请求的字节少,即使尚未到达流的末尾。

用于 BinaryReader 读取基元数据类型。

另请参阅

适用于