Package org.asynchttpclient.providers.grizzly

Examples of org.asynchttpclient.providers.grizzly.FeedableBodyGenerator


        if (GrizzlyAsyncHttpProvider.LOGGER.isDebugEnabled()) {
            GrizzlyAsyncHttpProvider.LOGGER.debug("REQUEST(modified): contentLength={}, contentType={}",
                    new Object[] { requestPacket.getContentLength(), requestPacket.getContentType() });
        }

        final FeedableBodyGenerator generator = new FeedableBodyGenerator() {
            @Override
            public Body createBody() throws IOException {
                return multipartBody;
            }
        };
        generator.setFeeder(new FeedableBodyGenerator.BaseFeeder(generator) {
            @Override
            public void flush() throws IOException {
                final Body bodyLocal = feedableBodyGenerator.createBody();
                try {
                    final MemoryManager mm = ctx.getMemoryManager();
                    boolean last = false;
                    while (!last) {
                        Buffer buffer = mm.allocate(BodyHandler.MAX_CHUNK_SIZE);
                        buffer.allowBufferDispose(true);
                        final long readBytes = bodyLocal.read(buffer.toByteBuffer());
                        if (readBytes > 0) {
                            buffer.position((int) readBytes);
                            buffer.trim();
                        } else {
                            buffer.dispose();
                            if (readBytes < 0) {
                                last = true;
                                buffer = Buffers.EMPTY_BUFFER;
                            } else {
                                throw new IllegalStateException("MultipartBody unexpectedly returned 0 bytes available");
                            }
                        }
                        feed(buffer, last);
                    }
                } finally {
                    if (bodyLocal != null) {
                        try {
                            bodyLocal.close();
                        } catch (IOException ignore) {
                        }
                    }
                }
            }
        });
        generator.initializeAsynchronousTransfer(ctx, requestPacket);
        return false;
    }
View Full Code Here

TOP

Related Classes of org.asynchttpclient.providers.grizzly.FeedableBodyGenerator

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.