へぇ - つもりリユースここに私の他の回答(それのほか、一部、とにかく)の1:
参考:
:それは
Reading from NetworkStream corrupts the buffer
が、私はこのような拡張メソッドを持っています
public static class Ext
{
public static IObservable<byte[]> ReadObservable(this Stream stream, int bufferSize)
{
// to hold read data
var buffer = new byte[bufferSize];
// Step 1: async signature => observable factory
var asyncRead = Observable.FromAsyncPattern<byte[], int, int, int>(
stream.BeginRead,
stream.EndRead);
return Observable.While(
// while there is data to be read
() => stream.CanRead,
// iteratively invoke the observable factory, which will
// "recreate" it such that it will start from the current
// stream position - hence "0" for offset
Observable.Defer(() => asyncRead(buffer, 0, bufferSize))
.Select(readBytes => buffer.Take(readBytes).ToArray()));
}
}
おそらく、そうのような形式で記述されたとして、これを使用することができます。
// Note: ToEnumerable works here because your filestream
// has a finite length - don't do this with infinite streams!
var blobboData = stream
.ReadObservable(bufferSize)
// take while we're still reading data
.TakeWhile(returnBuffer => returnBuffer.Length > 0)
.ToEnumerable()
// mash them all together
.SelectMany(buffer => buffer)
.ToArray();
偉大な記事ですが、大量に古いです。これは古いAPIを参照しています。この章では、これらの概念を取り上げ、より新しいバージョンのRxで動作させるようにしています。 * http://introtorx.com/Content/v1.0.10621.0/15_SchedulingAndThreading.html#CreatingYourOwnIterator –
ああ、そのリンクについて知ったことはありません@LeeCampbell - とてもいいです! – JerKimball
この質問には何が起こったのですか?以前の答えはどこにありますか? –