MemoryStream.Read 方法

定義

多載

Read(Span<Byte>)
Read(Byte[], Int32, Int32)

從目前的資料流讀取位元組區塊,並且將資料寫入緩衝區。Reads a block of bytes from the current stream and writes the data to a buffer.

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

參數

destination
Span<Byte>

傳回

Read(Byte[], Int32, Int32)

從目前的資料流讀取位元組區塊,並且將資料寫入緩衝區。Reads a block of bytes from the current stream and writes the data to a buffer.

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

參數

buffer
Byte[]

當這個方法傳回時,會包含指定的位元組陣列,這個陣列具有介於 offset 到 (offset + count - 1) 之間的值,已由讀取自目前資料流的字元所取代。When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.

offset
Int32

buffer 中以零起始的位元組位移,用來開始儲存讀取自目前資料流的資料。The zero-based byte offset in buffer at which to begin storing data from the current stream.

count
Int32

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

傳回

寫入至緩衝區的總位元組數。The total number of bytes written into the buffer. 如果目前無法提供那麼多的位元組數目,則這個數目可能小於所要求的位元組數目,或如果在讀取任何資料之前已經到達資料流末端,則為零。This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.

例外狀況

buffernullbuffer is null.

offsetcount 為負。offset or count is negative.

緩衝區的長度減去 offset 小於 countoffset subtracted from the buffer length is less than count.

目前的資料流執行個體已關閉。The current stream instance is closed.

範例

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

// Read the first 20 bytes from the stream.
byteArray = gcnew array<Byte>(memStream->Length);
count = memStream->Read( byteArray, 0, 20 );
// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
' Read the first 20 bytes from the stream.
byteArray = _
    New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)

備註

這個方法覆寫 ReadThis method overrides Read.

參數會提供從目前資料流程寫入資料的第buffer一個位元組位移。 offsetThe offset parameter gives the offset of the first byte in buffer to which data from the current stream is written. count參數會提供要從目前資料流程讀取的最大位元組數目。The count parameter gives the maximum number of bytes to read from the current stream. 傳回的值是讀取的實際位元組數目, 如果已到達資料流程末端, 則為零。The returned value is the actual number of bytes read, or zero if the end of the stream is reached.

如果讀取作業成功,資料流程中的目前位置會前進到讀取的位元組數。If the read operation is successful, the current position within the stream advances by the number of bytes read. 如果發生例外狀況,資料流程中的目前位置會保持不變。If an exception occurs, the current position within the stream remains unchanged.

只有Read在到達資料流程的結尾時,方法才會傳回零。The Read method will return zero 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方法會傳回零(自動到達資料流程的結尾)。By definition, if no data is available from the stream upon a call to Read, the Read method returns zero (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.

警告

如果buffer參數中指定的位元組陣列是GetBuffer方法所傳回的基礎緩衝區,則會覆寫陣列內容,而且不會擲回任何例外狀況。If the byte array specified in the buffer parameter is the underlying buffer returned by the GetBuffer method, the array contents are overwritten, and no exception is thrown.

另請參閱

適用於