validateArguments();
}
@Override
protected boolean doProcess(Record inputRecord, InputStream stream) throws IOException {
Record template = inputRecord.copy();
removeAttachments(template);
template.removeAll(Fields.MESSAGE);
Charset detectedCharset = detectCharset(inputRecord, charset);
Reader reader = new InputStreamReader(stream, detectedCharset);
BufferedReader lineReader = new BufferedReader(reader, getBufferSize(stream));
StringBuilder lines = null;
String line;
while ((line = lineReader.readLine()) != null) {
if (lines == null) {
lines = new StringBuilder(line);
} else {
boolean isMatch = regex.reset(line).matches();
if (negate) {
isMatch = !isMatch;
}
/*
not match && previous --> do next
not match && next --> do previous
match && previous --> do previous
match && next --> do next
*/
boolean doPrevious = (what == What.previous);
if (!isMatch) {
doPrevious = !doPrevious;
}
if (doPrevious) { // do previous
lines.append('\n');
lines.append(line);
} else { // do next
if (lines.length() > 0 && !flushRecord(template.copy(), lines.toString())) {
return false;
}
lines.setLength(0);
lines.append(line);
}
}
}
if (lines != null && lines.length() > 0) {
return flushRecord(template.copy(), lines.toString());
}
return true;
}