title: Is there a way to peek into a stream and check whether it is not empty WITHOUT consuming the stream
body: <p>I wrote a piece of code which retrieves thousands of String objects from a rest endpoint in form of stream.</p>
  <p>My issue is that the endpoint sends data in pages. Every page would have say 1000 elements. Now I don't know the number of pages beforehand.</p>
  <p>So I have to get the page as stream -&gt; look into it and check whether it is empty -&gt; if not consume it. Else stop receiving more pages and move on to next <code>source</code>.</p>
  <p>I tried <code>findFirst</code>, <code>findAny</code> ... you name it. All these are consumers and hence I cannot just check if the stream is empty or not with these as using these would effectively consume the stream.</p>
  <p>I don't want to send request twice either -&gt; one to check whether stream has elements. Then again send same request and retrieve the stream. That would double the download size.</p>
  <p><strong>I came up with an idiotic solution by using <code>peek()</code>, with <code>AtomicBoolean</code>. Is there a way to better do this?</strong></p>
  <pre><code>public static void main(String[] args) throws InterruptedException {
      AtomicBoolean found = new AtomicBoolean(false);
      for (String source : sources) {
          // incoming requests are retrieved page by page
          int page = 0;
          do {
              page++;
              found.set(false);
              try (
                      Stream&lt;String&gt; stringStream = consumer
                              // get the feed
                              .connectToEndpoint(page, source)
  
                              // I don't know whether the page would be empty or not when retrieving
                              // So I peek into the stream and make sure there are elements inside
                              // So when this stream ends, I don't have to go for the next page
  
                              // ===================================================================
                              // The problem I face is that I have to peek every incoming object
                              // There is NOT a way to just look into the stream -&gt; Oh its not empty -&gt; Stop receiving more pages
                              // Even more problematic is the AtomicInteger
                              // Since it is slow, IMAGINE setting it to true at every incoming object
                              // ===================================================================
                              .peek(string -&gt; found.set(true));
  
                      // I convert the string stream to some object stream and filter it out
                      // do further processing
              ) {
                  // Then I take the stream, build a json request and write it to file
  
              } catch (IOException e) {
  
                  // usually a server error -&gt; see #connectToFeedEndpoint
                  e.printStackTrace();
                  page--;
  
                  // give some time for server to relax
                  TimeUnit.SECONDS.sleep(3);
              }
  
              System.out.printf(&quot;Source = %s, Page = %d, Found = %s \n&quot;, source, page, found.get());
  
              // This is where I check found
              // First page is likely to have elements -&gt; So I'm using a do...while
              // At the end of first cycle I check whether found is true -&gt; if it is then I loop again
          } while (found.get());
      }
  }
  </code></pre>