ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maart...@apache.org
Subject svn commit: r798314 - /ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
Date Mon, 27 Jul 2009 22:32:28 GMT
Author: maartenc
Date: Mon Jul 27 22:32:28 2009
New Revision: 798314

URL: http://svn.apache.org/viewvc?rev=798314&view=rev
Log:
Trying to minimize the number of simultaneous open connections by following the guidelines
mentioned in http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html (IVY-1105)

Modified:
    ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java

Modified: ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java
URL: http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java?rev=798314&r1=798313&r2=798314&view=diff
==============================================================================
--- ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java (original)
+++ ant/ivy/core/trunk/src/java/org/apache/ivy/util/url/BasicURLHandler.java Mon Jul 27 22:32:28
2009
@@ -143,7 +143,7 @@
             disconnect(conn);
         }
     }
-
+    
     public void download(URL src, File dest, CopyProgressListener l) throws IOException {
         URLConnection srcConn = null;
         try {
@@ -211,6 +211,14 @@
 
     private void disconnect(URLConnection con) {
         if (con instanceof HttpURLConnection) {
+            if (!"HEAD".equals(((HttpURLConnection) con).getRequestMethod())) {
+                // We must read the response body before disconnecting!
+                // Cfr. http://java.sun.com/j2se/1.5.0/docs/guide/net/http-keepalive.html
+                // [quote]Do not abandon a connection by ignoring the response body. Doing
+                // so may results in idle TCP connections.[/quote]
+                readResponseBody((HttpURLConnection) con);
+            }
+            
             ((HttpURLConnection) con).disconnect();
         } else if (con != null
                 && "sun.net.www.protocol.file.FileURLConnection".equals(con.getClass().getName()))
{
@@ -227,4 +235,29 @@
         }
     }
 
+    private void readResponseBody(HttpURLConnection conn) {
+        byte[] buffer = new byte[BUFFER_SIZE];
+        
+        try {
+            InputStream inStream = conn.getInputStream();
+            while (inStream.read(buffer) > 0) {
+            }
+            inStream.close();
+        } catch (IOException e) {
+            // ignore
+        }
+        
+        InputStream errStream = ((HttpURLConnection) conn).getErrorStream();
+        if (errStream != null) {
+            try {
+                while (errStream.read(buffer) > 0) {
+                }
+                errStream.close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
+    }
+
+
 }



Mime
View raw message