Skip to content

Commit

Permalink
Improve FileTransfer.writeToStream()
Browse files Browse the repository at this point in the history
let's use the standard idiom for Input- to OutputStream transfers. This
also avoids an initial no-op on the first write, when the count is '0'.

Also fixes a bug when the size of file/stream transferred is '0' (which
is perfectly fine and possible).
  • Loading branch information
Flowdalic committed Sep 9, 2014
1 parent 090f7cf commit d8db43e
Showing 1 changed file with 4 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,10 @@ protected void writeToStream(final InputStream in, final OutputStream out)
int count = 0;
amountWritten = 0;

do {
// write to the output stream
out.write(b, 0, count);

amountWritten += count;

// read more bytes from the input stream
count = in.read(b);
} while (count != -1 && !getStatus().equals(Status.cancelled));
while ((count = in.read(b)) > 0 && !getStatus().equals(Status.cancelled)) {
out.write(b, 0, count);
amountWritten += count;
}

// the connection was likely terminated abruptly if these are not equal
if (!getStatus().equals(Status.cancelled) && getError() == Error.none
Expand Down

0 comments on commit d8db43e

Please sign in to comment.