private static IObservable <Unit> InternalCopyAsObservable(Stream inputStream, int readSize, Stream outputStream, int writeSize) { // ------------- prepare input IObservable <byte[]> inputSource = new ReadStreamObservable(inputStream, readSize); // ------------- prepare output Func <byte[], int, int, IObservable <Unit> > funcAsyncWrite = Observable.FromAsyncPattern <byte[], int, int>( outputStream.BeginWrite, outputStream.EndWrite); // ------------- chain together return(from bytes in inputSource.MinimumBuffer(writeSize) from writeResult in funcAsyncWrite(bytes, 0, bytes.Length) select writeResult); }
private static void CheckFromApm() { Stream stream = MakeReadableStream(Enumerable.Range(8, 10)); IObservable <byte[]> source = new ReadStreamObservable(stream, 4); using (source.Subscribe(bytes => { Debug.Assert(bytes.Length == 4, "impossible for partial reading"); int parsed = BitConverter.ToInt32(bytes, 0); Thread.Sleep(TimeSpan.FromSeconds(0.5)); // simulate long-time processing Console.WriteLine("{0} is read out.", parsed); }, () => Console.WriteLine("!!! reading completed !!!"))) { Helper.Pause(); } }