BufferedStream.Read 方法

定义

重载

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)

将字节从当前缓冲流复制到数组。Copies bytes from the current buffered stream to an array.

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)

将字节从当前缓冲流复制到数组。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

缓冲区中的字节偏移量,从此处开始读取字节。The byte offset in the buffer at which to begin reading bytes.

count
Int32

要读取的字节数。The number of bytes to be read.

返回

读入 array 中的总字节数。The total number of bytes read into array. 如果可用的字节没有所请求的那么多,总字节数可能小于请求的字节数;或者如果在可读取任何数据前就已到达流的末尾,则为零。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.

arraynullarray is null.

offsetcount 为负数。offset or count is negative.

流未打开或为 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.

另请参阅

适用于