Skip to content

Commit 50aeda0

Browse files
committed
File part is missing from transferred bytes sum, close AsyncHttpClient#1126
1 parent 5b51e2e commit 50aeda0

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,21 @@ public long write(WritableByteChannel target, byte[] boundary) throws IOExceptio
108108
RandomAccessFile raf = new RandomAccessFile(file, "r");
109109
FileChannel fc = raf.getChannel();
110110

111-
long l = file.length();
112-
int fileLength = 0;
113-
long nWrite = 0;
111+
long actualFileLength = file.length();
112+
long transferredFileBytes = 0;
114113
// FIXME why sync?
115114
try {
116115
synchronized (fc) {
117-
while (fileLength != l) {
116+
while (transferredFileBytes != actualFileLength) {
117+
long written = 0;
118+
118119
if (handler.isFailed()) {
119120
LOGGER.debug("Stalled error");
120121
throw new FileUploadStalledException();
121122
}
122123
try {
123-
nWrite = fc.transferTo(fileLength, l, target);
124-
125-
if (nWrite == 0) {
124+
written = fc.transferTo(transferredFileBytes, actualFileLength, target);
125+
if (written == 0) {
126126
LOGGER.info("Waiting for writing...");
127127
try {
128128
fc.wait(50);
@@ -148,14 +148,15 @@ public long write(WritableByteChannel target, byte[] boundary) throws IOExceptio
148148
throw ex;
149149
}
150150
}
151-
fileLength += nWrite;
151+
transferredFileBytes += written;
152152
}
153153
}
154154
} finally {
155155
handler.completed();
156156
raf.close();
157157
}
158158

159+
length += transferredFileBytes;
159160
length += MultipartUtils.writeBytesToChannel(target, generateFileEnd());
160161

161162
return length;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public byte[] getBoundary() {
7777
public long transferTo(long position, WritableByteChannel target) throws IOException {
7878

7979
if (transfertDone) {
80-
return -1;
80+
throw new UnsupportedOperationException("Transfer is already done");
8181
}
8282

8383
long overallLength = 0;

0 commit comments

Comments
 (0)