You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/05/15 17:41:27 UTC

svn commit: r406659 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/net/ main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/ main/java/org/apache/harmony/luni/internal/net/www/protocol/http/ test/java/tests...

Author: gharley
Date: Mon May 15 08:41:26 2006
New Revision: 406659

URL: http://svn.apache.org/viewcvs?rev=406659&view=rev
Log:
HARMONY 361 : Java 5 Enhancement - 4 new methods in java.net.URLConnection

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLConnectionTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java?rev=406659&r1=406658&r2=406659&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/URLConnection.java Mon May 15 08:41:26 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -68,7 +68,11 @@
 	protected boolean allowUserInteraction = defaultAllowUserInteraction;
 
 	private static ContentHandlerFactory contentHandlerFactory;
+    
+    private int readTimeout = 0;
 
+    private int connectTimeout = 0;
+    
 	/**
 	 * Cache for storing Content handler
 	 */
@@ -924,6 +928,54 @@
 		}
 		this.useCaches = newValue;
 	}
+    
+    /**
+     * Sets a timeout for connection to perform non-block. Default is zero.
+     * Timeout of zero means infinite.
+     * @param timeout
+     *            timeout for connection in milliseconds.
+     * @throws IllegalArgumentException
+     *             if timeout is less than zero.
+     */
+    public void setConnectTimeout(int timeout){
+        if (0 > timeout){
+            throw new IllegalArgumentException(Msg.getString("K0036"));            
+        }
+        this.connectTimeout = timeout;
+    }
+    
+    /**
+     * Returns a timeout of connection by milliseconds
+     * 
+     * @return timeout of connection by milliseconds
+     */
+    public int getConnectTimeout(){
+        return connectTimeout;
+    }
+    
+    /**
+     * Sets a timeout for reading to perform non-block. Default is zero.
+     * Timeout of zero means infinite.
+     * @param timeout
+     *            timeout for reading in milliseconds.
+     * @throws IllegalArgumentException
+     *             if timeout is less than zero.
+     */
+    public void setReadTimeout(int timeout){
+        if (0 > timeout){
+            throw new IllegalArgumentException(Msg.getString("K0036"));            
+        }
+        this.readTimeout = timeout;
+    }
+    
+    /**
+     * Returns a timeout of reading by milliseconds
+     * 
+     * @return timeout of reading by milliseconds
+     */
+    public int getReadTimeout() {
+        return readTimeout;
+    }
 
 	/**
 	 * Answers the name of the class of the <code>URLConnection </code>

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java?rev=406659&r1=406658&r2=406659&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/ftp/FtpURLConnection.java Mon May 15 08:41:26 2006
@@ -194,16 +194,16 @@
 	}
 	private void connectInternal() throws IOException {
 		int port = url.getPort();
+        int connectTimeout = getConnectTimeout();
 		if (port <= 0)
 			port = FTP_PORT;
-		if(null == currentProxy || Proxy.Type.HTTP == currentProxy.type()){
-			controlSocket = new Socket(hostName,port);
-		}else{
-			controlSocket = new Socket(currentProxy);
-			InetSocketAddress addr = new InetSocketAddress(hostName,port);
-			controlSocket.connect(addr);
-		}
-		
+		if (null == currentProxy || Proxy.Type.HTTP == currentProxy.type()) {
+            controlSocket = new Socket();
+        } else {
+            controlSocket = new Socket(currentProxy);
+        }		
+        InetSocketAddress addr = new InetSocketAddress(hostName, port);
+        controlSocket.connect(addr, connectTimeout);
 		connected = true;
 		ctrlOutput = controlSocket.getOutputStream();
 		ctrlInput = controlSocket.getInputStream();
@@ -217,12 +217,17 @@
 			dataPort = acceptSocket.getLocalPort();
 			/* Cannot set REUSEADDR so we need to send a PORT comannd */
 			port();
-			acceptSocket.setSoTimeout(3000);
+            if (connectTimeout == 0) {
+                // set timeout rather than zero as before
+                connectTimeout = 3000;
+            }
+            acceptSocket.setSoTimeout(getConnectTimeout());
 			if (getDoInput())
 				getFile();
 			else
 				sendFile();
 			dataSocket = acceptSocket.accept();
