Package org.apache.commons.io

# Class StreamIterator<E>

java.lang.Object
org.apache.commons.io.StreamIterator<E>

Type Parameters:
  `E` - The `Stream` and `Iterator` type.

All Implemented Interfaces:
  `AutoCloseable`, `Iterator<E>`

public final class StreamIterator<E> extends Object implements Iterator<E>, AutoCloseable
Wraps and presents a `Stream` as a `AutoCloseable` `Iterator` 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 this iterator until the end.

Since:
  2.15.0

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `close()`
  Closes the underlying stream.
  `boolean`
  `hasNext()`
  `static <T> StreamIterator<T>`
  `iterator(Stream<T> stream)`
  Wraps and presents a stream as a closable resource that automatically closes itself when reaching the end of stream.
  `E`
  `next()`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface java.util.Iterator

  `forEachRemaining, remove`

- ## Method Details

  - ### iterator

    public static <T> StreamIterator<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 this 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`

  - ### hasNext

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

  - ### next

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