Stream.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Read(Span<Byte>) |
當在派生類別中覆寫時,會從目前串流讀取一串位元組,並依讀取位元組數將該序列推進。 |
| Read(Byte[], Int32, Int32) |
當在派生類別中覆寫時,會從目前串流讀取一串位元組,並依讀取位元組數將該序列推進。 |
Read(Span<Byte>)
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- 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
參數
傳回
讀取到緩衝區的總位元組數。 如果目前沒有那麼多位元組可用,這個值可以小於緩衝區大小;若緩衝區長度為零或已到達串流結束,則可為零(0)。
備註
利用該 CanRead 屬性判斷目前實例是否支援讀取。 使用該 ReadAsync 方法非同步讀取目前串流。
此方法的實作最多可從當前串流讀取 buffer.Length 位元組並儲存在 buffer。 串流中的當前位置會依讀取的位元組數前進;但若發生例外,串流中當前位置保持不變。 實作會回傳讀取的位元組數。 若請求超過零位元組,實作不會完成操作,直到至少讀取一個位元組資料(若請求為零位元組,某些實作可能直到至少一個位元組未完成,但此時串流不會消耗資料)。
Read 只有當請求零位元組或串流中沒有更多資料且預期不再有資料(例如封閉套接字或檔案結束)時,才回傳 0。 即使串流尚未到達終點,實作也可以自由回傳比請求的位元組少。
用於 BinaryReader 讀取原始資料型態。
適用於
Read(Byte[], Int32, Int32)
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- Stream.cs
- 來源:
- 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
目前串流中最大可讀取的位元組數。
傳回
讀取到緩衝區的總位元組數。 若目前沒有該數量的位元組可用,請求位元組數可小於此;若為 0 或已到達串流終點,則為零(0)。count
例外狀況
與count的和offset大於緩衝區長度。
buffer 為 null。
offset 或 count 是陰性。
發生 I/O 錯誤。
串流不支援閱讀。
方法在溪流關閉後才被呼叫。
範例
以下範例說明如何使用 Read 讀取資料區塊。
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 元組,並從 開始儲存在 bufferoffset。 串流中的當前位置會依讀取的位元組數前進;但若發生例外,串流中當前位置保持不變。 實作會回傳讀取的位元組數。 若請求超過零位元組,實作不會在至少讀取一個位元組資料前完成操作(有些實作即使請求零位元組,也可能直到至少一個位元組未完成,且此時串流不會消耗資料)。
Read 只有當請求零位元組或串流中沒有更多資料且預期不再有資料(例如封閉套接字或檔案結束)時,才回傳 0。 即使串流尚未到達終點,實作也可以自由回傳比請求的位元組少。
用於 BinaryReader 讀取原始資料型態。