Skip to main content
Package: com.coinbase.cdp.core
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.

Constructor Details

Stream

Constructs a new Stream with the specified value type, reader, and delimiter. Parameters: valueType (Class<T>) - The class of the objects in the stream. reader (Reader) - The reader that provides the streamed data. delimiter (String) - The delimiter used to separate elements in the stream.

Method Details

fromJson

Overload 1
Parameters: valueType - Class<T> reader - Reader Returns: Stream<T> Overload 2
Parameters: valueType - Class<T> reader - Reader delimiter - String Returns: Stream<T>

fromSse

Overload 1
Parameters: valueType - Class<T> sseReader - Reader Returns: Stream<T> Overload 2
Parameters: valueType - Class<T> sseReader - Reader streamTerminator - String Returns: Stream<T>

fromSseWithEventDiscrimination

Overload 1
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 (Class<T>) - The class of the objects in the stream. sseReader (Reader) - The reader that provides the SSE data. discriminatorProperty (String) - The property name used for discrimination (e.g., “event”). Returns: Stream<T> - A new Stream instance configured for SSE with event-level discrimination. Overload 2
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 (Class<T>) - The class of the objects in the stream. sseReader (Reader) - The reader that provides the SSE data. discriminatorProperty (String) - The property name used for discrimination (e.g., “event”). streamTerminator (String) - The terminator string that signals end of stream (e.g., “[DONE]”). Returns: Stream<T> - A new Stream instance configured for SSE with event-level discrimination.

close

Specified by: [close](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html#close\(\) "class or interface in java.lang") in interface [AutoCloseable](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/AutoCloseable.html "class or interface in java.lang") Specified by: [close](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Closeable.html#close\(\) "class or interface in java.io") in interface [Closeable](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Closeable.html "class or interface in java.io") Throws: [IOException](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/IOException.html "class or interface in java.io") Returns: void

iterator

Returns an iterator over the elements in this stream that blocks during iteration when the next object is not yet available. Specified by: [iterator](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Iterable.html#iterator\(\) "class or interface in java.lang") in interface [Iterable](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Iterable.html "class or interface in java.lang")<[T](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream "type parameter in Stream")> Returns: Iterator<T> - An iterator that can be used to traverse the elements in the stream.