MemoryStream.Read 方法

定义

重载

Read(Span<Byte>)

从当前内存流读取字节序列,并将内存流中的位置向前移动读取的字节数。Reads a sequence of bytes from the current memory stream and advances the position within the memory stream by the number of bytes read.

Read(Byte[], Int32, Int32)

从当前流中读取字节块,并将数据写入缓冲区。Reads a block of bytes from the current stream and writes the data to a buffer.

Read(Span<Byte>)

从当前内存流读取字节序列,并将内存流中的位置向前移动读取的字节数。Reads a sequence of bytes from the current memory stream and advances the position within the memory 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 span are replaced by the bytes read from the current memory stream 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 memory stream has been reached.

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.

offset 参数提供当前流中的数据写入 buffer 中的第一个字节的偏移量。The 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.

另请参阅

适用于