if (separatorString == null) {
return readall();
}
final ByteList separator = (separatorString == PARAGRAPH_DELIMETER) ?
PARAGRAPH_SEPARATOR : separatorString;
descriptor.checkOpen();
if (feof()) {
return null;
}
int c = read();
if (c == -1) {
return null;
}
// unread back
buffer.position(buffer.position() - 1);
ByteList buf = new ByteList(40);
byte first = separator.bytes[separator.begin];
LineLoop : while (true) {
ReadLoop: while (true) {
byte[] bytes = buffer.array();
int offset = buffer.position();
int max = buffer.limit();
// iterate over remainder of buffer until we find a match
for (int i = offset; i < max; i++) {
c = bytes[i];
if (c == first) {
// terminate and advance buffer when we find our char
buf.append(bytes, offset, i - offset);
if (i >= max) {
buffer.clear();
} else {
buffer.position(i + 1);
}
break ReadLoop;
}
}
// no match, append remainder of buffer and continue with next block
buf.append(bytes, offset, buffer.remaining());
int read = refillBuffer();
if (read == -1) break LineLoop;
}
// found a match above, check if remaining separator characters match, appending as we go
for (int i = 0; i < separator.realSize; i++) {
if (c == -1) {
break LineLoop;
} else if (c != separator.bytes[separator.begin + i]) {
buf.append(c);
continue LineLoop;
}
buf.append(c);
if (i < separator.realSize - 1) {
c = read();
}
}
break;