On Wed, 5 Nov 2025 15:17:35 GMT, Michael McMahon <[email protected]> wrote:
>> I'd like to return to this issue with a new PR that addresses the usability
>> issue minimally. Namely to define numeric constants
>> for the counter-intuitive responseLength values in
>> `HttpExchange.sendResponseHeaders`. The apidoc then refers to the constant
>> names rather than the numeric values.
>>
>> I've updated the implementation and some tests (though not all yet) to use
>> the constant names. I'm open to suggestions on the names themselves. Once
>> that is agreed, I'll update the remaining tests and any other call sites in
>> the implementation.
>>
>> Thanks,
>> Michael
>
> Michael McMahon has updated the pull request incrementally with one
> additional commit since the last revision:
>
> update
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);
}
}
versus
public void handle(final HttpExchange exchange) throws IOException {
byte[] bytes = getResponseBody(exchange.getRequestURI());
exchange.sendResponseHeaders(200, ResponseLength.known(bytes));
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
This has the benefit of us being able to deprecate `.sendResponseHeaders(int,
long)` to guide usage to it. The only reason not to do it today is that we lack
the ability to make `ResponseLength` in the way that is ideal (with
`known(...)` and `unknown()` having matching extraction patterns)
And in the presence of such an API there are no uses for the constants so...do
you see my frustration? You need to know to look for `RSPBODY_EMPTY` and write
`bytes.length == 0 ? RSPBODY_EMPTY : bytes.length;` This will not lead to a
better result.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/28132#issuecomment-3492106632