Read the current line only and evaluate it. If the line is ok, append it into your buffer
and keep reading. This way you only evaluate the current line in the reader
and not the whole content.
StringBuilder buffer = new StringBuilder();
String line = reader.readLine();
while (line != null && !line.endsWith(endString)) {
buffer.append(line);
line = reader.readLine();
}
if (line != null) {
buffer.append(line);
}
return buffer.toString();