Refactoring UrlLoader
@@ -78,35 +78,47 @@ public abstract class UrlLoader {
throw new IOException("Unexpected http response loading " + url + " : " + rc + " " + con.getResponseMessage());
}
}
- long lm = connection.getLastModified();
- if (lm > 0 && lm <= lastModified) {
+
+ if (didNotChange(connection)) {
lastChecked = time;
return false;
}
+ boolean wasRead = read(connection);
+ lastModified = connection.getLastModified();
+ lastChecked = time;
+ return wasRead;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private boolean didNotChange(URLConnection connection) {
+ long lm = connection.getLastModified();
+ return lm > 0 && lm <= lastModified;
+ }
+
+ private boolean read(URLConnection connection) throws IOException {
InputStream is = null;
try {
is = new BufferedInputStream(connection.getInputStream());
- // Auto-detect gzipped streams
- is.mark(512);
- int b0 = is.read();
- int b1 = is.read();
- is.reset();
- if (b0 == 0x1f && b1 == 0x8b) {
+ if (isGzipStream(is)) {
is = new GZIPInputStream(is);
}
- boolean r = doRead(is);
- lastModified = lm;
- lastChecked = time;
- return r;
+ return doRead(is);
} finally {
// cannot be use try-with-resources, as it would not close GZIPInpuStream
if (is != null) {
is.close();
}
}
- } catch (IOException e) {
- throw new RuntimeException(e);
}
+
+ private boolean isGzipStream(InputStream is) throws IOException {
+ is.mark(512);
+ int b0 = is.read();
+ int b1 = is.read();
+ is.reset();
+ return (b0 == 0x1f && b1 == 0x8b);
}
protected abstract boolean doRead(InputStream is) throws IOException;
