Package org.asynchttpclient

Examples of org.asynchttpclient.Body


            }
        };
        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) {
                        }
                    }
                }
            }
View Full Code Here


    @SuppressWarnings({ "unchecked" })
    public boolean doHandle(final FilterChainContext ctx, final Request request, final HttpRequestPacket requestPacket) throws IOException {

        final BodyGenerator generator = request.getBodyGenerator();
        final Body bodyLocal = generator.createBody();
        final long len = bodyLocal.getContentLength();
        if (len >= 0) {
            requestPacket.setContentLengthLong(len);
        } else {
            requestPacket.setChunked(true);
        }

        final MemoryManager mm = ctx.getMemoryManager();
        boolean last = false;

        while (!last) {
            Buffer buffer = mm.allocate(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();
View Full Code Here

        final byte[] srcArray = new byte[srcArraySize];
        random.nextBytes(srcArray);

        final ByteArrayBodyGenerator babGen =
            new ByteArrayBodyGenerator(srcArray);
        final Body body = babGen.createBody();

        final ByteBuffer chunkBuffer = ByteBuffer.allocate(chunkSize);

        // should take 1 read to get through the srcArray
        assertEquals(body.read(chunkBuffer), srcArraySize);
        assertEquals(chunkBuffer.position(), srcArraySize, "bytes read");
        chunkBuffer.clear();

        assertEquals(body.read(chunkBuffer), -1, "body at EOF");
    }
View Full Code Here

        final byte[] srcArray = new byte[srcArraySize];
        random.nextBytes(srcArray);

        final ByteArrayBodyGenerator babGen =
            new ByteArrayBodyGenerator(srcArray);
        final Body body = babGen.createBody();

        final ByteBuffer chunkBuffer = ByteBuffer.allocate(chunkSize);

        int reads = 0;
        int bytesRead = 0;
        while (body.read(chunkBuffer) != -1) {
          reads += 1;
          bytesRead += chunkBuffer.position();
          chunkBuffer.clear();
        }
        assertEquals(reads, 4, "reads to drain generator");
View Full Code Here

    }

    private static void compareContentLength(final List<Part> parts) {
        Assert.assertNotNull(parts);
        // get expected values
        final Body multipartBody = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap());
        final long expectedContentLength = multipartBody.getContentLength();
        try {
            final ByteBuffer buffer = ByteBuffer.allocate(8192);
            boolean last = false;
            long totalBytes = 0;
            while (!last) {
                long readBytes = 0;
                try {
                    readBytes = multipartBody.read(buffer);
                } catch (IOException ie) {
                    Assert.fail("read failure");
                }
                if (readBytes > 0) {
                    totalBytes += readBytes;
                } else {
                    last = true;
                }
                buffer.clear();
            }
            Assert.assertEquals(totalBytes, expectedContentLength);
        } finally {
            try {
                multipartBody.close();
            } catch (IOException ignore) {
            }
        }
    }
View Full Code Here

TOP

Related Classes of org.asynchttpclient.Body

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.