BufferedStream.Read 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
Read(Span<Byte>) |
将当前缓冲流中的字节复制到字节跨度,并按读取的字节数向前移动缓冲流中的位置。 |
Read(Byte[], Int32, Int32) |
将字节从当前缓冲流复制到数组。 |
Read(Span<Byte>)
- Source:
- BufferedStream.cs
- Source:
- BufferedStream.cs
- Source:
- BufferedStream.cs
将当前缓冲流中的字节复制到字节跨度,并按读取的字节数向前移动缓冲流中的位置。
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
参数
返回
读入缓冲区中的总字节数。 如果很多字节当前不可用,则这可小于在缓冲区中分配的字节数;如果已到达流结尾,则为零 (0)。
注解
CanRead使用 属性确定当前实例是否支持读取。 ReadAsync使用 方法从当前流异步读取。
此方法的实现从当前流中读取最大字节数 buffer.Length
,并将其存储在 中 buffer
。 流中的当前位置按读取的字节数提前;但是,如果发生异常,流中的当前位置保持不变。 实现返回读取的字节数。 实现将阻止,直到至少一个字节的数据可以读取,如果没有任何数据可用。 Read
仅当流中没有更多数据且预期不会再 ((例如关闭的套接字或文件) 末尾)时才返回 0。 实现可以自由地返回比请求的字节少,即使尚未到达流的末尾。
用于 BinaryReader 读取基元数据类型。
适用于
Read(Byte[], Int32, Int32)
- Source:
- BufferedStream.cs
- Source:
- BufferedStream.cs
- Source:
- BufferedStream.cs
将字节从当前缓冲流复制到数组。
public:
override int Read(cli::array <System::Byte> ^ array, int offset, int count);
public:
override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read (byte[] array, int offset, int count);
public override int Read (byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (array As Byte(), offset As Integer, count As Integer) As Integer
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
参数
- arraybuffer
- Byte[]
将字节复制到的缓冲区。
- offset
- Int32
缓冲区中的字节偏移量,从此处开始读取字节。
- count
- Int32
要读取的字节数。
返回
读入 array
中的总字节数。 如果可用的字节没有所请求的那么多,总字节数可能小于请求的字节数;或者如果在可读取任何数据前就已到达流的末尾,则为零。
例外
array
的长度减去 offset
小于 count
。
array
为 null
。
offset
或 count
为负数。
流未打开或为 null
。
流不支持读取。
在流关闭后调用方法。
示例
此代码示例是为 BufferedStream 类提供的一个更大示例的一部分。
// 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.
printfn "Receiving data using BufferedStream."
bytesReceived <- 0
let startTime = DateTime.Now
let mutable numBytesToRead = receivedData.Length
let mutable broken = false
while not broken && numBytesToRead > 0 do
// Read may return anything from 0 to numBytesToRead.
let n = bufStream.Read(receivedData,0, receivedData.Length)
// The end of the file is reached.
if n = 0 then
broken <- true
else
bytesReceived <- bytesReceived + n
numBytesToRead <- numBytesToRead - n
let bufferedTime = (DateTime.Now - startTime).TotalSeconds
printfn $"{bytesReceived} bytes received in {bufferedTime:F1} seconds.\n"
' 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。 在所有其他情况下, Read
始终在返回之前从流中至少读取一个字节。 根据定义,如果在调用 Read
时流中没有数据可用,则 Read
方法返回 0, () 自动到达流的末尾。 实现可以自由地返回比请求的字节少,即使尚未到达流的末尾。
用于 BinaryReader 读取基元数据类型。
另请参阅
- BlockCopy(Array, Int32, Array, Int32, Int32)
- CanRead
- Write(Byte[], Int32, Int32)
- 文件和流 I/O
- 如何:从文件中读取文本
- 如何:将文本写入文件