Stream::Read 方法 (array<Byte>^, Int32, Int32)
2016-06-24 10:01
417 查看
Stream::Read 方法 (array<Byte>^, Int32, Int32)
.NET Framework (current version)其他版本
当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。
命名空间: System.IO
程序集: mscorlib(mscorlib.dll 中)
C#
C++
F#
VB
public: virtual int Read( array<unsigned char>^ buffer, int offset, int count ) abstract
参数
buffer字节数组。此方法返回时,该缓冲区包含指定的字符数组,该数组的 offset 和 (offset + count -1) 之间的值由从当前源中读取的字节替换。
offset
buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。
count
要从当前流中最多读取的字节数。
返回值
Type: System::Int32读入缓冲区中的总字节数。如果很多字节当前不可用,则总字节数可能小于请求的字节数;如果已到达流结尾,则为零 (0)。
Exception | Condition |
---|---|
ArgumentException | offset 与 count 的和大于缓冲区长度。 |
ArgumentNullException | buffer 为 null。 |
ArgumentOutOfRangeException | offset 或 count 为负。 |
IOException | 发生 I/O 错误。 |
NotSupportedException | 流不支持读取。 |
ObjectDisposedException | 在流关闭后调用方法。 |
使用 CanRead 属性来确定当前实例是否支持读取。使用 ReadAsync 方法以从当前流中以异步方式读取。
此方法的实现读取的最多 count 从当前的字节流,并将其在存储 buffer 开始的位置 offset。流中的当前位置高级读取 ; 的字节数但是,如果发生异常,该流中的当前位置保持不变。实现返回读取的字节数。该实现将阻止直到至少一个字节的数据可供读取的事件中任何数据都可用。 Read只有当在流中没有更多数据,并且没有更多预期
(如套接字已关闭或文件结尾),则返回 0。实现可以自由地返回少于所请求的字节,即使尚未达到流的末尾。
使用 BinaryReader 用于读取基元数据类型。
下面的示例演示如何使用 Read 要读取的数据块。
C#
C++
VB
using namespace System; using namespace System::IO; public ref class Block { public: static void Main() { Stream^ s = gcnew MemoryStream(); for (int i = 0; i < 100; i++) { s->WriteByte((Byte)i); } s->Position = 0; // Now read s into a byte buffer. array<Byte>^ bytes = gcnew array<Byte>(s->Length); int numBytesToRead = (int) s->Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to 10. int n = s->Read(bytes, numBytesRead, 10); // The end of the file is reached. if (n == 0) { break; } numBytesRead += n; numBytesToRead -= n; } s->Close(); // numBytesToRead should be 0 now, and numBytesRead should // equal 100. Console::WriteLine("number of bytes read: {0:d}", numBytesRead); } }; int main() { Block::Main(); }
Universal Windows Platform
4.5 后可用
.NET Framework
1.1 后可用
Portable Class Library
受以下版本支持:portable .NET platforms
Silverlight
2.0 后可用
Windows Phone Silverlight
7.0 后可用
Windows Phone
8.1 后可用
相关文章推荐
- React-Native学习指南
- 嵌入式linux开发uboot移植(四)——uboot启动内核的机制
- Maven 环境的配置
- VC++循环读写串口
- SQL Server LightWeight Pooling(纤程) 选项
- Triangle
- SQL Server LightWeight Pooling(纤程) 选项
- WEB技术架构的资源链接。
- 身份证号码验证算法
- 代码规范
- pku 3253 - Fence Repair
- js获取url地址
- Android Support Library package简介
- Ajax 传包含集合的JSON
- 麦饭石、吸氨石与沸石
- StringUtils.isEmpty用法
- 查看方法调用栈
- html或jsp页面刷新验证码的js方式和jquery方式
- android数据存储—SharedPreference
- Retrofit分析与实现