Guest User

Untitled

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