Class RecordStreamBody
- All Implemented Interfaces:
HttpBody
Lets a caller pass a Consumer<RecordWriter> or Consumer<TokenWriter> as the body of a request.
The consumer is invoked lazily during writeTo(OutputStream) — the cursor writes directly to the
live transport output stream, so arbitrarily large payloads are streamed to the wire without being buffered in
memory first.
While the next-generation client negotiates a parser for inbound/response bodies (from the response
Content-Type), the request-body OUTPUT format here is an explicit caller choice: the Serializer
that backs the cursor is supplied directly (defaulting to JsonSerializer.DEFAULT). The body's
getContentType() reflects that serializer's media type.
Repeatability:
Streaming bodies are non-repeatable by default (isRepeatable() returns false) because the
caller's consumer may be backed by a one-shot source. A non-repeatable body fails fast (throws IOException)
if a resend is required (i.e. writeTo(OutputStream) is invoked more than once). Callers whose consumer can
be replayed safely may opt in via repeatable().
Usage:
See Also:
- Since:
- 9.2.1
-
Method Summary
Modifier and TypeMethodDescriptionlongReturns the content length in bytes, or-1if unknown.Returns the MIME content type of this body, e.g.booleanReturnstrueif this body can be written more than once.static RecordStreamBodyrecords(Consumer<RecordWriter> consumer) Creates a body that streams via aRecordWriter(whole-value record cursor) using the default JSON serializer.static RecordStreamBodyrecords(Serializer serializer, Consumer<RecordWriter> consumer) Creates a body that streams via aRecordWriter(whole-value record cursor) using the given serializer.Returns a copy of this body flagged as repeatable.static RecordStreamBodytoken(Consumer<TokenWriter> consumer) Creates a body that streams via aTokenWriter(fine-grained structural cursor) using the default JSON serializer.static RecordStreamBodytoken(Serializer serializer, Consumer<TokenWriter> consumer) Creates a body that streams via aTokenWriter(fine-grained structural cursor) using the given serializer.voidwriteTo(OutputStream out) Writes the body content to the given output stream.
-
Method Details
-
records
Creates a body that streams via aRecordWriter(whole-value record cursor) using the default JSON serializer.- Parameters:
consumer- The callback that emits records. Must not benull .- Returns:
- A new body. Never
null .
-
records
Creates a body that streams via aRecordWriter(whole-value record cursor) using the given serializer.- Parameters:
serializer- The serializer that opens the record cursor. Must not benull and must implementRecordWritable.consumer- The callback that emits records. Must not benull .- Returns:
- A new body. Never
null . - Throws:
IllegalArgumentException- If the serializer does not support the record-writer surface.
-
token
Creates a body that streams via aTokenWriter(fine-grained structural cursor) using the default JSON serializer.- Parameters:
consumer- The callback that emits structural events. Must not benull .- Returns:
- A new body. Never
null .
-
token
Creates a body that streams via aTokenWriter(fine-grained structural cursor) using the given serializer.- Parameters:
serializer- The serializer that opens the token cursor. Must not benull and must implementTokenWritable.consumer- The callback that emits structural events. Must not benull .- Returns:
- A new body. Never
null . - Throws:
IllegalArgumentException- If the serializer does not support the token-writer surface.
-
repeatable
Returns a copy of this body flagged as repeatable.Only opt in when the caller's consumer can be safely replayed (e.g. it iterates a re-readable source).
- Returns:
- A new repeatable body. Never
null .
-
getContentType
Description copied from interface:HttpBodyReturns the MIME content type of this body, e.g."application/json; charset=UTF-8".- Specified by:
getContentTypein interfaceHttpBody- Returns:
- The content type, or
null if unknown.
-
getContentLength
Description copied from interface:HttpBodyReturns the content length in bytes, or-1if unknown.Implementations that buffer content may return the exact length; streaming implementations should return
-1to cause the transport to use chunked transfer encoding.- Specified by:
getContentLengthin interfaceHttpBody- Returns:
- The content length, or
-1if unknown.
-
isRepeatable
Description copied from interface:HttpBodyReturnstrueif this body can be written more than once.Streaming bodies backed by a one-shot
InputStreamshould returnfalse. Buffered bodies (byte arrays, strings, files) should returntrue.- Specified by:
isRepeatablein interfaceHttpBody- Returns:
trueif repeatable.
-
writeTo
Description copied from interface:HttpBodyWrites the body content to the given output stream.Implementations must not close
out.- Specified by:
writeToin interfaceHttpBody- Parameters:
out- The stream to write to. Nevernull .- Throws:
IOException- If an I/O error occurs.
-