|
34 | 34 |
|
35 | 35 | public class MultipartBodyTest {
|
36 | 36 |
|
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 |
| - } |
| 37 | + private static final List<Part> PARTS = new ArrayList<>(); |
44 | 38 |
|
45 |
| - @Test |
46 |
| - public void transferZeroCopy() throws Exception { |
47 |
| - try (MultipartBody multipartBody = buildMultipart()) { |
48 |
| - long tranferred = transferZeroCopy(multipartBody); |
49 |
| - assertEquals(tranferred, multipartBody.getContentLength()); |
| 39 | + static { |
| 40 | + try { |
| 41 | + PARTS.add(new FilePart("filePart", getTestfile())); |
| 42 | + } catch (URISyntaxException e) { |
| 43 | + throw new ExceptionInInitializerError(e); |
50 | 44 | }
|
| 45 | + PARTS.add(new ByteArrayPart("baPart", "testMultiPart".getBytes(UTF_8), "application/test", UTF_8, "fileName")); |
| 46 | + PARTS.add(new StringPart("stringPart", "testString")); |
51 | 47 | }
|
52 | 48 |
|
53 |
| - private File getTestfile() throws URISyntaxException { |
| 49 | + private static File getTestfile() throws URISyntaxException { |
54 | 50 | final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
|
55 | 51 | final URL url = cl.getResource("textfile.txt");
|
56 | 52 | assertNotNull(url);
|
57 | 53 | return new File(url.toURI());
|
58 | 54 | }
|
59 | 55 |
|
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); |
| 56 | + private static long MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE; |
| 57 | + |
| 58 | + static { |
| 59 | + try (MultipartBody dummyBody = buildMultipart()) { |
| 60 | + // separator is random |
| 61 | + MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE = dummyBody.getContentLength() + 100; |
| 62 | + } catch (IOException e) { |
| 63 | + throw new ExceptionInInitializerError(e); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private static MultipartBody buildMultipart() { |
| 68 | + return MultipartUtils.newMultipartBody(PARTS, HttpHeaders.EMPTY_HEADERS); |
66 | 69 | }
|
67 | 70 |
|
68 |
| - private long transferWithCopy(MultipartBody multipartBody) throws IOException { |
69 |
| - final ByteBuf buffer = Unpooled.buffer(8192); |
70 |
| - while (multipartBody.transferTo(buffer) != BodyState.STOP) { |
| 71 | + @Test |
| 72 | + public void transferWithCopy() throws Exception { |
| 73 | + for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) { |
| 74 | + try (MultipartBody multipartBody = buildMultipart()) { |
| 75 | + long tranferred = transferWithCopy(multipartBody, bufferLength); |
| 76 | + assertEquals(tranferred, multipartBody.getContentLength()); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void transferZeroCopy() throws Exception { |
| 83 | + for (int bufferLength = 1; bufferLength < MAX_MULTIPART_CONTENT_LENGTH_ESTIMATE + 1; bufferLength++) { |
| 84 | + try (MultipartBody multipartBody = buildMultipart()) { |
| 85 | + long tranferred = transferZeroCopy(multipartBody, bufferLength); |
| 86 | + assertEquals(tranferred, multipartBody.getContentLength()); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private static long transferWithCopy(MultipartBody multipartBody, int bufferSize) throws IOException { |
| 92 | + long transferred = 0; |
| 93 | + final ByteBuf buffer = Unpooled.buffer(bufferSize); |
| 94 | + try { |
| 95 | + while (multipartBody.transferTo(buffer) != BodyState.STOP) { |
| 96 | + transferred += buffer.readableBytes(); |
| 97 | + buffer.clear(); |
| 98 | + } |
| 99 | + return transferred; |
| 100 | + } finally { |
| 101 | + buffer.release(); |
71 | 102 | }
|
72 |
| - return buffer.readableBytes(); |
73 | 103 | }
|
74 | 104 |
|
75 |
| - private static long transferZeroCopy(MultipartBody multipartBody) throws IOException { |
| 105 | + private static long transferZeroCopy(MultipartBody multipartBody, int bufferSize) throws IOException { |
76 | 106 |
|
77 |
| - final ByteBuffer buffer = ByteBuffer.allocate(8192); |
| 107 | + final ByteBuffer buffer = ByteBuffer.allocate(bufferSize); |
78 | 108 | final AtomicLong transferred = new AtomicLong();
|
79 | 109 |
|
80 | 110 | WritableByteChannel mockChannel = new WritableByteChannel() {
|
|
0 commit comments