> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cdp.coinbase.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Stream<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.

Package: [`com.coinbase.cdp.core`](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/index)

```java theme={null}
public final class Stream<T> extends Object implements Iterable<T>, Closeable
```

The `Stream` class implements [`Iterable`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Iterable.html "class or interface in java.lang") to provide a simple mechanism for reading and parsing objects of a given type from data streamed via a [`Reader`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Reader.html "class or interface in java.io") using a specified delimiter.

`Stream` assumes that data is being pushed to the provided [`Reader`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/Reader.html "class or interface in java.io") 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`

```java theme={null}
public Stream(Class<T> valueType, Reader reader, String delimiter)
```

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**

```java theme={null}
public static <T> Stream<T> fromJson(Class<T> valueType, Reader reader)
```

**Parameters:**

`valueType` - `Class<T>`

`reader` - `Reader`

**Returns:**

[Stream](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`>

**Overload 2**

```java theme={null}
public static <T> Stream<T> fromJson(Class<T> valueType, Reader reader, String delimiter)
```

**Parameters:**

`valueType` - `Class<T>`

`reader` - `Reader`

`delimiter` - `String`

**Returns:**

[Stream](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`>

### `fromSse`

**Overload 1**

```java theme={null}
public static <T> Stream<T> fromSse(Class<T> valueType, Reader sseReader)
```

**Parameters:**

`valueType` - `Class<T>`

`sseReader` - `Reader`

**Returns:**

[Stream](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`>

**Overload 2**

```java theme={null}
public static <T> Stream<T> fromSse(Class<T> valueType, Reader sseReader, String streamTerminator)
```

**Parameters:**

`valueType` - `Class<T>`

`sseReader` - `Reader`

`streamTerminator` - `String`

**Returns:**

[Stream](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`>

### `fromSseWithEventDiscrimination`

**Overload 1**

```java theme={null}
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` (`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](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`> - A new Stream instance configured for SSE with event-level discrimination.

**Overload 2**

```java theme={null}
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` (`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](/sdks/cdp-sdks-v2/java/com/coinbase/cdp/core/Stream)\<`T`> - A new Stream instance configured for SSE with event-level discrimination.

### `close`

```java theme={null}
public void close() throws IOException
```

**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`

```java theme={null}
public Iterator<T> 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.
