perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

Streaming back dynamically-generated content #1284

Open s5bug opened 9 months ago

s5bug commented 9 months ago

I have a route that ends with code like

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?