我想将本地进程的标准输出流传输到 Vert.x 的 HttpResponse。为了实现这个目的,我认为需要将一个 java.io.InputStream(管道化进程的标准输出流)流转换成 io.vertx.core.streams.ReadStream,然后可以将 ReadStream 管道化到 HttpResponse。我正在寻找一个内存占用小的解决方案,因此不可能将整个 stdout 读入内存,然后刷新到 HttpResponse 中。谢谢。
查看:
https://gist.github.com/Stwissel/a7f8ce79785afd49eb2ced69b56335de
以下是我如何使用它:
InputStream is = ...
AsyncInputStream ais = new AsyncInputStream(
vertx, vertx.getOrCreateContext(), is);
ais.pipeTo(response);
另一种方式是使用vertx-java.io库中的OutputToReadStream
类(我是作者):
new OutputToReadStream(vertx).wrap(inputStream).pipeTo(response);
System.out
是一个无限流。结果将导致您的HttpResponse永远不会关闭。 - Alexey Soshin