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

Reply via email to