@Override
    public int read(final char[] cbuf, final int offset, final int length) throws IOException {
        int len = 0;

        while (len == 0) {
            boolean doneReplacement = false;
            if (outBuffer.length() == 0) {
                if (allowReplacement) {
                    // Read text into the input buffer, replace the first match if possible and copy replaced
                    // text to the output buffer.
                    doneReplacement = performReplacement();

                } else {
                    // Put any remaining text from the input buffer into the output buffer.
                    if (inBuffer.length() > 0) {
                        copyBuffer(inBuffer.length());

                    } else {
                        // There is no more text to be replaced so pass through.
                        return super.read(cbuf, offset, length);
                    }
                }
            }

            // Copy text from the output buffer to the supplied char array up to the requested length or
            // length of the output buffer.
            len = Math.min(length, outBuffer.length());
            outBuffer.getChars(0, len, cbuf, offset);

            // If there was nothing left in the output buffer, we didn't return any content and we are
            // EOF then return -1.
            if (len == 0 && inBuffer.isEof()) {
                len = doneReplacement
                        ? 0
                        : -1;
            } else {
                // Move the offset in the output buffer ready for the next read operation.
                outBuffer.move(len);
            }
        }

        return len;
    }
Reads characters into an array.  This method will block until some input
is available, an I/O error occurs, or the end of the stream is reached.

@param cbuf Destination buffer
@return The number of characters read, or -1
if the end of the stream
has been reached
@throws IOException If an I/O error occurs
stroom-pipeline/src/main/java/stroom/pipeline/reader/FindReplaceFilter.java