异步重复从流中读取?
如何构造以下代码,以便可以分块读取所有流。 目前它不断返回相同的数据。不能前进。
public static IObservable<byte[]> AsyncRead(this Stream stream, int bufferSize)
{
var asyncRead = Observable.FromAsyncPattern<byte[], int, int, int>(stream.BeginRead, stream.EndRead);
var buffer = new byte[bufferSize];
return asyncRead(buffer, 0, bufferSize)
.Select(cbRead =>
{
var dataChunk = new byte[cbRead];
Buffer.BlockCopy(buffer, 0, dataChunk, 0, cbRead);
return dataChunk;
})
.Repeat()
.TakeWhile(dataChunk => dataChunk.Length > 0);
}
How can I structure the following code so that I can read all of the stream in chunks.
Currently it keeps returning the same data. Can not advance.
public static IObservable<byte[]> AsyncRead(this Stream stream, int bufferSize)
{
var asyncRead = Observable.FromAsyncPattern<byte[], int, int, int>(stream.BeginRead, stream.EndRead);
var buffer = new byte[bufferSize];
return asyncRead(buffer, 0, bufferSize)
.Select(cbRead =>
{
var dataChunk = new byte[cbRead];
Buffer.BlockCopy(buffer, 0, dataChunk, 0, cbRead);
return dataChunk;
})
.Repeat()
.TakeWhile(dataChunk => dataChunk.Length > 0);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也落入了同样的陷阱。异步流的行为类似于可重放的流。虽然它只包含那一项。要解决这个问题,请将其包装在
Defer
中,如下所示Fell into the same trap as well. Async streams behave like a replayable one. Although it only contains that one item. To fix that, wrap it in
Defer
like this