Package org.xnio.channels

Examples of org.xnio.channels.StreamSourceChannel


            }
            this.handler = handler;
            //we need to delegate to a thread pool
            //as we parse with blocking operations

            StreamSourceChannel requestChannel = exchange.getRequestChannel();
            if (requestChannel == null) {
                throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided());
            }
            if (executor == null) {
                exchange.dispatch(new NonBlockingParseTask(exchange.getConnection().getWorker(), requestChannel));
View Full Code Here


            public void completed(final ClientExchange exchange) {
                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                exchange.setResponseListener(new ClientCallback<ClientExchange>() {
                    @Override
                    public void completed(final ClientExchange exchange) {
                        StreamSourceChannel source = exchange.getResponseChannel();
                        final ByteBuffer buffer = ByteBuffer.wrap(new byte[1024]);
                        for(;;) {
                            try {
                                int res = source.read(buffer);
                                if(res == -1) {
                                    handleDone(exchange, out);
                                    return;
                                } else if(res == 0) {
                                    source.getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
                                        @Override
                                        public void handleEvent(StreamSourceChannel channel) {
                                            for(;;) {
                                                try {
                                                    int res = channel.read(buffer);
                                                    if (res == -1) {
                                                        handleDone(exchange, out);
                                                        return;
                                                    } else if (res == 0) {
                                                        return;
                                                    } else {
                                                        buffer.flip();
                                                        out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.arrayOffset() + buffer.limit());
                                                        buffer.clear();
                                                    }
                                                } catch (IOException e) {
                                                    result.setException(e);
                                                }
                                            }
                                        }
                                    });
                                    source.resumeReads();
                                    return;
                                } else {
                                    buffer.flip();
                                    out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.arrayOffset() + buffer.limit());
                                    buffer.clear();
View Full Code Here

            this.options = options;
        }

        @Override
        public void handleEvent(StreamConnection channel) {
            StreamSourceChannel readChannel = channel.getSourceChannel();
            StreamSinkChannel writeChannel = channel.getSinkChannel();
            //set read and write timeouts
            if (channel.supportsOption(Options.READ_TIMEOUT)) {
                readChannel = new ReadTimeoutStreamSourceChannel(readChannel);
            }
View Full Code Here

        String sentKey = connection.getAttachment(KEY);
        final String dKey = solve(sentKey);
        if (!dKey.equals(acceptKey)) {
            throw WebSocketMessages.MESSAGES.webSocketAcceptKeyMismatch(dKey, acceptKey);
        }
        StreamSourceChannel responseChannel = response.readReplyBody();
        for (; ; ) {
            try {
                long read = Channels.drain(responseChannel, Long.MAX_VALUE);
                if (read == 0) {

                    responseChannel.getReadSetter().set(ChannelListeners.drainListener(Long.MAX_VALUE,
                            new ChannelListener<StreamSourceChannel>() {
                                @Override
                                public void handleEvent(final StreamSourceChannel channel) {
                                    handleUpgrade(uri, connection, callback);
                                }
                            }, new ChannelExceptionHandler<StreamSourceChannel>() {
                                @Override
                                public void handleException(final StreamSourceChannel channel, final IOException e) {
                                    callback.failed(e);
                                }
                            }
                    ));
                    responseChannel.resumeReads();
                    return;
                } else if (read == -1) {
                    break;
                }
            } catch (IOException e) {
View Full Code Here

            } else {
                closeConnection = true;
            }
        }
        // Create the http response
        final StreamSourceChannel responseChannel = new ConduitStreamSourceChannel(channel, conduit);
        final HttpClientResponse response = new HttpClientResponse(this, request, contentLength, responseChannel);
        result.setResult(response);

        // If there is no content to read, complete the request right away
        if(noContent) {
View Full Code Here

    @Override
    public IoFuture<byte[]> readRequestData() {
        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        final Pooled<ByteBuffer> pooled = exchange.getConnection().getBufferPool().allocate();
        final ByteBuffer buffer = pooled.getResource();
        final StreamSourceChannel channel = exchange.getRequestChannel();
        int res;
        for (; ; ) {
            try {
                res = channel.read(buffer);
                if (res == -1) {
                    return new FinishedIoFuture<byte[]>(data.toByteArray());
                } else if (res == 0) {
                    //callback
                    final ConcreteIoFuture<byte[]> future = new ConcreteIoFuture<>();
                    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;
                } else {
                    buffer.flip();
                    while (buffer.hasRemaining()) {
                        data.write(buffer.get());
View Full Code Here

                public void handleException(StreamSourceChannel channel, IOException exception) {
                    callback.failed();
                    IoUtils.safeClose(exchange.getConnection());
                }
            });
            StreamSourceChannel responseChannel = result.getResponseChannel();
            responseChannel.getReadSetter().set(listener);
            responseChannel.resumeReads();
            listener.handleEvent(responseChannel);
        }
View Full Code Here

            if (exchange.getAttachment(FORM_DATA) != null) {
                handler.handleRequest(exchange);
                return;
            }
            this.handler = handler;
            StreamSourceChannel channel = exchange.getRequestChannel();
            if (channel == null) {
                throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided());
            } else {
                doParse(channel);
                if (state != 4) {
                    channel.getReadSetter().set(this);
                    channel.resumeReads();
                } else {
                    HttpHandlers.executeRootHandler(handler, exchange, exchange.isInIoThread());
                }
            }
        }
View Full Code Here

            final FormData existing = exchange.getAttachment(FORM_DATA);
            if (existing != null) {
                return existing;
            }

            StreamSourceChannel channel = exchange.getRequestChannel();
            if (channel == null) {
                throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided());
            } else {
                while (state != 4) {
                    doParse(channel);
                    if (state != 4) {
                        channel.awaitReadable();
                    }
                }
            }
            return data;
        }
View Full Code Here

                return existing;
            }

            final MultipartParser.ParseState parser = MultipartParser.beginParse(exchange.getConnection().getBufferPool(), this, boundary.getBytes());
            final Pooled<ByteBuffer> resource = exchange.getConnection().getBufferPool().allocate();
            StreamSourceChannel requestChannel = exchange.getRequestChannel();
            if (requestChannel == null) {
                throw new IOException(UndertowMessages.MESSAGES.requestChannelAlreadyProvided());
            }
            final ByteBuffer buf = resource.getResource();
            try {
                while (!parser.isComplete()) {
                    buf.clear();
                    requestChannel.awaitReadable();
                    int c = requestChannel.read(buf);
                    buf.flip();
                    if (c == -1) {
                        throw UndertowMessages.MESSAGES.connectionTerminatedReadingMultiPartData();
                    } else if (c != 0) {
                        parser.parse(buf);
View Full Code Here

TOP

Related Classes of org.xnio.channels.StreamSourceChannel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.