BufferedStream.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Read(Span<Byte>) |
將目前緩衝串流的位元組複製到位元組區間,並依讀取位元組數推進緩衝串流中的位置。 |
| Read(Byte[], Int32, Int32) |
將目前緩衝中的串流位元組複製到陣列。 |
Read(Span<Byte>)
將目前緩衝串流的位元組複製到位元組區間,並依讀取位元組數推進緩衝串流中的位置。
public:
override int Read(Span<System::Byte> destination);
public override int Read(Span<byte> destination);
override this.Read : Span<byte> -> int
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
參數
傳回
讀取到緩衝區的總位元組數。 如果目前沒有該位元組可用,這可能少於緩衝區中分配的位元組數;若已到達串流結束,則為零(0)。
備註
利用該 CanRead 屬性判斷目前實例是否支援讀取。 使用該 ReadAsync 方法非同步讀取目前串流。
此方法的實作最多可從當前串流讀取 buffer.Length 位元組並儲存在 buffer。 串流中的當前位置會依讀取的位元組數前進;但若發生例外,串流中當前位置保持不變。 實作會回傳讀取的位元組數。 若無法讀取資料,實作會阻塞,直到至少能讀取一個位元組資料。
Read 只有當串流中沒有更多資料且預期沒有更多資料(例如封閉套接字或檔案結束)時,才回傳 0。 即使串流尚未到達終點,實作也可以自由回傳比請求的位元組少。
用於 BinaryReader 讀取原始資料型態。
適用於
Read(Byte[], Int32, Int32)
將目前緩衝中的串流位元組複製到陣列。
public:
override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public:
override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public override int Read(byte[] buffer, int offset, int count);
public override int Read(byte[] array, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
參數
- bufferarray
- Byte[]
- offset
- Int32
緩衝區中開始讀取位元組的位元組偏移量。
- count
- Int32
要讀取的位元組數。
傳回
讀取到 array的總位元組數。 若目前沒有該數量的位元組可用,則請求的位元組數可能少於;若資料已到達串流末端,則可為 0。
例外狀況
負offset值長度array小count於 。
array 為 null。
offset 或 count 是陰性。
該溪流未開啟或為 null。
串流不支援閱讀。
方法在溪流關閉後才被呼叫。
範例
此程式碼範例是本類別更大範例 BufferedStream 的一部分。
// Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.");
bytesReceived = 0;
startTime = DateTime.Now;
int numBytesToRead = receivedData.Length;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = bufStream.Read(receivedData,0, receivedData.Length);
// The end of the file is reached.
if (n == 0)
break;
bytesReceived += n;
numBytesToRead -= n;
}
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes received in {1} seconds.\n",
bytesReceived.ToString(),
bufferedTime.ToString("F1"));
// Receive data using the BufferedStream.
printfn "Receiving data using BufferedStream."
bytesReceived <- 0
let startTime = DateTime.Now
let mutable numBytesToRead = receivedData.Length
let mutable broken = false
while not broken && numBytesToRead > 0 do
// Read may return anything from 0 to numBytesToRead.
let n = bufStream.Read(receivedData,0, receivedData.Length)
// The end of the file is reached.
if n = 0 then
broken <- true
else
bytesReceived <- bytesReceived + n
numBytesToRead <- numBytesToRead - n
let bufferedTime = (DateTime.Now - startTime).TotalSeconds
printfn $"{bytesReceived} bytes received in {bufferedTime:F1} seconds.\n"
' Receive data using the BufferedStream.
Console.WriteLine("Receiving data using BufferedStream.")
bytesReceived = 0
startTime = DateTime.Now
Dim numBytesToRead As Integer = receivedData.Length
Dim n As Integer
Do While numBytesToRead > 0
'Read my return anything from 0 to numBytesToRead
n = bufStream.Read(receivedData, 0, receivedData.Length)
'The end of the file is reached.
If n = 0 Then
Exit Do
End If
bytesReceived += n
numBytesToRead -= n
Loop
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes received in {1} " & _
"seconds." & vbCrLf, _
bytesReceived.ToString(), _
bufferedTime.ToString("F1"))
備註
只有當資料流的終點到達時,該 Read 方法才會回傳 0。 在其他情況下, Read 總是至少讀取一個位元組後再返回。 根據定義,若呼叫 時串流無資料可用 Read,該 Read 方法回傳 0(自動到達串流末端)。 即使串流尚未到達終點,實作也可以自由回傳比請求的位元組少。
用於 BinaryReader 讀取原始資料型態。
另請參閱
- BlockCopy(Array, Int32, Array, Int32, Int32)
- CanRead
- Write(Byte[], Int32, Int32)
- 檔案和數據流 I/O
- 如何:從檔案讀取文字
- 如何:將文字寫入檔案