Package com.jsgalactic.axiom.http
Interface BodyWriter
public interface BodyWriter
The destination of a streamed response body (see
Response.stream(int, java.lang.String, com.jsgalactic.axiom.http.StreamBody)). A writer is valid only
while the StreamBody that received it runs, on the virtual thread that runs the handler;
it is not thread-safe and must not be kept or used afterwards.
Every write hands its bytes to the connection at once, so a write is also a flush: each call
becomes one chunk on the wire (large arrays are split into bounded pieces). A write blocks while
the client is not reading fast enough, and for at most the remaining request deadline. When the
stream can no longer be written, whether because the client disconnected, the byte cap was
exceeded, the deadline passed or the server is shutting down, the write throws a StreamAbortedException. The body should let it propagate or return: the response is then
abandoned, resources are released and nothing further is sent to the client.
-
Method Summary
Modifier and TypeMethodDescriptionlongReturns the number of body bytes accepted so far, counted against the stream's byte cap.default voidonShutdown(Runnable action) Runs an action once when shutdown begins, or at once on the calling thread if it already has.default booleanReports whether the listener has started to shut down.default voidwrite(byte[] bytes) Writes a whole array.voidwrite(byte[] bytes, int offset, int length) Writes a slice of an array, copying it before returning.default voidWrites text encoded as UTF-8.
-
Method Details
-
write
Writes a slice of an array, copying it before returning.- Parameters:
bytes- source arrayoffset- index of the first bytelength- number of bytes; zero writes nothing- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reasonIndexOutOfBoundsException- if the slice is outside the array
-
write
Writes a whole array.- Parameters:
bytes- content- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
write
Writes text encoded as UTF-8.- Parameters:
text- content- Throws:
StreamAbortedException- if the stream was abortedIOException- if the stream cannot be written for another reason
-
bytesWritten
long bytesWritten()Returns the number of body bytes accepted so far, counted against the stream's byte cap.- Returns:
- bytes written
-
shutdownRequested
default boolean shutdownRequested()Reports whether the listener has started to shut down. A stream is not cut off at once then: it keeps the shutdown grace period, during which writes still work, and is aborted withStreamAbortedException.Reason.SHUTDOWNonly if it is still running afterwards. A body that sees shutdown should send what completes it, such as a final event, and return, which ends the response with a final chunk and closes the connection. A body that ignores it is cut when the grace period ends.- Returns:
- true once shutdown has begun; always false for a writer that is not connected to a listener
-
onShutdown
Runs an action once when shutdown begins, or at once on the calling thread if it already has. Use it to wake a body that is blocked on something other than a write, for example to complete a future or unsubscribe so that a blocking poll returns. The action runs on a thread of the server (often its event loop) when shutdown begins, so it must be quick and must not block; an exception it throws is logged and ignored. Actions never run for a writer that is not connected to a listener.- Parameters:
action- what to run
-