From fc8213493e9c03e6846bc6351cfd0f8560de8848 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Wed, 16 Jun 2004 09:44:23 +0000 Subject: [PATCH] Alas, such a simple fix isn't going to do the job. We need to be smarter about handling resources that didn't provide a content length. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@3030 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/resource/Downloader.java | 19 ++++++++++++++--- .../threerings/resource/FileDownloader.java | 8 +++---- .../threerings/resource/JNLPDownloader.java | 21 ++++++++++++------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/java/com/threerings/resource/Downloader.java b/src/java/com/threerings/resource/Downloader.java index 29f93c1ae..e08965d81 100644 --- a/src/java/com/threerings/resource/Downloader.java +++ b/src/java/com/threerings/resource/Downloader.java @@ -1,5 +1,5 @@ // -// $Id: Downloader.java,v 1.3 2004/03/06 11:29:19 mdb Exp $ +// $Id: Downloader.java,v 1.4 2004/06/16 09:44:23 mdb Exp $ package com.threerings.resource; @@ -116,14 +116,24 @@ public abstract class Downloader // info on potentially partially downloaded data; if so, use a // "Range: bytes=HAVE-" header. + // if we were unable to determine our content length, record a + // single "byte" of progress to indicate that we've started to + // download this file + if (_contentLength <= 0) { + pinfo.currentSize += 1; + } + // read in the file data while ((read = in.read(buffer)) != -1) { // write it out to our local copy out.write(buffer, 0, read); - // report our progress to the download observer as a + // if we know we added something to the total download size, + // then report our progress to the download observer as a // percentage of the total file data to be transferred - pinfo.currentSize += read; + if (_contentLength > 0) { + pinfo.currentSize += read; + } // update our percent completion; if we're totally done, hold // off on notifying the observer until the download manager @@ -157,6 +167,9 @@ public abstract class Downloader /** The descriptor that we're downloading. */ protected DownloadDescriptor _desc; + /** We need this to cope gracefully with a missing content-length. */ + protected long _contentLength; + /** The last-modified difference in milliseconds allowed between the * source and destination file without considering the destination * file to be out of date. */ diff --git a/src/java/com/threerings/resource/FileDownloader.java b/src/java/com/threerings/resource/FileDownloader.java index 08f782da7..8ee914227 100644 --- a/src/java/com/threerings/resource/FileDownloader.java +++ b/src/java/com/threerings/resource/FileDownloader.java @@ -1,5 +1,5 @@ // -// $Id: FileDownloader.java,v 1.1 2003/08/05 01:33:20 mdb Exp $ +// $Id: FileDownloader.java,v 1.2 2004/06/16 09:44:23 mdb Exp $ package com.threerings.resource; @@ -17,12 +17,12 @@ public class FileDownloader extends Downloader { // read the file information directly from the file system File tfile = new File(_desc.sourceURL.getPath()); - long fileSize = tfile.length(); + _contentLength = tfile.length(); _desc.lastModified = tfile.lastModified(); - if (compareWithLocal(fileSize, _desc.lastModified)) { + if (compareWithLocal(_contentLength, _desc.lastModified)) { // increment the total file size to be fetched - info.totalSize += fileSize; + info.totalSize += _contentLength; Log.debug("File deemed stale [url=" + _desc.sourceURL + "]."); return true; } else { diff --git a/src/java/com/threerings/resource/JNLPDownloader.java b/src/java/com/threerings/resource/JNLPDownloader.java index d6938e7c6..b1435a23d 100644 --- a/src/java/com/threerings/resource/JNLPDownloader.java +++ b/src/java/com/threerings/resource/JNLPDownloader.java @@ -1,5 +1,5 @@ // -// $Id: JNLPDownloader.java,v 1.17 2004/06/16 09:25:52 mdb Exp $ +// $Id: JNLPDownloader.java,v 1.18 2004/06/16 09:44:23 mdb Exp $ package com.threerings.resource; @@ -95,13 +95,18 @@ public class JNLPDownloader extends Downloader throw new IOException(errmsg); } - // if we're versioning, we know we need an update; force the - // content length to be at least 1 so that we behave marginally - // sensibly (ticking off each file as we download it) if our - // server doesn't provide content-length for some assinine reason - int cl = Math.max(ucon.getContentLength(), 1); - info.totalSize += cl; - Log.info(getResourceURL() + " requires " + cl + " byte update."); + // determine our content length + _contentLength = ucon.getContentLength(); + if (_contentLength > 0) { + // and record it to the total size if it is a sane value + info.totalSize += _contentLength; + } else { + // if it's not sane, record a single byte which we will then + // properly interepret later during actual downloading + info.totalSize += 1; + } + Log.info(getResourceURL() + " requires " + _contentLength + + " byte update."); return true; }