您的位置:首页 > 其它

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)。


异常



ExceptionCondition
ArgumentExceptionoffset 与 count 的和大于缓冲区长度。

ArgumentNullExceptionbuffer 为 null

ArgumentOutOfRangeExceptionoffset 或 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 后可用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: