#1284·spark

Streaming back dynamically-generated content

Author: s5bugCreated Sep 12, 2023Updated Sep 12, 2023

I have a route that ends with code like

java
if(max.isPresent()) {
    Process p = new ProcessBuilder()
            ...
            .start();

    resp.raw().setContentType("application/octet-stream");
    resp.type("application/octet-stream");

    ServletOutputStream out = resp.raw().getOutputStream();
    p.getInputStream().transferTo(out);
    p.waitFor();
    out.close();

    return 200;
} else {
    return 500;
}

I would like to be able to tell the client that the request succeeded as soon as possible (before I create the process), so that it can start reading down the generated data. To put it another way, I would like to "move" return 200; up to before Process p, so that various HTTP clients can start reading the body ASAP. Is this possible with sparkjava?