public static Function<IHTTPSession, Response> asyncHandler(@javax.annotation.Nonnull final ExecutorService pool, final String mimeType, @javax.annotation.Nonnull final Consumer<OutputStream> logic, final boolean async) {
    return session -> {
      @javax.annotation.Nonnull final PipedInputStream snk = new PipedInputStream();
      @javax.annotation.Nonnull final Semaphore onComplete = new Semaphore(0);
      pool.submit(() -> {
        try (@javax.annotation.Nonnull OutputStream out = new BufferedOutputStream(new AsyncOutputStream(new PipedOutputStream(snk)))) {
          try {
            logic.accept(out);
          } finally {
            onComplete.release();
          }
        } catch (@javax.annotation.Nonnull final IOException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
        }
      });
      if (!async) {
        try {
          onComplete.acquire();
        } catch (@javax.annotation.Nonnull final InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
      return NanoHTTPD.newChunkedResponse(Response.Status.OK, mimeType, new BufferedInputStream(snk));
    };
  }
public static Function < IHTTPSession , Response > asyncHandler ( @ javax . annotation . Nonnull final ExecutorService pool , final String mimeType , @ javax . annotation . Nonnull final Consumer < OutputStream > logic , final boolean async ) { return session -> { @ javax . annotation . Nonnull final PipedInputStream snk = new PipedInputStream ( ) ; @ javax . annotation . Nonnull final Semaphore onComplete = new Semaphore ( 0 ) ; pool . submit ( ( ) -> { try ( @ javax . annotation . Nonnull OutputStream out = new BufferedOutputStream ( new AsyncOutputStream ( new PipedOutputStream ( snk ) ) ) ) { try { logic . accept ( out ) ; } finally { onComplete . release ( ) ; } } catch ( @ javax . annotation . Nonnull final IOException e ) { e . printStackTrace ( ) ; throw new RuntimeException ( e ) ; } } ) ; if ( ! async ) { try { onComplete . acquire ( ) ; } catch ( @ javax . annotation . Nonnull final InterruptedException e ) { throw new RuntimeException ( e ) ; } } return NanoHTTPD . newChunkedResponse ( Response . Status . OK , mimeType , new BufferedInputStream ( snk ) ) ; } ; }
Async handler function.

@param pool     the pool
@param mimeType the mime type
@param logic    the logic
@param async    the async
@return the function