Chris Hegarty chris.hegarty at oracle.com
Tue May 15 14:38:26 UTC 2018


Simon,

> On 15 May 2018, at 14:47, Simon Roberts <simon at dancingcloudservices.com> wrote:
> 
> Chris, I'm OK with the idea that the program might be "operating in a broken environment”

You may be ok with that, but many folk will not.

> but I find it had to accept the idea that "it's not a crash" as useful.

A recoverable exception in Java are not deemed as a “crash”.

> By the time the exception is thrown, I cannot access the data. Merely telling the caller that there was an inconsistency in the server (which I think we're assuming is true) has rendered the data unreachable.

That depends on your particular usage of the API, for example you could
do something like:

  HttpResponse<InputStream> response = client.send(getRequest,
          HttpResponse.BodyHandler.asInputStream());
  System.out.println("response: " + response);
  response.headers().firstValue("content-length")
          .ifPresent(cl -> System.out.println("content-length:" + cl));
  InputStream is = response.body();
  byte[] bytes = new byte[64*1024];
  int read = 0, count;
  try {
      while ((count = is.read(bytes, read, bytes.length - read)) != -1)
          read += count;
  } catch (IOException e) {
      System.err.println("Ignoring " + e);
  }
  System.out.println("bytes read: " + read);
  String bodyAsString = new String(bytes, StandardCharsets.UTF_8);
  System.out.println("body: " + bodyAsString);


> Since every other test mechanism at my disposal survives whatever inaccuracy the node server is producing, it seems a bit ... well, let's say "unforgiving" that this code treats it as an unrecoverable failure.

The API allows for the whole response, including the body, to be retrieved
completely asynchronously, i.e. through one of the String body handlers.
Or for the response headers to be returned first, then the body pulled, i.e.
through an InputStream for example.

While standardising in JDK 11, we’ve updated the javadoc to make this
more clear, as well as adding other pull handlers. Additionally, a custom
handler can be written, with a little extra work, to inspect headers, etc,
before reading the body. Such a handle will be delivered all the response
data before any potential error.

-Chris. 


More information about the net-dev mailing list