Class Web.Exchange

java.lang.Object
com.codename1.backend.Web.Exchange
Enclosing class:
Web

public static final class Web.Exchange extends Object

One streamed HTTP exchange: the request body written as it is produced, the response read as it arrives, neither held in memory whole. The way to move a large upload or download, or to forward one (see HttpProxy).

Use it from one thread, in order: write and close getRequestBody() when the request has one, then getStatus() and the headers, then read getResponseBody(). Writes block until the transport has taken the bytes and reads until some have arrived, so a slow peer at either end slows the producer rather than filling memory -- each direction buffers a fixed 256 KiB window. close() ends the exchange whatever state it is in; before the response has been read to its end that CANCELS it, closing the upstream connection rather than draining it. Always close it, in a finally.

Redirects are never followed here: a streamed body cannot be replayed. The request timeout of a WebClient does not apply either -- a stream may legitimately run for as long as it moves -- while its connect and read timeouts do, the read timeout bounding each wait for the peer. A stream has a connection of its own rather than one from the client's pool.

  • Method Details

    • getRequestBody

      public OutputStream getRequestBody() throws IOException
      Where the request body goes. Closing it ends the request body; the stream is the exchange's and closing it does not end the exchange.
      Throws:
      IOException
    • getStatus

      public int getStatus() throws IOException
      The response status, waiting for the response headers. Ends a request body that is still open first.
      Throws:
      IOException
    • getProtocol

      public String getProtocol() throws IOException
      "HTTP/1.1" or "HTTP/2", as negotiated.
      Throws:
      IOException
    • getHeaders

      public Map getHeaders() throws IOException
      The first value of every response header, by lower-case name.
      Throws:
      IOException
    • getHeaderValueMap

      public Map getHeaderValueMap() throws IOException
      Every response header, by lower-case name, each a List of values in arrival order -- Set-Cookie and the like keep every line.
      Throws:
      IOException
    • getHeader

      public String getHeader(String name) throws IOException
      Throws:
      IOException
    • getHeaderValues

      public List getHeaderValues(String name) throws IOException
      Throws:
      IOException
    • getResponseBody

      public InputStream getResponseBody() throws IOException
      The response body as it arrives. Reading it to the end completes the exchange; close() before that cancels it.
      Throws:
      IOException
    • close

      public void close()
      Ends the exchange. Before the response body has been read to its end this cancels it: the transfer is aborted and its connection closed.