Class NaruSseReader
java.lang.Object
net.thevpc.naru.ext.models.stream.NaruSseReader
- All Implemented Interfaces:
Closeable,AutoCloseable
Pull-based reader for
text/event-stream bodies.
Exists because the wire format is not line-delimited JSON and assuming it is gets it wrong in ways that only show up against a real server:
- a frame's payload can be split across several
data:lines, which must be rejoined with a newline rather than concatenated; - a comment line beginning with
:is a keep-alive, not an event, and a long silent generation is mostly keep-alives; - a server that closes the connection right after the last frame has no trailing blank line, so a pending frame has to be dispatched at end-of-input or the final token is lost.
It reads on demand from the underlying Reader rather than
pre-splitting, which is what makes it a real stream: a frame is handed back as
soon as its terminating blank line arrives, and nothing waits for the
response to end.
Not thread-safe, and not meant to be: a stream has one reader, on the thread that made the call.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordOne dispatched server-sent event. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static NaruSseReaderConvenience for tests and for replaying a captured body.read()The next event, blocking until one is complete.voidreadAll()Consumes the whole remaining stream, which is what a caller that only wants the assembled response does.
-
Field Details
-
DONE
The end-of-stream sentinel every OpenAI-compatible server sends, as a bare data payload. Not a JSON document, so it can never arrive as one.- See Also:
-
-
Constructor Details
-
NaruSseReader
-
-
Method Details
-
of
Convenience for tests and for replaying a captured body. -
read
The next event, blocking until one is complete.- Returns:
- the event, or
nullonce the stream is exhausted - Throws:
IOException- if the underlying transport fails
-
readAll
Consumes the whole remaining stream, which is what a caller that only wants the assembled response does.- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-