|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2013 Sonatype, Inc. All rights reserved. |
| 2 | + * Copyright (c) 2016 AsyncHttpClient Project. All rights reserved. |
3 | 3 | *
|
4 | 4 | * This program is licensed to you under the Apache License Version 2.0,
|
5 | 5 | * and you may not use this file except in compliance with the Apache License Version 2.0.
|
6 |
| - * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0. |
7 | 8 | *
|
8 | 9 | * Unless required by applicable law or agreed to in writing,
|
9 | 10 | * software distributed under the Apache License Version 2.0 is distributed on an
|
|
22 | 23 | import java.io.IOException;
|
23 | 24 | import java.net.URISyntaxException;
|
24 | 25 | import java.net.URL;
|
| 26 | +import java.nio.ByteBuffer; |
| 27 | +import java.nio.channels.WritableByteChannel; |
25 | 28 | import java.util.ArrayList;
|
26 | 29 | import java.util.List;
|
| 30 | +import java.util.concurrent.atomic.AtomicLong; |
27 | 31 |
|
28 |
| -import org.apache.commons.io.IOUtils; |
29 | 32 | import org.asynchttpclient.request.body.Body.BodyState;
|
30 | 33 | import org.testng.annotations.Test;
|
31 | 34 |
|
32 | 35 | public class MultipartBodyTest {
|
33 | 36 |
|
34 |
| - @Test(groups = "standalone") |
35 |
| - public void testBasics() throws Exception { |
36 |
| - final List<Part> parts = new ArrayList<>(); |
37 |
| - |
38 |
| - // add a file |
39 |
| - final File testFile = getTestfile(); |
40 |
| - System.err.println(testFile.length()); |
41 |
| - parts.add(new FilePart("filePart", testFile)); |
42 |
| - |
43 |
| - // add a byte array |
44 |
| - parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")); |
45 |
| - |
46 |
| - // add a string |
47 |
| - parts.add(new StringPart("stringPart", "testString")); |
| 37 | + @Test |
| 38 | + public void transferWithCopy() throws Exception { |
| 39 | + try (MultipartBody multipartBody = buildMultipart()) { |
| 40 | + long tranferred = transferWithCopy(multipartBody); |
| 41 | + assertEquals(tranferred, multipartBody.getContentLength()); |
| 42 | + } |
| 43 | + } |
48 | 44 |
|
49 |
| - compareContentLength(parts); |
| 45 | + @Test |
| 46 | + public void transferZeroCopy() throws Exception { |
| 47 | + try (MultipartBody multipartBody = buildMultipart()) { |
| 48 | + long tranferred = transferZeroCopy(multipartBody); |
| 49 | + assertEquals(tranferred, multipartBody.getContentLength()); |
| 50 | + } |
50 | 51 | }
|
51 | 52 |
|
52 |
| - private static File getTestfile() throws URISyntaxException { |
| 53 | + private File getTestfile() throws URISyntaxException { |
53 | 54 | final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
|
54 | 55 | final URL url = cl.getResource("textfile.txt");
|
55 | 56 | assertNotNull(url);
|
56 | 57 | return new File(url.toURI());
|
57 | 58 | }
|
58 | 59 |
|
59 |
| - private static void compareContentLength(final List<Part> parts) throws IOException { |
60 |
| - assertNotNull(parts); |
61 |
| - // get expected values |
62 |
| - final MultipartBody multipartBody = MultipartUtils.newMultipartBody(parts, HttpHeaders.EMPTY_HEADERS); |
63 |
| - final long expectedContentLength = multipartBody.getContentLength(); |
64 |
| - try { |
65 |
| - final ByteBuf buffer = Unpooled.buffer(8192); |
66 |
| - while (multipartBody.transferTo(buffer) != BodyState.STOP) { |
| 60 | + private MultipartBody buildMultipart() throws URISyntaxException { |
| 61 | + List<Part> parts = new ArrayList<>(); |
| 62 | + parts.add(new FilePart("filePart", getTestfile())); |
| 63 | + parts.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")); |
| 64 | + parts.add(new StringPart("stringPart", "testString")); |
| 65 | + return MultipartUtils.newMultipartBody(parts, HttpHeaders.EMPTY_HEADERS); |
| 66 | + } |
| 67 | + |
| 68 | + private long transferWithCopy(MultipartBody multipartBody) throws IOException { |
| 69 | + final ByteBuf buffer = Unpooled.buffer(8192); |
| 70 | + while (multipartBody.transferTo(buffer) != BodyState.STOP) { |
| 71 | + } |
| 72 | + return buffer.readableBytes(); |
| 73 | + } |
| 74 | + |
| 75 | + private static long transferZeroCopy(MultipartBody multipartBody) throws IOException { |
| 76 | + |
| 77 | + final ByteBuffer buffer = ByteBuffer.allocate(8192); |
| 78 | + final AtomicLong transferred = new AtomicLong(); |
| 79 | + |
| 80 | + WritableByteChannel mockChannel = new WritableByteChannel() { |
| 81 | + @Override |
| 82 | + public boolean isOpen() { |
| 83 | + return true; |
67 | 84 | }
|
68 |
| - assertEquals(buffer.readableBytes(), expectedContentLength); |
69 |
| - } finally { |
70 |
| - IOUtils.closeQuietly(multipartBody); |
| 85 | + |
| 86 | + @Override |
| 87 | + public void close() throws IOException { |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int write(ByteBuffer src) throws IOException { |
| 92 | + int written = src.remaining(); |
| 93 | + transferred.set(transferred.get() + written); |
| 94 | + src.position(src.limit()); |
| 95 | + return written; |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + while (transferred.get() < multipartBody.getContentLength()) { |
| 100 | + multipartBody.transferTo(mockChannel); |
| 101 | + buffer.clear(); |
71 | 102 | }
|
| 103 | + return transferred.get(); |
72 | 104 | }
|
73 | 105 | }
|
0 commit comments