转换 IObservable将不规则长度字节数组转换为 IObservable具有规则长度的数组
我有一个 IObservable
也就是说,如果我订阅时得到以下输入:
{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}
(并保留 {51, 52},等待更多输入出现)
I have an IObservable<byte[]>
that gives me an uncertain amount of bytes in the byte array. I want to know how I go from that, to returning an IObservable<byte[]>
with a set amount of bytes in each byte array. Let's assume we want 10 bytes at a time.
That is to say, if I get the following input if I were to subscribe:
{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));
The output would be
3
2
3
1
5
1
2
2
What I would like is to convert the input above into this:
{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));
The output would be
10
10
It must also work if an amount of bytes come in that are larger than a single output packet, i.e.:
{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}
Should be turned into
{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}
(and be holding on to {51, 52}, waiting for more input to come along)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很容易。试试这个:
It's easy. Try this:
经过一番思考和修改后,我想出了一个解决方案。下面的代码实现了我想要的功能:
这可能效率很低,而且我几乎可以肯定这不是最有效的方法,所以如果有人能提出更好、更有效的解决方案,我会洗耳恭听!
I came up with one solution after a bit of thinking and tinkering. The following code does what I want:
This is probably inefficient, and I'm almost certain it's not he most efficient way of doing this, so if somebody out there can come up with a better, more efficient solution, I'm all ears!