title: Efficient way to search a stream for a string
body: <p>Let's suppose that have a stream of text (or Reader in Java) that I'd like to check for a particular string. The stream of text might be very large so as soon as the search string is found I'd like to return true and also try to avoid storing the entire input in memory.</p>
  
  <p>Naively, I might try to do something like this (in Java):</p>
  
  <pre><code>public boolean streamContainsString(Reader reader, String searchString) throws IOException {
      char[] buffer = new char[1024];
      int numCharsRead;
      while((numCharsRead = reader.read(buffer)) &gt; 0) {
          if ((new String(buffer, 0, numCharsRead)).indexOf(searchString) &gt;= 0)
              return true;
      }
      return false;
  }
  </code></pre>
  
  <p>Of course this fails to detect the given search string if it occurs on the boundary of the 1k buffer:</p>
  
  <p>Search text: "stackoverflow"<br>
  Stream buffer 1: "abc.........stack"<br>
  Stream buffer 2: "overflow.......xyz"<br></p>
  
  <p>How can I modify this code so that it correctly finds the given search string across the boundary of the buffer but without loading the entire stream into memory?</p>
  
  <p><strong>Edit:</strong> Note when searching a stream for a string, we're trying to <strong>minimise the number of reads from the stream</strong> (to avoid latency in a network/disk) and to <strong>keep memory usage constant</strong> regardless of the amount of data in the stream. Actual efficiency of the <a href="http://en.wikipedia.org/wiki/String_searching_algorithm" rel="noreferrer">string matching algorithm</a> is secondary but obviously, it would be nice to find a solution that used one of the more efficient of those algorithms.</p>