Stream.Read 方法

定義

多載

Read(Span<Byte>)

當在衍生類別中覆寫時,自目前資料流讀取一連串的位元組,並依所讀取的位元組數目進階資料流中的位置。

Read(Byte[], Int32, Int32)

當在衍生類別中覆寫時,自目前資料流讀取一連串的位元組,並依所讀取的位元組數目進階資料流中的位置。

Read(Span<Byte>)

Source:
Stream.cs
Source:
Stream.cs
Source:
Stream.cs

當在衍生類別中覆寫時,自目前資料流讀取一連串的位元組,並依所讀取的位元組數目進階資料流中的位置。

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) 。

備註

CanRead使用 屬性來判斷目前的實例是否支援讀取。 ReadAsync使用 方法,從目前的資料流程非同步讀取。

這個方法的實作會從目前的資料流程讀取最大位元組, buffer.Length 並將其儲存在 buffer 中。 資料流程中的目前位置會依讀取的位元組數目進階;不過,如果發生例外狀況,資料流程中的目前位置會保持不變。 實作會傳回讀取的位元組數目。 如果要求超過零個位元組,則實作在至少可以讀取一個位元組的資料之前,不會完成作業, (如果要求零個位元組,某些實作在至少有一個位元組可用之前可能尚未完成,但在這種情況下,不會從資料流程取用任何資料) 。 Read 只有在要求零個位元組或資料流程中沒有其他資料時,才會傳回 0,而且預期不會再傳回 (0,例如關閉的通訊端或檔案) 的結尾。 即使尚未到達資料流程結尾,實作仍可傳回比要求的位元組少。

用於 BinaryReader 讀取基本資料類型。

適用於

Read(Byte[], Int32, Int32)

Source:
Stream.cs
Source:
Stream.cs
Source:
Stream.cs

當在衍生類別中覆寫時,自目前資料流讀取一連串的位元組,並依所讀取的位元組數目進階資料流中的位置。

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) 。

例外狀況

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 ,並從 開始 offset 加以儲存 buffer 。 資料流程中的目前位置會依讀取的位元組數目進階;不過,如果發生例外狀況,資料流程中的目前位置會保持不變。 實作會傳回讀取的位元組數目。 如果要求超過零個位元組,除非可以讀取至少一個位元組的資料,否則實作將不會完成此作業, (某些實作在至少一個位元組可供使用之前,即使要求零個位元組,但在這種情況下,不會從資料流程取用任何資料) 。 Read 只有在要求零個位元組或資料流程中沒有其他資料時,才會傳回 0,而且預期不會再傳回 (0,例如關閉的通訊端或檔案) 的結尾。 即使尚未到達資料流程結尾,實作仍可傳回比要求的位元組少。

用於 BinaryReader 讀取基本資料類型。

另請參閱

適用於