in httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionInputBufferImpl.java [209:315]
public boolean readLine(
final CharArrayBuffer lineBuffer,
final boolean endOfStream) throws IOException {
setOutputMode();
// See if there is LF char present in the buffer
int pos = -1;
for (int i = buffer().position(); i < buffer().limit(); i++) {
final int b = buffer().get(i);
if (b == Chars.LF) {
pos = i + 1;
break;
}
}
if (this.maxLineLen > 0) {
final int currentLen = (pos > 0 ? pos : buffer().limit()) - buffer().position();
if (currentLen >= this.maxLineLen) {
throw new MessageConstraintException("Maximum line length limit exceeded");
}
}
if (pos == -1) {
if (endOfStream && buffer().hasRemaining()) {
// No more data. Get the rest
pos = buffer().limit();
} else {
// Either no complete line present in the buffer
// or no more data is expected
return false;
}
}
final int origLimit = buffer().limit();
buffer().limit(pos);
final int requiredCapacity = buffer().limit() - buffer().position();
// Ensure capacity of len assuming ASCII as the most likely charset
lineBuffer.ensureCapacity(requiredCapacity);
if (this.charDecoder == null) {
if (buffer().hasArray()) {
final byte[] b = buffer().array();
final int off = buffer().position();
final int len = buffer().remaining();
lineBuffer.append(b, buffer().arrayOffset() + off, len);
buffer().position(off + len);
} else {
while (buffer().hasRemaining()) {
lineBuffer.append((char) (buffer().get() & 0xff));
}
}
} else {
if (this.charbuffer == null) {
this.charbuffer = CharBuffer.allocate(this.lineBuffersize);
}
this.charDecoder.reset();
for (;;) {
final CoderResult result = this.charDecoder.decode(
buffer(),
this.charbuffer,
true);
if (result.isError()) {
result.throwException();
}
if (result.isOverflow()) {
this.charbuffer.flip();
lineBuffer.append(
this.charbuffer.array(),
this.charbuffer.arrayOffset() + this.charbuffer.position(),
this.charbuffer.remaining());
this.charbuffer.clear();
}
if (result.isUnderflow()) {
break;
}
}
// flush the decoder
this.charDecoder.flush(this.charbuffer);
this.charbuffer.flip();
// append the decoded content to the line buffer
if (this.charbuffer.hasRemaining()) {
lineBuffer.append(
this.charbuffer.array(),
this.charbuffer.arrayOffset() + this.charbuffer.position(),
this.charbuffer.remaining());
}
}
buffer().limit(origLimit);
// discard LF if found
int l = lineBuffer.length();
if (l > 0) {
if (lineBuffer.charAt(l - 1) == Chars.LF) {
l--;
lineBuffer.setLength(l);
}
// discard CR if found
if (l > 0 && lineBuffer.charAt(l - 1) == Chars.CR) {
l--;
lineBuffer.setLength(l);
}
}
return true;
}