private int advanceBufferToNextPattern()

in src/com/amazon/kinesis/streaming/agent/tailing/RegexSplitter.java [52:83]


    private int advanceBufferToNextPattern(ByteBuffer buffer) {
        Matcher matcher;
        // this marks the position before which we already attempted to match the pattern
        int currentLookedPosition = buffer.position();
        boolean firstLine = true;
        while (buffer.hasRemaining()) {
            // start matching from the current position on a line by line basis
            if (buffer.get() == SingleLineSplitter.LINE_DELIMITER) {
                // Skip the first line as it must be part of the current record
                if (!firstLine) {
                    String line = new String(buffer.array(), currentLookedPosition, buffer.position() - currentLookedPosition, StandardCharsets.UTF_8);
                    matcher = startingPattern.matcher(line);
                    if(matcher.lookingAt()) {
                        buffer.position(currentLookedPosition);
                        return currentLookedPosition;
                    }
                }
                firstLine = false;
                // update the position that we already looked at
                currentLookedPosition = buffer.position();
            }
        }
        
        // We've scanned to the end and there is only one complete record in the buffer, set the position to the end
        if (!firstLine && buffer.limit() < buffer.capacity() 
                && buffer.position() > 0
                && buffer.get(buffer.position() - 1) == SingleLineSplitter.LINE_DELIMITER) {
            return buffer.position();
        }
        
        return -1;
    }