Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Convert IObservable<byte[]> with irregular length byte arrays to IObservable<byte[]> with regular length arrays
- {1, 2, 3, 4}
- {5, 6}
- {7, 8, 9}
- {10}
- {11, 12, 13, 14, 15}
- {16}
- {17, 18}
- {19, 20}
- Bytes.Subscribe(b => Console.WriteLine(b.Length));
- 3
- 2
- 3
- 1
- 5
- 1
- 2
- 2
- {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
- {11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
- Bytes.<WhateverItTakesToDoThat>.Subscribe(b => Console.WriteLine(b.Length));
- 10
- 10
- {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
- {33, 34, 35, 36, 37, 38, 39, 40, 41}
- {42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52}
- {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}
- {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}
- {41, 42, 43, 44, 45, 46, 47, 48, 49, 50}
- Bytes
- .SelectMany(b => b)
- .Buffer(10)
- .Select(bs => bs.ToArray());
- Bytes.Select( b => b.ToObservable() ) // Convert input to IObservable<IObservable<byte>>
- .Merge( 1 ) // Merges the IObservable<IObservable<byte>> to an IObservable<byte>
- // with the bytes in the right order
- .Buffer( 4 ) // Wait until we have 4 bytes ready
- .Select( bl => bl.ToArray() ) // Take these 4 bytes and turn them back into an array
- .Subscribe( b => Console.WriteLine( b.Length ) );
Add Comment
Please, Sign In to add comment