private void processBuffer() {
        if (buffer == null || buffer.length() == 0) {
            return;
        }

        position = 0;
        int maxPos = buffer.length() - 1;
        boolean done = false;
        IApimanBuffer originalBuffer = buffer;
        int bytesConsumed = 0;
        int preUrlFromPos = 0;
        int urlsFound = 0;
        while (!done) {
            if (originalBuffer.get(position) == (byte) 'h' || originalBuffer.get(position) == (byte) 'H') {
                if (isURLStart()) {
                    urlsFound++;
                    // Write everything up to this point - consider it "consumed"
                    if (position > 0) {
                        IApimanBuffer preUrlData = bufferFactory.createBuffer(originalBuffer.getBytes(preUrlFromPos, position));
                        super.write(preUrlData);
                        bytesConsumed = position;
                    }

                    // Now consume the URL
                    int originalPos = position;
                    String url = consumeURL();

                    // URL successfully read from the buffer?  If not, it was probably because we hit
                    // the end of the chunk and need to wait for more data
                    if (url != null) {
                        url = rewriteURL(url);
                        IApimanBuffer urlBuffer;
                        if (url != null) {
                            urlBuffer = bufferFactory.createBuffer(url, "UTF-8"); //$NON-NLS-1$
                        } else {
                            urlBuffer = bufferFactory.createBuffer(originalBuffer.getBytes(originalPos, position));
                        }
                        super.write(urlBuffer);
                        bytesConsumed = position;
                        preUrlFromPos = position;
                    } else {
                        done = true;
                    }
                } else {
                    position++;
                }
            } else {
                position++;
            }

            if (position > maxPos) {
                done = true;
            }
        }

        // What do we do with what's left?  If there *is* anything left!
        if (urlsFound == 0) {
            super.write(originalBuffer);
            buffer = null;
        } else {
            buffer = bufferFactory.createBuffer(originalBuffer.getBytes(bytesConsumed, maxPos + 1));
            if (atEnd) {
                super.write(buffer);
            }
        }

    }
Scan the buffer for possible URLs.  As they are found, extract them from the buffer,
optionally translate them (only if they match the regular expression), and then
send along the translated version.  If a potential URL match is found but its end
spans the end of the buffer, just wait for the next chunk of data!
gateway/engine/policies/src/main/java/io/apiman/gateway/engine/policies/rewrite/URLRewritingStream.java