Skip to content

Commit

Permalink
Merge pull request #258 from jcarvalho/handle-misbehaving-inputstreams
Browse files Browse the repository at this point in the history
[io] DownloadUtil can now handle misbehaving InputStreams
  • Loading branch information
pedrosan7os committed Dec 10, 2015
2 parents 150722d + 55d8e8e commit 606357f
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,18 @@ public static void downloadFile(GenericFile file, HttpServletRequest request, Ht
private static void copyStream(InputStream in, OutputStream out, long start, long bytesToRead) throws IOException {
ByteStreams.skipFully(in, start);
byte buffer[] = new byte[BUF_SIZE];
int len = buffer.length;
while ((bytesToRead > 0) && (len >= buffer.length)) {
len = in.read(buffer);
while (bytesToRead > 0) {
int len = in.read(buffer);
if (len == -1) {
break;
}
if (bytesToRead >= len) {
out.write(buffer, 0, len);
bytesToRead -= len;
} else {
out.write(buffer, 0, (int) bytesToRead);
bytesToRead = 0;
}
if (len < buffer.length) {
break;
}
}
}

Expand Down

0 comments on commit 606357f

Please sign in to comment.