Package com.langfuse.client.core
Class Stream<T>
java.lang.Object
com.langfuse.client.core.Stream<T>
- Type Parameters:
T- The type of objects in the stream.
- All Implemented Interfaces:
Closeable,AutoCloseable,Iterable<T>
The
Stream class implements Iterable to provide a simple mechanism for reading and parsing
objects of a given type from data streamed via a Reader using a specified delimiter.
Stream assumes that data is being pushed to the provided Reader asynchronously and utilizes a
Scanner to block during iteration if the next object is not available.
Iterable stream for parsing JSON and Server-Sent Events (SSE) data.
Supports both newline-delimited JSON and SSE with optional stream termination.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()static <T> Stream<T> static <T> Stream<T> static <T> Stream<T> static <T> Stream<T> static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty) Creates a stream from SSE data with event-level discrimination support.static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty, String streamTerminator) Creates a stream from SSE data with event-level discrimination support and a stream terminator.iterator()Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Stream
Constructs a newStreamwith the specified value type, reader, and delimiter.- Parameters:
valueType- The class of the objects in the stream.reader- The reader that provides the streamed data.delimiter- The delimiter used to separate elements in the stream.
-
-
Method Details
-
fromJson
-
fromJson
-
fromSse
-
fromSse
-
fromSseWithEventDiscrimination
public static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty) Creates a stream from SSE data with event-level discrimination support. Use this when the SSE payload is a discriminated union where the discriminator is an SSE envelope field (e.g., 'event').- Type Parameters:
T- The type of objects in the stream.- Parameters:
valueType- The class of the objects in the stream.sseReader- The reader that provides the SSE data.discriminatorProperty- The property name used for discrimination (e.g., "event").- Returns:
- A new Stream instance configured for SSE with event-level discrimination.
-
fromSseWithEventDiscrimination
public static <T> Stream<T> fromSseWithEventDiscrimination(Class<T> valueType, Reader sseReader, String discriminatorProperty, String streamTerminator) Creates a stream from SSE data with event-level discrimination support and a stream terminator.- Type Parameters:
T- The type of objects in the stream.- Parameters:
valueType- The class of the objects in the stream.sseReader- The reader that provides the SSE data.discriminatorProperty- The property name used for discrimination (e.g., "event").streamTerminator- The terminator string that signals end of stream (e.g., "[DONE]").- Returns:
- A new Stream instance configured for SSE with event-level discrimination.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
iterator
Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available.
-