You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2016/05/12 11:59:18 UTC

svn commit: r1743493 - /sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java

Author: bdelacretaz
Date: Thu May 12 11:59:18 2016
New Revision: 1743493

URL: http://svn.apache.org/viewvc?rev=1743493&view=rev
Log:
SLING-5715 - improve HttpURLConnection cleanup

Modified:
    sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java

Modified: sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java?rev=1743493&r1=1743492&r2=1743493&view=diff
==============================================================================
--- sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java (original)
+++ sling/trunk/testing/junit/teleporter/src/main/java/org/apache/sling/testing/teleporter/client/TeleporterHttpClient.java Thu May 12 11:59:18 2016
@@ -105,7 +105,7 @@ class TeleporterHttpClient {
                 throw new IOException("Got status code " + status + " for " + url);
             }
         } finally {
-            c.disconnect();
+            cleanup(c);
         }
     }
 
@@ -127,7 +127,7 @@ class TeleporterHttpClient {
                 throw new IOException("Got status code " + status + " for " + url);
             }
         } finally {
-            c.disconnect();
+            cleanup(c);
         }
     }
     
@@ -141,7 +141,7 @@ class TeleporterHttpClient {
         try {
             return c.getResponseCode();
         } finally {
-            c.disconnect();
+            cleanup(c);
         }
     }
 
@@ -186,7 +186,30 @@ class TeleporterHttpClient {
         } catch(ClassNotFoundException e) {
             throw new IOException("Exception reading test results:" + e, e);
         } finally {
-            c.disconnect();
+            cleanup(c);
         }
     }
+    
+    private void consumeAndClose(InputStream is) throws IOException {
+        if(is == null) {
+            return;
+        }
+        final byte [] buffer = new byte[16384];
+        while(is.read(buffer) != -1) {
+            // nothing to do, just consume the stream
+        }
+        is.close();
+    }
+    
+    private void cleanup(HttpURLConnection c) {
+        try {
+            consumeAndClose(c.getInputStream());
+        } catch(IOException ignored) {
+        }
+        try {
+            consumeAndClose(c.getErrorStream());
+        } catch(IOException ignored) {
+        }
+        c.disconnect();
+    }
 }