RFR: 8331195: Improve com.sun.net.httpserver.HttpExchange usability [v3]

Michael McMahon michaelm at openjdk.org
Wed Nov 5 13:45:38 UTC 2025


On Wed, 5 Nov 2025 12:26:55 GMT, Daniel Fuchs <dfuchs at openjdk.org> wrote:

> Thanks for adding the `@apiNote` and explicitely mentionning the value of `RSPBODY_CHUNKED`. The new text looks good to me now.
> 
> Could be good to double check what would happen if you have something like that:
> 
> ```
> public void handle(final HttpExchange exchange) throws IOException {
>      byte[] bytes = getResponseBody(exchange.getRequestURI());
>      long fixedlen = bytes.length == 0 ? RSPBODY_EMPTY : bytes.length;
>      exchange.sendResponseHeaders(200, fixedlen);
>      try (OutputStream os = exchange.getResponseBody()) {
>          os.write(bytes);
>      }
> }
> ```
> 
> where `getResponseBody(exchange.getRequestURI());` returns `new byte[0];` will os.write throw, or will it be no-op? If it throws then should it be specified that `sendResponseHeaders` closes the output stream?

That would work fine. What possibly needs to be mentioned is that with chunked encoding, you must always
close the output stream explicitly (or through a try with resources block as in your example). I will add
a sentence to that effect.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/28132#issuecomment-3491274498


More information about the net-dev mailing list