Search in sources :

Example 16 with PooledByteBuffer

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);
    }
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 17 with PooledByteBuffer

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;
        }
    }
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer) StreamSinkChannel(org.xnio.channels.StreamSinkChannel) IOException(java.io.IOException)

Example 18 with PooledByteBuffer

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);
    }
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException)

Example 19 with PooledByteBuffer

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);
    }
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 20 with PooledByteBuffer

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);
    }
}
Also used : PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

PooledByteBuffer (io.undertow.connector.PooledByteBuffer)54 ByteBuffer (java.nio.ByteBuffer)45 IOException (java.io.IOException)28 ImmediatePooledByteBuffer (io.undertow.util.ImmediatePooledByteBuffer)9 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)8 HttpServerExchange (io.undertow.server.HttpServerExchange)7 ClosedChannelException (java.nio.channels.ClosedChannelException)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ChannelListener (org.xnio.ChannelListener)5 HttpHandler (io.undertow.server.HttpHandler)4 StreamSinkChannel (org.xnio.channels.StreamSinkChannel)4 SendFrameHeader (io.undertow.server.protocol.framed.SendFrameHeader)3 HeaderMap (io.undertow.util.HeaderMap)3 HttpString (io.undertow.util.HttpString)3 InterruptedIOException (java.io.InterruptedIOException)3 ByteBufferPool (io.undertow.connector.ByteBufferPool)2 CharBuffer (java.nio.CharBuffer)2 CharsetDecoder (java.nio.charset.CharsetDecoder)2 IoCallback (io.undertow.io.IoCallback)1 Sender (io.undertow.io.Sender)1