Class RequestBody

java.lang.Object
dev.relism.flash.models.RequestBody

public final class RequestBody extends Object
Accessor for the HTTP request body. Supports two mutually exclusive read modes per request:
  • bytes() — materialises the full body into a byte[] and caches it. Safe to call multiple times; the second call returns the cached array. Throws for bodies larger than 2 GB.
  • stream() — returns a bounded InputStream without upfront allocation. For fixed-length bodies this is a view into the already-buffered header bytes stitched to the socket; for chunked bodies it is the raw
    invalid reference
    dev.relism.ChunkedInputStream
    that de-chunks on the fly.

Mutual exclusivity: calling both bytes() and stream() on the same request produces undefined results. Choose one mode per handler.

Keep-alive: unread body bytes are discarded by Request.drain() after the handler returns so the socket is correctly positioned for the next pipelined request.

  • Method Details

    • isEmpty

      public boolean isEmpty()
      true if the body has zero bytes (Content-Length: 0 or no body).
    • contentLength

      public long contentLength()
      Declared body size in bytes. Returns -1 for Transfer-Encoding: chunked bodies where the size is not known upfront.
    • bytes

      public byte[] bytes()
      Materialises and caches the full body. Suitable for JSON, small form data, and any payload that must be inspected in full. The result is cached — repeated calls return the same array.

      For chunked bodies (contentLength() == -1), reads until the chunked stream signals EOF.

      Throws:
      IllegalStateException - if contentLength() exceeds Integer.MAX_VALUE (~2 GB); use stream() for large bodies instead
    • stream

      public InputStream stream()
      Returns a bounded InputStream over the body without upfront allocation.

      For fixed-length bodies: a SequenceInputStream of any already-buffered header bytes followed by a bounded view of the socket stream — zero heap beyond those small pre-buffered bytes.

      For chunked bodies: the raw

      invalid reference
      dev.relism.ChunkedInputStream
      that de-chunks on the fly; EOF signals the end of the logical body and leaves the socket positioned for the next keep-alive request.

      If bytes() was called first, returns a fresh ByteArrayInputStream over the cached array.