Search in sources :

Example 1 with FinishedIoFuture

use of org.xnio.FinishedIoFuture in project undertow by undertow-io.

the class BlockingWebSocketHttpServerExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
Also used : FinishedIoFuture(org.xnio.FinishedIoFuture) FutureResult(org.xnio.FutureResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 2 with FinishedIoFuture

use of org.xnio.FinishedIoFuture in project undertow by undertow-io.

the class AsyncWebSocketHttpServerExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    final PooledByteBuffer pooled = exchange.getConnection().getByteBufferPool().allocate();
    final ByteBuffer buffer = pooled.getBuffer();
    final StreamSourceChannel channel = exchange.getRequestChannel();
    int res;
    for (; ; ) {
        try {
            res = channel.read(buffer);
            if (res == -1) {
                return new FinishedIoFuture<>(data.toByteArray());
            } else if (res == 0) {
                // callback
                final FutureResult<byte[]> future = new FutureResult<>();
                channel.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {

                    @Override
                    public void handleEvent(final StreamSourceChannel channel) {
                        int res;
                        try {
                            res = channel.read(buffer);
                            if (res == -1) {
                                future.setResult(data.toByteArray());
                                channel.suspendReads();
                                return;
                            } else if (res == 0) {
                                return;
                            } else {
                                buffer.flip();
                                while (buffer.hasRemaining()) {
                                    data.write(buffer.get());
                                }
                                buffer.clear();
                            }
                        } catch (IOException e) {
                            future.setException(e);
                        }
                    }
                });
                channel.resumeReads();
                return future.getIoFuture();
            } else {
                buffer.flip();
                while (buffer.hasRemaining()) {
                    data.write(buffer.get());
                }
                buffer.clear();
            }
        } catch (IOException e) {
            final FutureResult<byte[]> future = new FutureResult<>();
            future.setException(e);
            return future.getIoFuture();
        }
    }
}
Also used : StreamSourceChannel(org.xnio.channels.StreamSourceChannel) FinishedIoFuture(org.xnio.FinishedIoFuture) ChannelListener(org.xnio.ChannelListener) FutureResult(org.xnio.FutureResult) PooledByteBuffer(io.undertow.connector.PooledByteBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PooledByteBuffer(io.undertow.connector.PooledByteBuffer)

Example 3 with FinishedIoFuture

use of org.xnio.FinishedIoFuture in project undertow by undertow-io.

the class ServletWebSocketHttpExchange method readRequestData.

@Override
public IoFuture<byte[]> readRequestData() {
    final ByteArrayOutputStream data = new ByteArrayOutputStream();
    try {
        final ServletInputStream in = request.getInputStream();
        byte[] buf = new byte[1024];
        int r;
        while ((r = in.read(buf)) != -1) {
            data.write(buf, 0, r);
        }
        return new FinishedIoFuture<>(data.toByteArray());
    } catch (IOException e) {
        final FutureResult<byte[]> ioFuture = new FutureResult<>();
        ioFuture.setException(e);
        return ioFuture.getIoFuture();
    }
}
Also used : FinishedIoFuture(org.xnio.FinishedIoFuture) ServletInputStream(javax.servlet.ServletInputStream) FutureResult(org.xnio.FutureResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 FinishedIoFuture (org.xnio.FinishedIoFuture)3 FutureResult (org.xnio.FutureResult)3 PooledByteBuffer (io.undertow.connector.PooledByteBuffer)1 ByteBuffer (java.nio.ByteBuffer)1 ServletInputStream (javax.servlet.ServletInputStream)1 ChannelListener (org.xnio.ChannelListener)1 StreamSourceChannel (org.xnio.channels.StreamSourceChannel)1