Skip to content

Commit 84040f7

Browse files
committed
Incremental fix for AsyncHttpClient#128. ResumableRandomAccessFileListener::onBytesReceived() was passing in the array which backed the ByteBuffer, but didn't use the array offset for the initial position.
Added additional logic to perform a copy before the file write if the ByteBuffer provided returned false when calling ByteBuffer::hasArray().
1 parent a2093ad commit 84040f7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

api/src/main/java/com/ning/http/client/extra/ResumableRandomAccessFileListener.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ public ResumableRandomAccessFileListener(RandomAccessFile file) {
3737
*/
3838
public void onBytesReceived(ByteBuffer buffer) throws IOException {
3939
file.seek(file.length());
40-
file.write(buffer.array());
40+
if (buffer.hasArray()) {
41+
file.write(buffer.array(), buffer.arrayOffset(), buffer.remaining());
42+
} else { // if the buffer is direct or backed by a String...
43+
byte[] b = new byte[buffer.remaining()];
44+
int pos = buffer.position();
45+
buffer.get(b);
46+
buffer.position(pos);
47+
file.write(b);
48+
}
4149
}
4250

4351
/**

0 commit comments

Comments
 (0)