void (@NotNull String text, @NotNull Key outputType) { int from = 0; // new line char and teamcity message start are two reasons to flush previous line int newLineInd = text.indexOf(NEW_LINE); int teamcityMessageStartInd = text.indexOf(TEAMCITY_SERVICE_MESSAGE_PREFIX); while (from < text.length()) { int nextFrom = Math.min(newLineInd != -1 ? newLineInd + 1 : text.length(), teamcityMessageStartInd != -1 ? teamcityMessageStartInd : text.length()); String chunk = text.substring(from, nextFrom); processLine(chunk, outputType); from = nextFrom; if (nextFrom == teamcityMessageStartInd) { flush(); // Message may still go to buffer if it does not end with new line, force flush teamcityMessageStartInd = text.indexOf(TEAMCITY_SERVICE_MESSAGE_PREFIX, nextFrom + TEAMCITY_SERVICE_MESSAGE_PREFIX.length()); } if (newLineInd != -1 && nextFrom == newLineInd + 1) { newLineInd = text.indexOf(NEW_LINE, nextFrom); } } }