From aed5163a832c6bdd237f65009e560d5c3dbff16d Mon Sep 17 00:00:00 2001 From: Jeffrey Han Date: Sun, 30 Aug 2015 23:18:46 -0500 Subject: [PATCH] Check completed download size against Content-Length header. If the number of bytes received is less than the reported content length (e.g. a network timeout), mark the download with the "error" status instead of "complete". Content-Length should be reliable if reported at all, so this should be a valid approach. Signed-off-by: Jeffrey Han --- src/itdelatrisu/opsu/downloads/Download.java | 15 ++++++++++++--- .../opsu/downloads/servers/MengSkyServer.java | 4 +--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/itdelatrisu/opsu/downloads/Download.java b/src/itdelatrisu/opsu/downloads/Download.java index a3f62390..09ebe292 100644 --- a/src/itdelatrisu/opsu/downloads/Download.java +++ b/src/itdelatrisu/opsu/downloads/Download.java @@ -245,9 +245,18 @@ public class Download { fos = fileOutputStream; status = Status.DOWNLOADING; updateReadSoFar(); - fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); + long bytesRead = fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); if (status == Status.DOWNLOADING) { // not interrupted - // TODO: if connection is lost before a download finishes, it's still marked as "complete" + // check if the entire file was received + if (bytesRead < contentLength) { + status = Status.ERROR; + Log.warn(String.format("Download '%s' failed: %d bytes expected, %d bytes received.", url.toString(), contentLength, bytesRead)); + if (listener != null) + listener.error(); + return; + } + + // mark download as complete status = Status.COMPLETE; rbc.close(); fos.close(); @@ -320,7 +329,7 @@ public class Download { public long readSoFar() { switch (status) { case COMPLETE: - return contentLength; + return (rbc != null) ? rbc.getReadSoFar() : contentLength; case DOWNLOADING: if (rbc != null) return rbc.getReadSoFar(); diff --git a/src/itdelatrisu/opsu/downloads/servers/MengSkyServer.java b/src/itdelatrisu/opsu/downloads/servers/MengSkyServer.java index 829a9094..ce4647be 100644 --- a/src/itdelatrisu/opsu/downloads/servers/MengSkyServer.java +++ b/src/itdelatrisu/opsu/downloads/servers/MengSkyServer.java @@ -174,9 +174,7 @@ public class MengSkyServer extends DownloadServer { continue; } - DownloadNode node = new DownloadNode(id, date, title, titleUnicode, artist, artistUnicode, creator); - System.out.println(node); - nodeList.add(node); + nodeList.add(new DownloadNode(id, date, title, titleUnicode, artist, artistUnicode, creator)); } nodes = nodeList.toArray(new DownloadNode[nodeList.size()]);