Stream::Read 方法 (array<Byte>^, Int32, Int32)
来源:互联网 发布:全民枪战 霸龙改36数据 编辑:程序博客网 时间:2024/05/20 21:20
当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。
命名空间: System.IO
程序集: mscorlib(mscorlib.dll 中)
语法
C#C++F#VBpublic: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 ArgumentExceptionoffset 与 count 的和大于缓冲区长度。
ArgumentNullExceptionbuffer 为 null。
ArgumentOutOfRangeExceptionoffset 或 count 为负。
IOException发生 I/O 错误。
NotSupportedException流不支持读取。
ObjectDisposedException在流关闭后调用方法。
备注
使用 CanRead 属性来确定当前实例是否支持读取。使用 ReadAsync 方法以从当前流中以异步方式读取。
此方法的实现读取的最多 count 从当前的字节流,并将其在存储 buffer 开始的位置 offset。流中的当前位置高级读取 ; 的字节数但是,如果发生异常,该流中的当前位置保持不变。实现返回读取的字节数。该实现将阻止直到至少一个字节的数据可供读取的事件中任何数据都可用。 Read只有当在流中没有更多数据,并且没有更多预期 (如套接字已关闭或文件结尾),则返回 0。实现可以自由地返回少于所请求的字节,即使尚未达到流的末尾。
使用 BinaryReader 用于读取基元数据类型。
示例
下面的示例演示如何使用 Read 要读取的数据块。
C#C++VBusing 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();}
当在派生类中重写时,从当前流读取字节序列,并将此流中的位置提升读取的字节数。
语法
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)。
异常
offset 与 count 的和大于缓冲区长度。
buffer 为 null。
offset 或 count 为负。
发生 I/O 错误。
流不支持读取。
在流关闭后调用方法。
备注
使用 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();}
0 0
- Stream::Read 方法 (array<Byte>^, Int32, Int32)
- Stream::Read 方法 (array<Byte>^, Int32, Int32)
- String.Substring 方法 (Int32, Int32)
- GraphicsPath.AddArc 方法 (Int32, Int32, Int32, Int32, Single, Single)
- convert byte[4] to int32
- Point.Offset 方法 (Int32, Int32) &&Point.Offset 方法 (Point)
- Int32.TryParse (String, Int32)
- C#中Socket.Receive 方法 (Byte[], Int32, SocketFlags)的SocketFlags的含义
- VB String.Split 方法 (String[], Int32, StringSplitOptions)
- C#Stream、BaseStream、StreamReader、BinaryReader、Convert.ToInt32、int32.Parse()
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String) 或 “Int32 Parse(System.String)”
- TreeView插入节点的方法:TreeNodeCollection.Insert 方法 (Int32, TreeNode)
- C# int32与byte[] 互转 / C/C++ int与BYTE[]互转
- 使用Int32.parse()方法要保证输入参数的正确性
- C#:对Int32数据类型直接增加位测试扩展方法
- LINQ to Entities 不识别方法“Int32 ToInt32(System.String)
- Int32.Parse(string)
- 数值转换(Convert/Int32)
- int最大值,最小值的获取
- 图解 Android 事件分发机制
- Linux下查看文件和文件夹大小
- 在 CentOS 7.1 上安装分布式存储系统 Ceph
- MIFARE Plus-X和MIFARE Plus-S的区别
- Stream::Read 方法 (array<Byte>^, Int32, Int32)
- anaconda 中包的更新和安装
- Spark定制班第29课:深入理解Spark 2.x中的Structured Streaming内幕
- 百度推送(Push)简单集成
- Java内存泄露监控工具
- 专业级智能PHP IDE Zend Studio发布v13.5|附下载
- Android控件ListView的外面嵌套ScrollView的坑
- react-native android环境开发踩坑之旅
- java 绘制数值计算云图