Package com.jsgalactic.axiom.http
Interface BodyReader
- All Superinterfaces:
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 TypeMethodDescriptionlongReturns the number of bytes this reader has handed to the handler.voidclose()Cancels the reader: any buffered bytes are dropped, reads blocked on it fail, and any temporary storage it owns is released.static BodyReaderof(byte[] bytes) Creates a reader over a copy ofbytes.voidpause()Asks the transport to stop taking bytes from the connection.booleanpaused()Returns whether delivery is currently paused, whether by the handler or by the transport's water marks.default intread(byte[] bytes) Copies up to the array's length into it.intread(byte[] bytes, int offset, int length) Copies up tolengthbytes into the array, blocking until a byte is available or the body ends.voidresume()Asks the transport to take bytes again.
-
Method Details
-
read
Copies up tolengthbytes into the array, blocking until a byte is available or the body ends.- Parameters:
bytes- destination arrayoffset- first index in the destinationlength- 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 interruptedIndexOutOfBoundsException- if the slice is outside the array
-
read
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- ifread(byte[], int, int)fails
-
pause
void pause()Asks the transport to stop taking bytes from the connection. Idempotent. Buffered readers block subsequent reads untilresume()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:
closein interfaceAutoCloseable
-
of
Creates a reader over a copy ofbytes. Reads hand over the whole array as soon as it is available;pause()andresume()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
-