Feature-Request: HttpResponse.BodyHandlers.ofReader or alternatively convenience function
Daniel Fuchs
daniel.fuchs at oracle.com
Tue Nov 10 19:19:12 UTC 2020
Hi Klaus,
Thank you for you interest in the HttpClient API.
Although using an InputStream (or a Reader) to read the response body
brings you back to blocking synchronous mode, it seems indeed that
creating a Reader from the InputStream is a very common case that
is more difficult than it should be.
I have logged https://bugs.openjdk.java.net/browse/JDK-8256153 to
investigate what help the API could provide.
best regards,
-- daniel
On 05/11/2020 08:37, Klaus Malorny wrote:
>
>
> Hi,
>
> while the new HTTP client provides means to read text files as a single
> string or as a stream of lines, it actually does not provide any way to
> read the text via the Reader class. For many text types, the character
> encoding is not fixed by the format but may vary. Thus one cannot easily
> wrap the input stream of HttpResponse.BodyHandlers.ofInputStream into a
> Reader.
>
> Usually, the character encoding is contained in the content-type header
> field, but it is not easily extractable if done correctly with
> whitespace and quote handling. I have seen in the source code, that the
> implementation does have a method to retrieve the encoding, but this is
> unfortunately deeply buried and not exposed in the user API.
>
> So I would like to suggest to add a HttpResponse.BodyHandler.ofReader
> method to the API, which takes any charset parameter in the content-type
> header into account, like the ofString and ofLines methods.
> Alternatively, convenience methods in HttpHeaders to read the character
> set (and perhaps the MIME type as well) or a helper class in java.net
> could be provided. I know that there are quite a few third party
> implementations, even in the now detached J2EE framework (activation and
> mail), but I am regarding this as essential enough to be included in the
> base Java API or at least in the java.net.http module.
>
> Thanks for your attention,
>
> regards,
>
> Klaus
More information about the net-dev
mailing list