BufferedStream.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
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> ^ array, int offset, int count);
public:
override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] array, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
參數
- arraybuffer
- Byte[]
位元組所要複製到的目的緩衝區。
- offset
- Int32
緩衝區中的位元組位移 (Offset),要開始讀取位元組處。
- count
- Int32
要讀取的位元組數。
傳回
讀到 array
中的位元組總數。 如果目前無法提供那麼多的位元組數目,則這個數目可能小於所要求的位元組數;如果在可以讀取任何資料之前已經到達資料流結尾,則為 0。
例外狀況
array
長度減 offset
小於 count
。
array
為 null
。
offset
或 count
為負。
資料流未開啟或為 null
。
資料流不支援讀取。
關閉資料流後呼叫了方法。
範例
此程式代碼範例是針對 類別提供的較大範例的 BufferedStream 一部分。
// Receive data using the BufferedStream.
Console::WriteLine( "Receiving data using BufferedStream." );
bytesReceived = 0;
startTime = DateTime::Now;
while ( bytesReceived < numberOfLoops * receivedData->Length )
{
bytesReceived += bufStream->Read( receivedData, 0, receivedData->Length );
}
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes received in {1} seconds.\n", bytesReceived.ToString(), bufferedTime.ToString( "F1" ) );
// 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
- 作法:讀取檔案中的文字
- 作法:將文字寫入檔案