Class EventStream<T>

java.lang.Object
com.google.genai.gaos.utils.EventStream<T>
Type Parameters:
T - the type that SSE data fields will be deserialized into
All Implemented Interfaces:
AutoCloseable, Iterable<T>

public final class EventStream<T> extends Object implements Iterable<T>, AutoCloseable
Provides a convenient way to consume Server-Sent Events (SSE) from a stream.

Each SSE message's data field is deserialized into the type T, allowing for easy processing of events as domain objects.

Event Consumption

Events can be consumed in multiple ways:

  • Iteration: Use a for-each loop to process each event:

 try (EventStream<MyEvent> eventStream = new EventStream<>(...)) {
     for (MyEvent event : eventStream) {
         handleEvent(event);
     }
 }
 
  • Stream API: Consume events as a Java Stream (must be closed after use):

 try (EventStream<MyEvent> eventStream = new EventStream<>(...);
      Stream<MyEvent> stream = eventStream.stream()) {
     stream.forEach(this::handleEvent);
 }
 
  • Collect to List: Read all remaining events into a list:

 try (EventStream<MyEvent> eventStream = new EventStream<>(...)) {
     List<MyEvent> events = eventStream.toList();
 }
 

Events are lazily loaded from the underlying SSE stream. Consumption stops either when the stream ends or when an optional terminal message is encountered.

Important: This class implements AutoCloseable and must be used within a try-with-resources block to ensure that underlying streams are properly closed after consumption, preventing resource leaks.

  • Constructor Details

    • EventStream

      public EventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage)
    • EventStream

      public EventStream(InputStream in, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, com.fasterxml.jackson.databind.ObjectMapper mapper, Optional<String> terminalMessage, boolean dataRequired)
  • Method Details

    • next

      public Optional<T> next() throws IOException
      Returns the next message. If another message does not exist returns Optional.empty().
      Returns:
      the next message or Optional.empty() if no more messages
      Throws:
      IOException - when parsing the next message.
    • toList

      public List<T> toList()
      Reads all events and returns them as a List. This method calls close().
      Returns:
      list of events
    • iterator

      public Iterator<T> iterator()
      Returns an Iterator of EventStream events, enabling iteration via for-each loops.
      Specified by:
      iterator in interface Iterable<T>
      Returns:
      events iterator.
    • stream

      public Stream<T> stream()
      Returns a Stream of events. Must be closed after use!
      Returns:
      streamed events
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException
    • isClosed

      public boolean isClosed()