Package dev.relism.flash.models
Class Response
java.lang.Object
dev.relism.flash.models.Response
HTTP response. All mutating methods return
this for fluent chaining.
// fixed body
return new Response(200, "ok", ContentType.TEXT_PLAIN);
// known-length stream → Content-Length header
return new Response(200, ContentType.BINARY).stream(Files.newInputStream(p), Files.size(p));
// unknown-length stream → Transfer-Encoding: chunked
return new Response(200, ContentType.TEXT_PLAIN).chunked(source);
The connection driver (e.g. Http1Connection) owns one Response instance per
connection, reset before every handler call rather than reallocated — the same treatment Request gets (see its Javadoc for the full pooling/dev-mode-guard rationale, which applies
identically here). A handler that returns a different Response instance (e.g.
return new Response(404, "Not Found", ContentType.TEXT_PLAIN);) is fully supported — that
instance is a normal, unpooled, freshly-constructed object like any public-constructor
Response always was; only the connection driver's own default instance is pooled and poisoned
after use.-
Constructor Summary
ConstructorsConstructorDescriptionResponse(int statusCode, byte[] body, ContentType contentType) Response(int statusCode, ContentType contentType) Response(int statusCode, String text, ContentType contentType) -
Method Summary
Modifier and TypeMethodDescriptionbody(byte[] bytes) chunked(InputStream is) Streaming response with unknown length; written withTransfer-Encoding: chunked.byte[]getBody()byte[]List<byte[]> Returns custom headers as fully-rendered"Name: Value\r\n"lines, or an empty list if none were added.byte[]intlongbooleanheader(byte[] preEncoded) Adds a pre-encoded, fully-rendered header line (e.g. a static"X-RateLimit-Limit: 100\r\n"byte array pre-built at boot time).header(PreEncodedHeader preEncoded) Adds a header from aPreEncodedHeaderbuilt once (typically at boot).reused byte region (viaByteWriter.writeAscii(java.lang.String)) instead of building an intermediateStringand re-encoding it — zero allocation once the region has grown to this connection's high-water mark.booleanbooleanInternal distinction between producer-driven and InputStream-driven response bodies.booleanvoidrecycle()Marks this instance unsafe for further use — seeRequest.recycle()for the full rationale, identical here.redirect(HttpStatus status, String url) Redirect with an explicit 3xx status.302 Found redirect.reset(int statusCode, ContentType contentType) Repositions this instance for a new request/response cycle — clears the body, stream, status, content type, and every header recorded by the previous cycle.Sets the body from a handler return value.voidsetStatusCode(int code) Lombok-style setter kept for API compatibility — equivalent tostatus(int)without the fluent return.status(int code) Sets the status code.status(HttpStatus status) Sets the status from anHttpStatusconstant.stream(InputStream is, long length) Streaming response with known length; written withContent-Length.streaming(Consumer<ResponseStream> producer) Push-style streaming response with bounded blocking backpressure.toString()trailer(PreEncodedHeader trailer) Adds a pre-encoded structured trailer.Adds a trailer rendered after the response body on both HTTP versions.type(byte[] ct) A content type encoded once, typically at boot: the array is kept, not copied, so it must not change.type(ContentType ct) voidwriteHeaders(OutputStream out) Writes every custom header directly toout, in call order.voidwriteHeadersInto(ByteWriter head) Writes every custom header directly intohead(a scratchByteWriter— This is whatHttp1ResponseWriteruses;writeHeaders(OutputStream)below (theOutputStreamequivalent) exists for the streaming-body write paths that cannot fold their whole write into one scratch buffer.voidwriteTrailers(OutputStream output)
-
Constructor Details
-
Response
-
Response
-
Response
-
-
Method Details
-
reset
Repositions this instance for a new request/response cycle — clears the body, stream, status, content type, and every header recorded by the previous cycle. Public because the connection driver that owns the pooled instance lives in a different package (matchingRequestLine.reset(dev.relism.flash.http.HttpMethod, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.fpr.core.ByteView, dev.relism.flash.models.HeaderView)'s precedent); user code never calls this. -
recycle
public void recycle()Marks this instance unsafe for further use — seeRequest.recycle()for the full rationale, identical here.publicfor the same cross-package reason. -
status
Sets the status code. The phrase is looked up fromHttpStatuson the write path. -
setStatusCode
public void setStatusCode(int code) Lombok-style setter kept for API compatibility — equivalent tostatus(int)without the fluent return. -
status
Sets the status from anHttpStatusconstant. The pre-encoded bytes are used directly on the write path — zero lookup, zero allocation. -
type
-
type
A content type encoded once, typically at boot: the array is kept, not copied, so it must not change. -
type
-
body
-
body
-
stream
Streaming response with known length; written withContent-Length. -
chunked
Streaming response with unknown length; written withTransfer-Encoding: chunked. -
streaming
Push-style streaming response with bounded blocking backpressure. -
trailer
Adds a trailer rendered after the response body on both HTTP versions. -
trailer
Adds a pre-encoded structured trailer. -
redirect
302 Found redirect. Clears the body, sets status andLocationheader. Encoded once at call time; zero-alloc on the write path.return res.redirect("/login"); -
redirect
Redirect with an explicit 3xx status. UseHttpStatus.MOVED_PERMANENTLY,HttpStatus.TEMPORARY_REDIRECT(307), orHttpStatus.PERMANENT_REDIRECT(308) when semantics matter.return res.redirect(HttpStatus.MOVED_PERMANENTLY, "/new-path"); -
header
reused byte region (viaByteWriter.writeAscii(java.lang.String)) instead of building an intermediateStringand re-encoding it — zero allocation once the region has grown to this connection's high-water mark. -
header
Adds a header from aPreEncodedHeaderbuilt once (typically at boot). Copies its precomputedname/valuebytes into this response's region — a memcpy, not a re-encode. Preserving the field structure makes it usable by both HTTP versions. -
header
Adds a pre-encoded, fully-rendered header line (e.g. a static"X-RateLimit-Limit: 100\r\n"byte array pre-built at boot time). Zero-alloc on both the call path and the h1 write path.h1-only: a rendered
"Name: Value\r\n"line carries no structured name/value data an HPACK encoder could use, so this header is not representable on a HTTP/2 response path — preferheader(PreEncodedHeader)for anything that must render correctly on both protocols. Kept for existing HTTP/1-only callers. -
isStreaming
public boolean isStreaming() -
isChunked
public boolean isChunked() -
isPushStreaming
public boolean isPushStreaming()Internal distinction between producer-driven and InputStream-driven response bodies. -
getStatusCode
public int getStatusCode() -
getStatusBytes
public byte[] getStatusBytes() -
getBody
public byte[] getBody() -
getContentType
public byte[] getContentType() -
getStream
-
getStreamLength
public long getStreamLength() -
hasTrailers
public boolean hasTrailers() -
writeTrailers
- Throws:
IOException
-
setBody
Sets the body from a handler return value. Accepted types:byte[],String,CharSequence. Any other non-null type throwsIllegalArgumentException— return aResponsedirectly, or serialize toString/byte[]before returning. -
getHeaders
Returns custom headers as fully-rendered"Name: Value\r\n"lines, or an empty list if none were added. Introspection/debugging accessor — reconstructs each line from the internal region on every call, so it is not on the zero-alloc write path;writeHeaders(java.io.OutputStream)andResponseSerializerread the internal representation directly instead of going through this method. -
writeHeadersInto
Writes every custom header directly intohead(a scratchByteWriter— This is whatHttp1ResponseWriteruses;writeHeaders(OutputStream)below (theOutputStreamequivalent) exists for the streaming-body write paths that cannot fold their whole write into one scratch buffer. -
writeHeaders
Writes every custom header directly toout, in call order. Zero-alloc when no headers are set or on a warm region.- Throws:
IOException
-
toString
-