Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8306040

HttpResponseInputStream.available() returns 1 on empty stream

    XMLWordPrintable

Details

    • b06
    • 13
    • b13
    • generic
    • generic
    • Verified

    Backports

      Description

        ADDITIONAL SYSTEM INFORMATION :
        Oracle Linux 9
        Oracle JDK 17.0.6

        A DESCRIPTION OF THE PROBLEM :
        When making calls using HttpClient to an endpoint which returns a 204 and no content, the jdk.internal.net.http.ResponseSubscribers.HttpResponseInputStream.available() method returns 1 instead of the expected result of 0 when called on the result.

        REGRESSION : Last worked in version 11.0.18

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Execute a call against an endpoint returning a 204 and no content like this:
              HttpClient client = HttpClient.newHttpClient();
              HttpResponse<InputStream> resp = client.send(req, HttpResponse.BodyHandlers.ofInputStream());
              try (InputStream in = resp.body()) {
                int available = in.available();
                if (available > 0) {
                  throw new RuntimeException("Received available()" + available);
                }
              }

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        available() should return 0.
        ACTUAL -
        available() returns 1. This occurs because the buffers queue within the client contains a single HeapByteBuffer with a capacity of 0.

        ---------- BEGIN SOURCE ----------
        import java.io.IOException;
        import java.io.InputStream;
        import java.net.*;
        import java.net.http.*;

        import com.sun.net.httpserver.HttpServer;

        public class TestHttpClient {
          public static void main(String... args)
              throws IOException, URISyntaxException, InterruptedException {
            // start a basic server that responds with 204 to all requests
            InetAddress address = InetAddress.getLoopbackAddress();
            InetSocketAddress socketAddress = new InetSocketAddress(address.getHostName(), 8080);
            HttpServer server = HttpServer.create(socketAddress, 20);
            server.createContext("/", e -> e.sendResponseHeaders(204, -1));
            server.start();

            // call it with a client
            try {
              HttpRequest req = HttpRequest.newBuilder()
                  .uri(new URI("http", null, address.getHostName(), 8080, null, null, null)).GET().build();
              HttpClient client = HttpClient.newHttpClient();
              HttpResponse<InputStream> resp = client.send(req, HttpResponse.BodyHandlers.ofInputStream());
              try (InputStream in = resp.body()) {
                int available = in.available();
                if (available > 0) {
                  throw new RuntimeException("Received available()" + available);
                }
              }
            }
            finally {
              server.stop(0);
            }
          }
        }
        ---------- END SOURCE ----------

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

                ccleary Conor Cleary (Inactive)
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                8 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: