code: public String findWithinHorizon(Pattern pattern, int horizon) {
          ensureOpen();
          if (pattern == null)
              throw new NullPointerException();
          if (horizon < 0)
              throw new IllegalArgumentException("horizon < 0");
          clearCaches();
  
          // Search for the pattern
          while (true) {
              String token = findPatternInBuffer(pattern, horizon);
              if (token != null) {
                  matchValid = true;
                  return token;
              }
              if (needInput)
                  readInput();
              else
                  break; // up to end of input
          }
          return null;
      }
docs: Attempts to find the next occurrence of the specified pattern.
  
  <p>This method searches through the input up to the specified
  search horizon, ignoring delimiters. If the pattern is found the
  scanner advances past the input that matched and returns the string
  that matched the pattern. If no such pattern is detected then the
  null is returned and the scanner's position remains unchanged. This
  method may block waiting for input that matches the pattern.
  
  <p>A scanner will never search more than <code>horizon</code> code
  points beyond its current position. Note that a match may be clipped
  by the horizon; that is, an arbitrary match result may have been
  different if the horizon had been larger. The scanner treats the
  horizon as a transparent, non-anchoring bound (see {@link
  Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds}).
  
  <p>If horizon is <code>0</code>, then the horizon is ignored and
  this method continues to search through the input looking for the
  specified pattern without bound. In this case it may buffer all of
  the input searching for the pattern.
  
  <p>If horizon is negative, then an IllegalArgumentException is
  thrown.
  
  @param pattern the pattern to scan for
  @param horizon the search horizon
  @return the text that matched the specified pattern
  @throws IllegalStateException if this scanner is closed
  @throws IllegalArgumentException if horizon is negative