BufferedStream.Read BufferedStream.Read BufferedStream.Read BufferedStream.Read Method

定義

多載

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

將目前緩衝資料流中的位元組複製到位元組範圍,並依讀取的位元組數將緩衝資料流中位置往前移。Copies bytes from the current buffered stream to a byte span and advances the position within the buffered stream by the number of bytes read.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

從目前的緩衝資料流複製位元組到陣列。Copies bytes from the current buffered stream to an array.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

將目前緩衝資料流中的位元組複製到位元組範圍,並依讀取的位元組數將緩衝資料流中位置往前移。Copies bytes from the current buffered stream to a byte span and advances the position within the buffered stream by the number of bytes read.

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

參數

destination
Span<Byte>

記憶體區域。A region of memory. 當這個方法傳回時,讀取自目前來源的位元組會取代此區域內容。When this method returns, the contents of this region are replaced by the bytes read from the current source.

傳回

緩衝區所讀取的總位元組數。The total number of bytes read into the buffer. 如果目前無法取得足夠的位元組,則這個數目可能小於緩衝區所配置的位元組數,如果已經到達資料流末端,則為零 (0)。This can be less than the number of bytes allocated in the buffer if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

備註

CanRead使用屬性來判斷目前的實例是否支援讀取。Use the CanRead property to determine whether the current instance supports reading. ReadAsync使用方法, 以非同步方式從目前的資料流程讀取。Use the ReadAsync method to read asynchronously from the current stream.

這個方法的執行會從目前的buffer.Length資料流程讀取最多個位元組, 並將buffer它們儲存在中。Implementations of this method read a maximum of buffer.Length bytes from the current stream and store them in buffer. 資料流程中目前的位置會依讀取的位元組數來前移;不過, 如果發生例外狀況, 則資料流程中的目前位置會保持不變。The current position within the stream is advanced by the number of bytes read; however, if an exception occurs, the current position within the stream remains unchanged. 「實現」會傳回讀取的位元組數目。Implementations return the number of bytes read. 在沒有可用資料的情況下, 將會封鎖執行, 直到至少有一個位元組的資料可以讀取為止。The implementation will block until at least one byte of data can be read, in the event that no data is available. Read只有在資料流程中沒有其他資料, 而且不需要更多 (例如封閉的通訊端或檔案結尾) 時, 才會傳回0。Read returns 0 only when there is no more data in the stream and no more is expected (such as a closed socket or end of file). 即使尚未到達資料流程的結尾, 執行也可以自由地傳回比要求還要少的位元組。An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

BinaryReader來讀取基本資料類型。Use BinaryReader for reading primitive data types.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

從目前的緩衝資料流複製位元組到陣列。Copies bytes from the current buffered stream to an array.

public:
 override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public override int Read (byte[] array, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer

參數

array
Byte[]

位元組所要複製到的目的緩衝區。The buffer to which bytes are to be copied.

offset
Int32 Int32 Int32 Int32

緩衝區中的位元組位移 (Offset),要開始讀取位元組處。The byte offset in the buffer at which to begin reading bytes.

count
Int32 Int32 Int32 Int32

要讀取的位元組數。The number of bytes to be read.

傳回

讀到 array 中的位元組總數。The total number of bytes read into array. 如果目前無法提供那麼多的位元組數目,則這個數目可能小於所要求的位元組數;如果在可以讀取任何資料之前已經到達資料流結尾,則為 0。This can be less than the number of bytes requested if that many bytes are not currently available, or 0 if the end of the stream has been reached before any data can be read.

例外狀況

array 長度減 offset 小於 countLength of array minus offset is less than count.

資料流未開啟或為 nullThe stream is not open or is null.

資料流不支援讀取。The stream does not support reading.

關閉資料流後呼叫了方法。Methods were called after the stream was closed.

範例

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

// 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.
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。The Read method will return 0 only if the end of the stream is reached. 在所有其他情況下Read , 一律會從資料流程讀取至少一個位元組, 然後再傳回。In all other cases, Read always reads at least one byte from the stream before returning. 根據定義, 如果在呼叫Read時, 資料流程中沒有可用的資料, 此Read方法會傳回 0 (自動到達資料流程的結尾)。By definition, if no data is available from the stream upon a call to Read, the Read method returns 0 (the end of the stream is reached automatically). 即使尚未到達資料流程的結尾, 執行也可以自由地傳回比要求還要少的位元組。An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

BinaryReader來讀取基本資料類型。Use BinaryReader for reading primitive data types.

另請參閱

適用於