+            dataSocket.setSoTimeout(getReadTimeout());
 			acceptSocket.close();
 		} catch (InterruptedIOException e) {
 			throw new IOException(Msg.getString("K0095"));

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java?rev=406659&r1=406658&r2=406659&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/internal/net/www/protocol/http/HttpURLConnection.java Mon May 15 08:41:26 2006
@@ -81,8 +81,6 @@
 
     private int hostPort = -1;
 
-    private int readTimeout = -1;
-
     // proxy which is used to make the connection.
     private Proxy proxy = null;
 
@@ -424,37 +422,20 @@
         if (connected)
             return;
         Socket socket;
-
+        int connectTimeout = getConnectTimeout();
         InetAddress host = getHostAddress();
         int port = getHostPort();
+        SocketAddress sa = new InetSocketAddress(host, port);
         if (null == currentProxy || Proxy.Type.HTTP == currentProxy.type()) {
-            socket = new Socket(host, port);
+            socket = new Socket();
         } else {
             socket = new Socket(currentProxy);
-            SocketAddress sa = new InetSocketAddress(host, port);
-            socket.connect(sa);
         }
-
-        if (readTimeout >= 0)
-            socket.setSoTimeout(readTimeout);
+        socket.connect(sa, connectTimeout);
+        socket.setSoTimeout(getReadTimeout());
         connected = true;
         socketOut = socket.getOutputStream();
         is = new BufferedInputStream(socket.getInputStream());
-    }
-
-    /**
-     * Sets the read timeout for the http connection.
-     * 
-     * 
-     * @param value
-     *            the read timeout value. Must be >= 0
-     * 
-     * @see java.net.Socket#setSoTimeout
-     */
-    public void setReadTimeout(int value) {
-        if (value < 0)
-            throw new IllegalArgumentException(Msg.getString("K0036"));
-        readTimeout = value;
     }
 
     /**

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLConnectionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLConnectionTest.java?rev=406659&r1=406658&r2=406659&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLConnectionTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/URLConnectionTest.java Mon May 15 08:41:26 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -958,6 +958,62 @@
 	public void test_setUseCachesZ() {
 		assertTrue("Used to test", true);
 	}
+  
+    /**
+     * @tests java.net.URLConnection#setConnectTimeout(int)
+     */
+    public void test_setConnectTimeoutI() throws Exception {
+        URLConnection uc = new URL("http://localhost").openConnection();
+        assertEquals(0, uc.getConnectTimeout());
+        uc.setConnectTimeout(0);
+        assertEquals(uc.getConnectTimeout(),0);
+        try{
+            uc.setConnectTimeout(-100);
+            fail("should throw IllegalArgumentException");
+        }
+        catch(IllegalArgumentException e){
+            // correct
+        }
+        assertEquals(0, uc.getConnectTimeout());
+        uc.setConnectTimeout(100);
+        assertEquals(100, uc.getConnectTimeout());
+        try{
+            uc.setConnectTimeout(-1);
+            fail("should throw IllegalArgumentException");
+        }
+        catch(IllegalArgumentException e){
+            // correct
+        }
+        assertEquals(100, uc.getConnectTimeout());
+    }
+    
+    /**
+     * @tests java.net.URLConnection#setReadTimeout(int)
+     */
+    public void test_setReadTimeoutI() throws Exception {
+        URLConnection uc = new URL("http://localhost").openConnection();
+        assertEquals(0, uc.getReadTimeout());
+        uc.setReadTimeout(0);
+        assertEquals(0, uc.getReadTimeout());
+        try{
+            uc.setReadTimeout(-100);
+            fail("should throw IllegalArgumentException");
+        }
+        catch(IllegalArgumentException e){
+            // correct
+        }
+        assertEquals(0, uc.getReadTimeout());
+        uc.setReadTimeout(100);
+        assertEquals(100, uc.getReadTimeout());
+        try{
+            uc.setReadTimeout(-1);
+            fail("should throw IllegalArgumentException");
+        }
+        catch(IllegalArgumentException e){
+            // correct
+        }
+        assertEquals(100,uc.getReadTimeout());
+    }
 
 	/**
 	 * @tests java.net.URLConnection#toString()