BufferedStream.Flush 方法

定義

清除這個資料流的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。Clears all buffers for this stream and causes any buffered data to be written to the underlying device.

public:
 override void Flush();
public override void Flush ();
override this.Flush : unit -> unit
Public Overrides Sub Flush ()

例外狀況

資料流已經處置。The stream has been disposed.

資料來源或儲存機制未開啟The data source or repository is not open.

範例

這個程式碼範例是針對 BufferedStream 類別所提供的較大範例的一部分。This code example is part of a larger example provided for the BufferedStream class.

// Send the data using the BufferedStream.
Console::WriteLine( "Sending data using BufferedStream." );
startTime = DateTime::Now;
for ( int i = 0; i < numberOfLoops; i++ )
{
   bufStream->Write( dataToSend, 0, dataToSend->Length );

}
bufStream->Flush();
bufferedTime = (DateTime::Now - startTime).TotalSeconds;
Console::WriteLine( "{0} bytes sent in {1} seconds.\n", (numberOfLoops * dataToSend->Length).ToString(), bufferedTime.ToString(  "F1" ) );

// Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.");
startTime = DateTime.Now;
for(int i = 0; i < numberOfLoops; i++)
{
    bufStream.Write(dataToSend, 0, dataToSend.Length);
}
bufStream.Flush();
bufferedTime = (DateTime.Now - startTime).TotalSeconds;
Console.WriteLine("{0} bytes sent in {1} seconds.\n",
    numberOfLoops * dataToSend.Length,
    bufferedTime.ToString("F1"));
' Send the data using the BufferedStream.
Console.WriteLine("Sending data using BufferedStream.")
startTime = DateTime.Now
For i As Integer = 1 To numberOfLoops
    bufStream.Write(dataToSend, 0, dataToSend.Length)
Next i

bufStream.Flush()
bufferedTime = DateTime.Now.Subtract(startTime).TotalSeconds
Console.WriteLine("{0} bytes sent In {1} seconds." & vbCrLf, _
    numberOfLoops * dataToSend.Length, _
    bufferedTime.ToString("F1"))

備註

除非您明確地呼叫 FlushClose,否則清除資料流程不會排清其基礎編碼器。Flushing the stream will not flush its underlying encoder unless you explicitly call Flush or Close.

如果您使用 BufferedStream 的函式,因此在建立 BufferedStream 物件時指定緩衝區大小,則會在到達緩衝區大小時將內容排清。If you use the BufferedStream constructor, thus specifying the buffer size while creating the BufferedStream object, the content is flushed when it reaches the buffer size. 例如,BufferedStream bs = new BufferedStream(bs, 5) 之類的程式碼會在緩衝區大小達到5個位元組時排清內容。For example, code such as BufferedStream bs = new BufferedStream(bs, 5) will flush the content when the buffer size reaches 5 bytes.

BufferedStream 的所有讀取和寫入方法都會自動維護緩衝區,因此在讀取和寫入之間來回切換時,不需要叫用 FlushAll the read and write methods of BufferedStream automatically maintain the buffer, so there is no need to invoke Flush when switching back and forth between reading and writing.

適用於

另請參閱