boolean findMarker() throws IOException {
  if (markerIndex == marker.length) {
    return true;
  }
  if (log.isDebugEnabled())   log.debug("Looking for token, " + BytesUtil.toHexString(marker) + ", reads: "+ nreads);
  while (markerIndex < marker.length) {
    final int remtok=marker.length - markerIndex;
    markerBB.limit(remtok);
    markerBB.position(0);
    final int rdLen=client.read(markerBB);
    if (rdLen == -1) {
      throw new IOException("EOF: nreads=" + nreads + ", bytesRead="+ bytesRead);
    }
    nreads++;
    bytesRead+=rdLen;
    for (int i=0; i < rdLen; i++) {
      if (markerBuffer[i] != marker[markerIndex]) {
        if (foundMarkerInInitialPosition) {
          foundMarkerInInitialPosition=false;
          log.error("Marker not found: skipping");
        }
        markerIndex=0;
        if (markerBuffer[i] == marker[markerIndex]) {
          markerIndex++;
        }
      }
 else {
        markerIndex++;
        nmarkerbytematches++;
      }
    }
    if (nreads % 10000 == 0) {
      if (log.isDebugEnabled())       log.debug("...still looking: reads=" + nreads + ", bytesRead="+ bytesRead);
    }
  }
  if (markerIndex != marker.length) {
    if (log.isDebugEnabled())     log.debug("Not found token yet!");
    return false;
  }
 else {
    if (log.isDebugEnabled())     log.debug("Found token after " + nreads + " token reads and "+ nmarkerbytematches+ " byte matches");
    return true;
  }
}

boolean findMarker ( ) throws IOException { if ( markerIndex == marker . length ) { return true ; } if ( log . isDebugEnabled ( ) ) log . debug ( "Looking for token, " + BytesUtil . toHexString ( marker ) + ", reads: " + nreads ) ; while ( markerIndex < marker . length ) { final int remtok = marker . length - markerIndex ; markerBB . limit ( remtok ) ; markerBB . position ( 0 ) ; final int rdLen = client . read ( markerBB ) ; if ( rdLen == - 1 ) { throw new IOException ( "EOF: nreads=" + nreads + ", bytesRead=" + bytesRead ) ; } nreads ++ ; bytesRead += rdLen ; for ( int i = 0 ; i < rdLen ; i ++ ) { if ( markerBuffer [ i ] != marker [ markerIndex ] ) { if ( foundMarkerInInitialPosition ) { foundMarkerInInitialPosition = false ; log . error ( "Marker not found: skipping" ) ; } markerIndex = 0 ; if ( markerBuffer [ i ] == marker [ markerIndex ] ) { markerIndex ++ ; } } else { markerIndex ++ ; nmarkerbytematches ++ ; } } if ( nreads % 10000 == 0 ) { if ( log . isDebugEnabled ( ) ) log . debug ( "...still looking: reads=" + nreads + ", bytesRead=" + bytesRead ) ; } } if ( markerIndex != marker . length ) { if ( log . isDebugEnabled ( ) ) log . debug ( "Not found token yet!" ) ; return false ; } else { if ( log . isDebugEnabled ( ) ) log . debug ( "Found token after " + nreads + " token reads and " + nmarkerbytematches + " byte matches" ) ; return true ; } }
note that the logic for finding the token bytes depends on the first byte in the token being unique ! < p > we have to be a bit clever to be sure we do not read beyond the token and therefore complicate the reading into the localbuffer . < p > this is optimized for the normal case where the marker is read as from the next bytes from the stream . in the worst case scenario this could read large amounts of data only a few bytes at a time , however this is not in reality a significant overhead .
