You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by cs...@apache.org on 2017/07/04 09:57:02 UTC

karaf git commit: Refactoring UrlLoader

Repository: karaf
Updated Branches:
  refs/heads/master 25771e1ea -> 28c0864a9


Refactoring UrlLoader


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/28c0864a
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/28c0864a
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/28c0864a

Branch: refs/heads/master
Commit: 28c0864a90e5ca4e252270f6444855a72de874a5
Parents: 25771e1
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Tue Jul 4 11:56:55 2017 +0200
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Tue Jul 4 11:56:55 2017 +0200

----------------------------------------------------------------------
 .../features/internal/repository/UrlLoader.java | 58 ++++++++++++--------
 1 file changed, 35 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/28c0864a/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
----------------------------------------------------------------------
diff --git a/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java b/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
index 433b39b..41652f0 100644
--- a/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
+++ b/features/core/src/main/java/org/apache/karaf/features/internal/repository/UrlLoader.java
@@ -78,37 +78,49 @@ 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;
             }
-            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) {
-                    is = new GZIPInputStream(is);
-                }
-                boolean r = doRead(is);
-                lastModified = lm;
-                lastChecked = time;
-                return r;
-            } finally {
-                // cannot be use try-with-resources, as it would not close GZIPInpuStream
-                if (is != null) {
-                    is.close();
-                }
-            }
+            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());
+            if (isGzipStream(is)) {
+                is = new GZIPInputStream(is);
+            }
+            return doRead(is);
+        } finally {
+            // cannot be use try-with-resources, as it would not close GZIPInpuStream
+            if (is != null) {
+                is.close();
+            }
+        }
+    }
+
+    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;
 
 }