opennlp.tools.util

## Interface ObjectStream<T>

- All Superinterfaces:
    AutoCloseable
  All Known Subinterfaces:
    SequenceStream
  All Known Implementing Classes:
    AbstractEventStream, AbstractObjectStream, AbstractParserEventStream, AbstractToSentenceSampleStream, ADChunkSampleStream, ADNameSampleStream, ADPOSSampleStream, ADSentenceSampleStream, ADSentenceStream, BioNLP2004NameSampleStream, BratAnnotationStream, BratDocumentStream, BratNameSampleStream, ChunkerEventStream, ChunkSampleSequenceStream, ChunkSampleStream, ChunkSampleStream, CollectionObjectStream, Conll02NameSampleStream, Conll03NameSampleStream, ConlluLemmaSampleStream, ConlluPOSSampleStream, ConlluSentenceSampleStream, ConlluStream, ConlluTokenSampleStream, ConllXPOSSampleStream, ConstitParseSampleStream, CrossValidationPartitioner.TrainingSampleStream, DetokenizeSentenceSampleStream, DirectorySampleStream, DocumentCategorizerEventStream, DocumentSampleStream, DocumentToLineStream, EmptyLinePreprocessorStream, EvalitaNameSampleStream, EventTraceStream, FileEventStream, FileToByteArraySampleStream, FileToStringSampleStream, FilterObjectStream, HashSumEventStream, LanguageDetectorEventStream, LanguageDetectorSampleStream, LeipzigLanguageSampleStream, LemmaSampleEventStream, LemmaSampleSequenceStream, LemmaSampleStream, MosesSentenceSampleStream, MucNameSampleStream, NameFinderCensus90NameStream, NameFinderEventStream, NameSampleCountersStream, NameSampleDataStream, NameSampleSequenceStream, NameSampleTypeFilter, NameToSentenceSampleStream, NameToTokenSampleStream, NKJPSentenceSampleStream, OntoNotesNameSampleStream, OntoNotesParseSampleStream, ParagraphStream, ParserEventStream, ParserEventStream, ParseSampleStream, ParseToPOSSampleStream, PlainTextByLineStream, POSSampleEventStream, POSSampleSequenceStream, PosSampleStream, POSToSentenceSampleStream, POSToTokenSampleStream, RealBasicEventStream, RealValueFileEventStream, SDEventStream, SegmenterObjectStream, SentenceSampleStream, SequenceStreamEventStream, TokenizerStream, TokenSampleStream, TokSpanEventStream, TwentyNewsgroupSampleStream, WhitespaceTokenStream, WordTagSampleStream

      public interface ObjectStream<T>
      extends AutoCloseable

  Reads `Object`s from a stream.

  Design Decision:
  This interface provides a means for iterating over the objects in a stream, it does not implement `Iterator` or `Iterable` because:

  - `Iterator.next()` and `Iterator.hasNext()` are declared as throwing no checked exceptions. Thus the `IOException`s thrown by `read()` would have to be wrapped in `RuntimeException`s, and the compiler would be unable to force users of this code to catch such exceptions.
  - Implementing `Iterable` would mean either silently calling `reset()` to guarantee that all items were always seen on each iteration, or documenting that the Iterable only iterates over the remaining elements of the ObjectStream. In either case, users not reading the documentation carefully might run into unexpected behavior.
  See Also:
    `ObjectStreamException`

- - ### Method Summary

    All Methods Instance Methods Abstract Methods Default Methods
    Modifier and Type | Method and Description
    --- | ---
    `default void` | `close()` Closes the `ObjectStream` and releases all allocated resources.
    `T` | `read()` Returns the next object.
    `default void` | `reset()` Repositions the stream at the beginning and the previously seen object sequence will be repeated exactly.

- - ### Method Detail

    - #### read

          T read()
          throws IOException

      Returns the next object. Calling this method repeatedly until it returns null will return each object from the underlying source exactly once.
      Returns:
        the next object or null to signal that the stream is exhausted
      Throws:
        `IOException` - if there is an error during reading

    - #### reset

          default void reset()
                      throws IOException,
                             UnsupportedOperationException

      Repositions the stream at the beginning and the previously seen object sequence will be repeated exactly. This method can be used to re-read the stream if multiple passes over the objects are required. The implementation of this method is optional.
      Throws:
        `IOException` - if there is an error during reseting the stream
        `UnsupportedOperationException`

    - #### close

          default void close()
                      throws IOException

      Closes the `ObjectStream` and releases all allocated resources. After close was called its not allowed to call read or reset.
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException` - if there is an error during closing the stream

Copyright © 2026 The Apache Software Foundation. All rights reserved.