Interface BodyReader

All Superinterfaces:
AutoCloseable

public interface BodyReader extends AutoCloseable
A source of request body bytes that a handler reads incrementally. A reader is valid while the handler that obtained it from Context.bodyReader() runs; it is confined to that handler's thread, except for close(), which a transport may call from another thread to cancel a request.

A read blocks until at least one byte is available, returns the number of bytes copied, or returns -1 once the body has ended. A zero-length request ends at once. Reads honour interruption: an interrupted handler gets an IOException with its interrupt flag set, as it does for a timed-out request. Every byte is delivered at most once and in wire order.

pause() and resume() control delivery from the transport. Pausing asks it to stop reading bytes from the connection; resuming asks it to continue. A transport also pauses and resumes on its own at buffer water marks, so a handler reading slowly cannot make it buffer an unbounded body. Pausing never discards bytes that were already accepted by a read.

  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Returns the number of bytes this reader has handed to the handler.
    void
    Cancels the reader: any buffered bytes are dropped, reads blocked on it fail, and any temporary storage it owns is released.
    static BodyReader
    of(byte[] bytes)
    Creates a reader over a copy of bytes.
    void
    Asks the transport to stop taking bytes from the connection.
    boolean
    Returns whether delivery is currently paused, whether by the handler or by the transport's water marks.
    default int
    read(byte[] bytes)
    Copies up to the array's length into it.
    int
    read(byte[] bytes, int offset, int length)
    Copies up to length bytes into the array, blocking until a byte is available or the body ends.
    void
    Asks the transport to take bytes again.
  • Method Details

    • read

      int read(byte[] bytes, int offset, int length) throws IOException
      Copies up to length bytes into the array, blocking until a byte is available or the body ends.
      Parameters:
      bytes - destination array
      offset - first index in the destination
      length - maximum number of bytes; zero returns zero
      Returns:
      bytes copied, or -1 at the end of the body
      Throws:
      IOException - if the reader was closed, the request was cancelled or the thread was interrupted
      IndexOutOfBoundsException - if the slice is outside the array
    • read

      default int read(byte[] bytes) throws IOException
      Copies up to the array's length into it.
      Parameters:
      bytes - destination array
      Returns:
      bytes copied, or -1 at the end of the body
      Throws:
      IOException - if read(byte[], int, int) fails
    • pause

      void pause()
      Asks the transport to stop taking bytes from the connection. Idempotent. Buffered readers block subsequent reads until resume() is called, so tests can hold a hand-off deterministically.
    • resume

      void resume()
      Asks the transport to take bytes again. Idempotent, and wakes a read waiting on a paused buffered reader.
    • paused

      boolean paused()
      Returns whether delivery is currently paused, whether by the handler or by the transport's water marks.
      Returns:
      true while reads from the connection are suspended
    • bytesRead

      long bytesRead()
      Returns the number of bytes this reader has handed to the handler.
      Returns:
      bytes read
    • close

      void close()
      Cancels the reader: any buffered bytes are dropped, reads blocked on it fail, and any temporary storage it owns is released. Idempotent, callable from any thread, and never throws. A handler should call it when it stops reading a body before its end.
      Specified by:
      close in interface AutoCloseable
    • of

      static BodyReader of(byte[] bytes)
      Creates a reader over a copy of bytes. Reads hand over the whole array as soon as it is available; pause() and resume() gate those reads so that a test can model a slow handler without timing dependence. In-memory execution and the test client use this hand-off; no temporary storage is involved.
      Parameters:
      bytes - body content; copied
      Returns:
      a reader positioned at the first byte