5

I would like to stream the stdout of a local process to a Vert.x HttpResponse.

To do it I think I have to stream/convert/pipe a java.io.InputStream (which streams the process stdout) to an io.vertx.core.streams.ReadStream and then I can pipe the ReadStream to the HttpResponse.

I'm searching for a solution that has a small memory impact, so read the whole stdout in memory and then flush it to the HttpResponse is not possible.

Thanks

b1zzu
  • 370
  • 4
  • 17
  • 1
    Not sure what are you trying to achieve by that. ReadStream represent large, but finite amount of data. `System.out` is an infinite stream. The result would be that your HttpResponse never closes. – Alexey Soshin Feb 12 '21 at 15:38
  • 1
    I think OP wants to start a process (e.g. via ProcessBuilder) and pipe the output to the HttpResponse rather than piping System.out to the HttpResponse – Selim Feb 13 '21 at 20:35

2 Answers2

3

Check out:

https://gist.github.com/Stwissel/a7f8ce79785afd49eb2ced69b56335de

Here is how I used it:

InputStream is = ...
          AsyncInputStream ais = new AsyncInputStream(
            vertx, vertx.getOrCreateContext(), is);
          ais.pipeTo(response);
2

Another way is to use the OutputToReadStream class from the vertx-java.io library (I am the author):

new OutputToReadStream(vertx).wrap(inputStream).pipeTo(response);
Guss
  • 30,470
  • 17
  • 104
  • 128