use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.
the class AsyncSenderImpl method send.
@Override
public void send(final String data, final Charset charset, final IoCallback callback) {
if (exchange.isResponseComplete()) {
throw UndertowMessages.MESSAGES.responseComplete();
}
ByteBuffer bytes = ByteBuffer.wrap(data.getBytes(charset));
if (bytes.remaining() == 0) {
callback.onComplete(exchange, this);
} else {
int i = 0;
ByteBuffer[] bufs = null;
while (bytes.hasRemaining()) {
PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
if (bufs == null) {
//round up division trick
int noBufs = (bytes.remaining() + pooled.getBuffer().remaining() - 1) / pooled.getBuffer().remaining();
pooledBuffers = new PooledByteBuffer[noBufs];
bufs = new ByteBuffer[noBufs];
}
pooledBuffers[i] = pooled;
bufs[i] = pooled.getBuffer();
Buffers.copy(pooled.getBuffer(), bytes);
pooled.getBuffer().flip();
++i;
}
send(bufs, callback);
}
}
use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.
the class AsyncSenderImpl method invokeOnComplete.
/**
* Invokes the onComplete method. If send is called again in onComplete then
* we loop and write it out. This prevents possible stack overflows due to recursion
*/
private void invokeOnComplete() {
for (; ; ) {
if (pooledBuffers != null) {
for (PooledByteBuffer buffer : pooledBuffers) {
buffer.close();
}
pooledBuffers = null;
}
IoCallback callback = this.callback;
this.buffer = null;
this.fileChannel = null;
this.callback = null;
inCallback = true;
try {
callback.onComplete(exchange, this);
} finally {
inCallback = false;
}
StreamSinkChannel channel = this.channel;
if (this.buffer != null) {
long t = Buffers.remaining(buffer);
final long total = t;
long written = 0;
try {
do {
long res = channel.write(buffer);
written += res;
if (res == 0) {
if (writeListener == null) {
initWriteListener();
}
channel.getWriteSetter().set(writeListener);
channel.resumeWrites();
return;
}
} while (written < total);
//we loop and invoke onComplete again
} catch (IOException e) {
invokeOnException(callback, e);
}
} else if (this.fileChannel != null) {
if (transferTask == null) {
transferTask = new TransferTask();
}
if (!transferTask.run(false)) {
return;
}
} else {
return;
}
}
}
use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.
the class BlockingReceiverImpl method receivePartialString.
@Override
public void receivePartialString(final PartialStringCallback callback, final ErrorCallback errorCallback, Charset charset) {
if (done) {
throw UndertowMessages.MESSAGES.requestBodyAlreadyRead();
}
final ErrorCallback error = errorCallback == null ? END_EXCHANGE : errorCallback;
if (callback == null) {
throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
}
if (exchange.isRequestComplete()) {
callback.handle(exchange, "", true);
return;
}
String contentLengthString = exchange.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH);
long contentLength;
if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
if (contentLength > Integer.MAX_VALUE) {
error.error(exchange, new RequestToLargeException());
return;
}
} else {
contentLength = -1;
}
if (maxBufferSize > 0) {
if (contentLength > maxBufferSize) {
error.error(exchange, new RequestToLargeException());
return;
}
}
CharsetDecoder decoder = charset.newDecoder();
int s;
try (PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate()) {
while ((s = inputStream.read(pooled.getBuffer().array(), pooled.getBuffer().arrayOffset(), pooled.getBuffer().remaining())) > 0) {
pooled.getBuffer().limit(s);
CharBuffer res = decoder.decode(pooled.getBuffer());
callback.handle(exchange, res.toString(), false);
pooled.getBuffer().clear();
}
callback.handle(exchange, "", true);
} catch (IOException e) {
error.error(exchange, e);
}
}
use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.
the class BlockingReceiverImpl method receiveFullBytes.
@Override
public void receiveFullBytes(final FullBytesCallback callback, final ErrorCallback errorCallback) {
if (done) {
throw UndertowMessages.MESSAGES.requestBodyAlreadyRead();
}
final ErrorCallback error = errorCallback == null ? END_EXCHANGE : errorCallback;
if (callback == null) {
throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
}
if (exchange.isRequestComplete()) {
callback.handle(exchange, EMPTY_BYTE_ARRAY);
return;
}
String contentLengthString = exchange.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH);
long contentLength;
final ByteArrayOutputStream sb;
if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
if (contentLength > Integer.MAX_VALUE) {
error.error(exchange, new RequestToLargeException());
return;
}
sb = new ByteArrayOutputStream((int) contentLength);
} else {
contentLength = -1;
sb = new ByteArrayOutputStream();
}
if (maxBufferSize > 0) {
if (contentLength > maxBufferSize) {
error.error(exchange, new RequestToLargeException());
return;
}
}
int s;
try (PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate()) {
while ((s = inputStream.read(pooled.getBuffer().array(), pooled.getBuffer().arrayOffset(), pooled.getBuffer().remaining())) > 0) {
sb.write(pooled.getBuffer().array(), pooled.getBuffer().arrayOffset(), s);
}
callback.handle(exchange, sb.toByteArray());
} catch (IOException e) {
error.error(exchange, e);
}
}
use of io.undertow.connector.PooledByteBuffer in project undertow by undertow-io.
the class BlockingReceiverImpl method receiveFullString.
@Override
public void receiveFullString(final FullStringCallback callback, final ErrorCallback errorCallback, final Charset charset) {
if (done) {
throw UndertowMessages.MESSAGES.requestBodyAlreadyRead();
}
final ErrorCallback error = errorCallback == null ? END_EXCHANGE : errorCallback;
if (callback == null) {
throw UndertowMessages.MESSAGES.argumentCannotBeNull("callback");
}
if (exchange.isRequestComplete()) {
callback.handle(exchange, "");
return;
}
String contentLengthString = exchange.getRequestHeaders().getFirst(Headers.CONTENT_LENGTH);
long contentLength;
final ByteArrayOutputStream sb;
if (contentLengthString != null) {
contentLength = Long.parseLong(contentLengthString);
if (contentLength > Integer.MAX_VALUE) {
error.error(exchange, new RequestToLargeException());
return;
}
sb = new ByteArrayOutputStream((int) contentLength);
} else {
contentLength = -1;
sb = new ByteArrayOutputStream();
}
if (maxBufferSize > 0) {
if (contentLength > maxBufferSize) {
error.error(exchange, new RequestToLargeException());
return;
}
}
int s;
try (PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().getArrayBackedPool().allocate()) {
while ((s = inputStream.read(pooled.getBuffer().array(), pooled.getBuffer().arrayOffset(), pooled.getBuffer().remaining())) > 0) {
sb.write(pooled.getBuffer().array(), pooled.getBuffer().arrayOffset(), s);
}
callback.handle(exchange, sb.toString(charset.name()));
} catch (IOException e) {
error.error(exchange, e);
}
}
Aggregations