Class StreamIterator<E>

java.lang.Object
org.apache.commons.io.StreamIterator<E>
Type Parameters:
E - The stream and iterator type.
All Implemented Interfaces:
Closeable, AutoCloseable, Iterator<E>

final class StreamIterator<E> extends Object implements Iterator<E>, Closeable
Wraps and presents a stream as a closable iterator resource that automatically closes itself when reaching the end of stream.
Since:
2.9.0
  • Field Details

    • iterator

      private final Iterator<E> iterator
    • stream

      private final Stream<E> stream
  • Constructor Details

    • StreamIterator

      private StreamIterator(Stream<E> stream)
  • Method Details

    • iterator

      public static <T> Iterator<T> iterator(Stream<T> stream)
      Wraps and presents a stream as a closable resource that automatically closes itself when reaching the end of stream.

      Warning

      In order to close the stream, the call site MUST either close the stream it allocated OR call the iterator until the end.

      Type Parameters:
      T - The stream and iterator type.
      Parameters:
      stream - The stream iterate.
      Returns:
      A new iterator.
    • close

      public void close()
      Closes the underlying stream.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • hasNext

      public boolean hasNext()
      Specified by:
      hasNext in interface Iterator<E>
    • next

      public E next()
      Specified by:
      next in interface Iterator<E>