private void writeBuf ( final int endPos ) throws IOException { // If the stream is not closed, we only process half the buffer at once. String searchTerm ; int pos = 0 ; int lastWritePos = 0 ; while ( pos < endPos ) { searchTerm = findSearchStrings ( pos ) ; if ( searchTerm != null ) { if ( lastWritePos != pos ) { backing . write ( buffer , lastWritePos , pos - lastWritePos ) ; } doReplace ( searchTerm , backing ) ; pos += searchTerm . length ( ) ; lastWritePos = pos ; } else { pos ++ ; } } // Write the remaining characters that weren't matched if ( lastWritePos != pos ) { backing . write ( buffer , lastWritePos , pos - lastWritePos ) ; } // Shuffle the buffer System . arraycopy ( buffer , pos , buffer , 0 , buffer . length - pos ) ; bufferLen -= pos ; }
Writes the current contents of the buffer up to the given position . More data may be written from the buffer when there is a search string that crosses over endPos .