Skip to content

Commit 88d582e

Browse files
carryelslandelle
authored andcommitted
Fixed "Sometimes multipart body missed last 1 byte." AsyncHttpClient#1271 and added the testcase. (AsyncHttpClient#1275)
1 parent da4e4af commit 88d582e

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/main/java/com/ning/http/client/multipart/MultipartBody.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public long read(ByteBuffer buffer) throws IOException {
183183
}
184184

185185
private boolean currentBytesFullyRead() {
186-
return currentBytes == null || currentBytesPosition >= currentBytes.length - 1;
186+
return currentBytes == null || currentBytesPosition == -1;
187187
}
188188

189189
private void initializeFileBody(AbstractFilePart part) throws IOException {

src/test/java/com/ning/http/client/multipart/MultipartBodyTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ public void transferWithCopy() throws IOException {
4040
}
4141
}
4242

43+
@Test
44+
public void transferWithCopy2() throws IOException {
45+
try (final MultipartBody multipartBody = buildMultipart()) {
46+
final long contentLength = multipartBody.getContentLength();
47+
final int bufferSize = (int)contentLength - 1;
48+
final long tranferred = transferWithCopy(multipartBody, bufferSize);
49+
Assert.assertEquals(tranferred, contentLength);
50+
}
51+
}
52+
4353
@Test
4454
public void transferZeroCopy() throws IOException {
4555
try (MultipartBody multipartBody = buildMultipart()) {
@@ -70,8 +80,11 @@ private static File getTestfile() {
7080
}
7181

7282
private static long transferWithCopy(MultipartBody multipartBody) throws IOException {
83+
return transferWithCopy(multipartBody, 8192);
84+
}
7385

74-
final ByteBuffer buffer = ByteBuffer.allocate(8192);
86+
private static long transferWithCopy(MultipartBody multipartBody, int bufferSize) throws IOException {
87+
final ByteBuffer buffer = ByteBuffer.allocate(bufferSize);
7588
long totalBytes = 0;
7689
while (true) {
7790
long readBytes = multipartBody.read(buffer);

0 commit comments

Comments
 (0)