title: Java 8 Stream (based on resource) .iterator() that auto-closes the resource?
body: <p>Does Java 8 <code>Stream.iterator()</code> auto-close the stream when it's done?
  I suppose not...</p>
  
  <p>I have something like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>class Provider implements Serializable {
  
    Iterator&lt;String&gt; iterator() {
      Stream&lt;String&gt; stream = new BufferedReader(...).lines();
      return stream.iterator();
    }
  }
  </code></pre>
  
  <p>This iterator is used by some other class that doesn't know the iterator is based on a file-reading resource. That is</p>
  
  <pre class="lang-java prettyprint-override"><code>class Consumer {
    void f() {
      Iterator&lt;String&gt; iterator = provider.iterator();
      // code that calls iterator methods at non-determined times
    }
  }
  </code></pre>
  
  <p>I have to stream the file because it's too large to fit into memory. But I'd like to be able to auto-close the resource when the iterator doesn't have any more elements, so that I don't have leaked resources. The <code>Provider</code> class is <code>Serializable</code> and I can't have either the Stream or the BufferedReader as members.</p>
  
  <p>Is there a good way to do this?</p>