int lastUsedOffset = -1;
while (offset != -1) {
writer = new LineBufferWriter();
lastUsedOffset = offset;
ApplogPage page = fetchPage(offset, 1000, writer, LogLevel.LEVEL_ALL);
List lines = new ArrayList(writer.getBuffer());
if (!forward) {
// If the retrieved lines includes line numbers beyond the search start line, we must remove them to prevent them from being searched (can only happen in backward mode)
if (offset + lines.size() > searchStartLine) {
lines = lines.subList(0, searchStartLine - offset + 1);
}
// If not searching forward we will reverse the buffer for searching. Resulting line numbers will be calculated back.
Collections.reverse(lines);
}
switch (searchType) {
case SEARCHTYPE_PLAINTEXT:
line = searchForTerm(lines, searchString);
break;
case SEARCHTYPE_REGEXP:
line = searchForRegExp(lines, searchString);
break;
default:
throw new IllegalArgumentException("Unknown search type " + searchType);
}
if (line != -1) {
// Calculating back "real" line if we reversed the buffer previously
if (!forward) {
line = lines.size() - line - 1;
}
break;
}
// Calculate next offset, if the end was not reached
if (!page.isEndReached()) {
if (forward) {
offset = page.getEndIndex() + 1;
}
else {
offset = scrollOffsetBackward(offset);
}
}
else {
offset = -1;
}
}
// Get the exact page beginning with the found line
if (line != -1) {
writer = new LineBufferWriter();
ApplogPage page = fetchPage(lastUsedOffset + line, size, writer, LogLevel.LEVEL_ALL);
return new ArrayList(writer.getBuffer());
}
else {
return new ArrayList();
}