title: Invoking .map() on an infinite stream?
body: <p>According to Javadocs for SE 8 Stream.map() does the following </p>
  
  <blockquote>
    <p>Returns a stream consisting of the results of applying the given function to the elements of this stream.</p>
  </blockquote>
  
  <p>However, a book I'm reading (<em>Learning Network Programming with Java</em>, Richard M. Reese) on networking implements roughly the following code snippet in an echo server. </p>
  
  <pre><code>Supplier&lt;String&gt; inputLine = () -&gt; {
      try {
          return br.readLine();
      } catch(IOException e) {
          e.printStackTrace();
          return null;
      }
  };
  
  Stream.generate(inputLine).map((msg) -&gt; {
      System.out.println("Recieved: " + (msg == null ? "end of stream" : msg));
      out.println("echo: " + msg);
      return msg;
  }).allMatch((msg) -&gt; msg != null);
  </code></pre>
  
  <p>This is supposed to be a functional way to accomplish getting user input to print to the socket input stream. It works as intended, but I don't quite understand how. Is it because map knows the stream is infinite so it lazily executes as new stream tokens become available? It seems like adding something to a collection currently being iterated over by map is a little black magick. Could someone please help me understand what is going on behind the scenes?</p>
  
  <hr>
  
  <p>Here is how I restated this in order to avoid the confusing map usage. I believe the author was trying to avoid an infinite loop since you can't break out of a forEach. </p>
  
  <pre><code>Stream.generate(inputLine).allMatch((msg) -&gt; {
          boolean alive = msg != null;
          System.out.println("Recieved: " + (alive ? msg : "end of stream"));
          out.println("echo: " + msg);
  
          return alive;
  });
  </code></pre>