java - 在java9中将发布者流到字符串
问题描述
我怎样才能得到一个字符串Flow.Publisher<Byte> body
?我只想解析来自 Publisher 的字符串。
解决方案
这就是你可以用RxJava2做到这一点的方法:
Flow.Publisher<Byte> bytes = ...;
Flowable.fromPublisher(
FlowAdapters.toPublisher(
bytes
)
).toList()
.map(byteList -> new String(convert(byteList)))
.subscribe((String string) -> {
System.out.println(string);
});
转换定义如下:
static byte[] convert(List<Byte> list) {
final byte[] bytes = new byte[list.size()];
int idx = 0;
for (byte b : list) {
bytes[idx] = b;
idx++;
}
return bytes;
}
推荐阅读
- python - 如何将 JSON 转换为自定义类的实例?
- r - 如何并排比较零膨胀负二项式回归输出,有和没有聚集错误
- r - Pivot_longer in r 用于具有多个值的多个列
- r - 创建一个循环,将 DF 中的一行中的特定值删除到列表中
- paypal - 区分 PayPal Adaptive 和 PayPal Payments Standard 中使用的支付模式
- c++ - 如何专门化具有迭代器特征的函数模板?
- javascript - 检索 http.ServerResponse 正文
- javascript - 重构nodejs进行维护的问题
- python - 如何使所有批次共享一层(批次大小为1)?
- python - 过滤不在列表中的熊猫数据框列名