Skip to content

Commit e4c5c77

Browse files
author
Stephane Landelle
committed
Port 6f60a45 on master
1 parent edd600d commit e4c5c77

File tree

2 files changed

+116
-9
lines changed

2 files changed

+116
-9
lines changed

api/src/main/java/org/asynchttpclient/multipart/MultipartBody.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public long read(ByteBuffer buffer) throws IOException {
7878
try {
7979
int overallLength = 0;
8080

81-
int maxLength = buffer.capacity();
81+
int maxLength = buffer.remaining();
8282

8383
if (startPart == parts.size() && endWritten) {
8484
return overallLength;
@@ -132,6 +132,7 @@ public long read(ByteBuffer buffer) throws IOException {
132132
initializeFileEnd(currentFilePart);
133133
} else if (fileLocation == FileLocation.END) {
134134
startPart++;
135+
fileLocation = FileLocation.NONE;
135136
if (startPart == parts.size() && currentStream.available() == 0) {
136137
doneWritingParts = true;
137138
}
@@ -146,6 +147,7 @@ public long read(ByteBuffer buffer) throws IOException {
146147
initializeFileEnd(currentFilePart);
147148
} else if (fileLocation == FileLocation.END) {
148149
startPart++;
150+
fileLocation = FileLocation.NONE;
149151
if (startPart == parts.size() && currentStream.available() == 0) {
150152
doneWritingParts = true;
151153
}
@@ -165,6 +167,7 @@ public long read(ByteBuffer buffer) throws IOException {
165167
initializeFileEnd(currentFilePart);
166168
} else if (fileLocation == FileLocation.END) {
167169
startPart++;
170+
fileLocation = FileLocation.NONE;
168171
if (startPart == parts.size() && currentStream.available() == 0) {
169172
doneWritingParts = true;
170173
}
@@ -202,7 +205,8 @@ public long read(ByteBuffer buffer) throws IOException {
202205
private void initializeByteArrayBody(FilePart filePart)
203206
throws IOException {
204207

205-
ByteArrayOutputStream output = generateByteArrayBody(filePart);
208+
ByteArrayOutputStream output = new ByteArrayOutputStream();
209+
filePart.sendData(output);
206210

207211
initializeBuffer(output);
208212

@@ -388,15 +392,9 @@ private StringPart generateClientStringpart(org.asynchttpclient.Part part) {
388392
private long handleByteArrayPart(WritableByteChannel target,
389393
FilePart filePart, byte[] data) throws IOException {
390394

391-
ByteArrayOutputStream output = generateByteArrayBody(filePart);
392-
return writeToTarget(target, output);
393-
}
394-
395-
private ByteArrayOutputStream generateByteArrayBody(FilePart filePart)
396-
throws IOException {
397395
ByteArrayOutputStream output = new ByteArrayOutputStream();
398396
Part.sendPart(output, filePart, boundary);
399-
return output;
397+
return writeToTarget(target, output);
400398
}
401399

402400
private long handleFileEnd(WritableByteChannel target, FilePart filePart)
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2013 Sonatype, Inc. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
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.
7+
*
8+
* Unless required by applicable law or agreed to in writing,
9+
* software distributed under the Apache License Version 2.0 is distributed on an
10+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12+
*/
13+
package org.asynchttpclient.multipart;
14+
15+
import org.asynchttpclient.*;
16+
import org.asynchttpclient.Part;
17+
import org.asynchttpclient.util.AsyncHttpProviderUtils;
18+
import org.testng.Assert;
19+
import org.testng.annotations.Test;
20+
21+
import java.io.File;
22+
import java.io.FileNotFoundException;
23+
import java.io.IOException;
24+
import java.io.UnsupportedEncodingException;
25+
import java.net.URISyntaxException;
26+
import java.net.URL;
27+
import java.nio.ByteBuffer;
28+
import java.util.ArrayList;
29+
import java.util.List;
30+
31+
public class MultipartBodyTest {
32+
33+
@Test(groups = "fast")
34+
public void testBasics() {
35+
final List<Part> parts = new ArrayList<Part>();
36+
37+
// add a file
38+
final File testFile = getTestfile();
39+
try {
40+
parts.add(new FilePart("filePart", testFile));
41+
} catch (FileNotFoundException fne) {
42+
Assert.fail("file not found: " + testFile);
43+
}
44+
45+
// add a byte array
46+
try {
47+
parts.add(new ByteArrayPart("baPart", "fileName", "testMultiPart".getBytes("utf-8"), "application/test", "utf-8"));
48+
} catch (UnsupportedEncodingException ignore) {
49+
}
50+
51+
// add a string
52+
parts.add(new StringPart("stringPart", "testString", "utf-8"));
53+
54+
compareContentLength(parts);
55+
}
56+
57+
private static File getTestfile() {
58+
final ClassLoader cl = MultipartBodyTest.class.getClassLoader();
59+
final URL url = cl.getResource("textfile.txt");
60+
Assert.assertNotNull(url);
61+
File file = null;
62+
try {
63+
file = new File(url.toURI());
64+
} catch (URISyntaxException use) {
65+
Assert.fail("uri syntax error");
66+
}
67+
return file;
68+
}
69+
70+
private static void compareContentLength(final List<Part> parts) {
71+
Assert.assertNotNull(parts);
72+
// get expected values
73+
MultipartRequestEntity mre = null;
74+
try {
75+
mre = AsyncHttpProviderUtils.createMultipartRequestEntity(parts, new FluentCaseInsensitiveStringsMap());
76+
} catch (FileNotFoundException fne) {
77+
Assert.fail("file not found: " + parts);
78+
}
79+
final long expectedContentLength = mre.getContentLength();
80+
81+
// get real bytes
82+
final Body multipartBody = new MultipartBody(parts, mre.getContentType(), String.valueOf(expectedContentLength));
83+
try {
84+
final ByteBuffer buffer = ByteBuffer.allocate(8192);
85+
boolean last = false;
86+
long totalBytes = 0;
87+
while (!last) {
88+
long readBytes = 0;
89+
try {
90+
readBytes = multipartBody.read(buffer);
91+
} catch (IOException ie) {
92+
Assert.fail("read failure");
93+
}
94+
if (readBytes >= 0) {
95+
totalBytes += readBytes;
96+
} else {
97+
last = true;
98+
}
99+
buffer.clear();
100+
}
101+
Assert.assertEquals(totalBytes, expectedContentLength);
102+
} finally {
103+
try {
104+
multipartBody.close();
105+
} catch (IOException ignore) {
106+
}
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)