The first issue I can see with that code is it's not doing what he expects. He does this to read the file into a StringBuffer: bf.lines().forEach(s -> sb.append(s)); However, this ends up reading all the lines into one giant line, since the String's that lines() produces have the newline character stripped. This leads to the second lines() call to read a 23MB line (the file produced by gen.py). This is less than opti…
public void readString(String data) throws IOException {
int lastIdx = 0;
for (int idx = data.indexOf('\n'); idx > -1; idx = data.indexOf('\n', lastIdx)) {
parseLine(subSequenceView(data, lastIdx, idx));
lastIdx = idx + 1;
}
parseLine(subSequenceView(data, lastIdx, data.length()));
}
CharSequence subSequenceView(CharSequence base, int beginIndex, int endIndex) {
return new StringView(base, beginIndex, endIndex - beginIndex);
}
static class StringView implements CharSequence {
final CharSequence base;
final int offset;
final int length;
StringView(CharSequence base, int offset, int length) {
if (length = 0");
this.base = base;
this.offset = offset;
this.length = length;
}
@Override
public char charAt(int n) {
if (n = length)
throw new IndexOutOfBoundsException(n);
return base.charAt(offset + n);
}
@Override
public int length() {
return length;
}
@Override
public CharSequence subSequence(int beginIndex, int endIndex) {
return new StringView(base, offset + beginIndex, endIndex - beginIndex);
}
}