throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
}
if (this.buffer != null) {
throw UndertowMessages.MESSAGES.dataAlreadyQueued();
}
StreamSinkChannel channel = this.channel;
if (channel == null) {
if (callback == IoCallback.END_EXCHANGE) {
if (exchange.getResponseContentLength() == -1) {
exchange.setResponseContentLength(buffer.remaining());
}
}
this.channel = channel = exchange.getResponseChannel();
if (channel == null) {
throw UndertowMessages.MESSAGES.responseChannelAlreadyProvided();
}
}
this.callback = callback;
if (inCallback) {
this.buffer = new ByteBuffer[]{buffer};
return;
}
try {
do {
if (buffer.remaining() == 0) {
callback.onComplete(exchange, this);
return;
}
int res = channel.write(buffer);
if (res == 0) {
this.buffer = new ByteBuffer[]{buffer};
this.callback = callback;
channel.getWriteSetter().set(writeListener);
channel.resumeWrites();
return;
}
} while (buffer.hasRemaining());
invokeOnComplete();