in httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java [159:234]
public void writeLine(final CharArrayBuffer lineBuffer) throws CharacterCodingException {
if (lineBuffer == null) {
return;
}
setInputMode();
// Do not bother if the buffer is empty
if (lineBuffer.length() > 0 ) {
if (this.charEncoder == null) {
final int requiredCapacity = buffer().position() + lineBuffer.length();
ensureCapacity(requiredCapacity);
if (buffer().hasArray()) {
final byte[] b = buffer().array();
final int len = lineBuffer.length();
final int off = buffer().position();
final int arrayOffset = buffer().arrayOffset();
for (int i = 0; i < len; i++) {
final int c = lineBuffer.charAt(i);
b[arrayOffset + off + i] = TextUtils.castAsByte(c);
}
buffer().position(off + len);
} else {
for (int i = 0; i < lineBuffer.length(); i++) {
final int c = lineBuffer.charAt(i);
buffer().put(TextUtils.castAsByte(c));
}
}
} else {
if (this.charbuffer == null) {
this.charbuffer = CharBuffer.allocate(this.lineBufferSize);
}
this.charEncoder.reset();
// transfer the string in small chunks
int remaining = lineBuffer.length();
int offset = 0;
while (remaining > 0) {
int l = this.charbuffer.remaining();
boolean eol = false;
if (remaining <= l) {
l = remaining;
// terminate the encoding process
eol = true;
}
this.charbuffer.put(lineBuffer.array(), offset, l);
this.charbuffer.flip();
boolean retry = true;
while (retry) {
final CoderResult result = this.charEncoder.encode(this.charbuffer, buffer(), eol);
if (result.isError()) {
result.throwException();
}
if (result.isOverflow()) {
expand();
}
retry = !result.isUnderflow();
}
this.charbuffer.compact();
offset += l;
remaining -= l;
}
// flush the encoder
boolean retry = true;
while (retry) {
final CoderResult result = this.charEncoder.flush(buffer());
if (result.isError()) {
result.throwException();
}
if (result.isOverflow()) {
expand();
}
retry = !result.isUnderflow();
}
}
}
writeCRLF();
}