This patch adds the missing [get|set]ReadTimeout methods to
java.net.URLConnection. It is lightly tested, but I will create some
mauve tests for it.
If there are no objections, I will commit it after a couple more of days
of testing.
2006-12-02 David Daney <[EMAIL PROTECTED]>
* gnu/java/net/protocol/http/HTTPConnection.java (imports): Add
SocketException.
(HTTPConnection): Handle NumberFormatException in properties
parsing.
(Pool.get): Set timeout on reused sockets.
* gnu/java/net/protocol/http/HTTPURLConnection.java (proxyPort):
Initialize.
(HTTPURLConnection): Cleanup properties handling.
(getConnection): Use both connection and read timeouts.
(setConnectTimeout): Removed.
(setReadTimeout): New method.
* java/net/URLConnection.java (timeout): Renamed to...
(connectTimeout): ... connectTimeout throughout.
(readTimeout): New field.
(getReadTimeout): New method.
(setReadTimeout): New method.
Index: gnu/java/net/protocol/http/HTTPConnection.java
===================================================================
RCS file:
/sources/classpath/classpath/gnu/java/net/protocol/http/HTTPConnection.java,v
retrieving revision 1.14
diff -u -p -r1.14 HTTPConnection.java
--- gnu/java/net/protocol/http/HTTPConnection.java 12 May 2006 20:59:30
-0000 1.14
+++ gnu/java/net/protocol/http/HTTPConnection.java 3 Dec 2006 07:27:58
-0000
@@ -48,6 +48,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
+import java.net.SocketException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -227,10 +228,16 @@ public class HTTPConnection
* @param secure whether to use a secure connection
* @param connectionTimeout the connection timeout
* @param timeout the socket read timeout
+ *
+ * @throws IllegalArgumentException if either connectionTimeout or
+ * timeout less than zero.
*/
public HTTPConnection(String hostname, int port, boolean secure,
int connectionTimeout, int timeout)
{
+ if (connectionTimeout < 0 || timeout < 0)
+ throw new IllegalArgumentException();
+
this.hostname = hostname;
this.port = port;
this.secure = secure;
@@ -471,14 +478,32 @@ public class HTTPConnection
{
String ttl =
SystemProperties.getProperty("classpath.net.http.keepAliveTTL");
- connectionTTL = (ttl != null && ttl.length() > 0) ?
- 1000 * Math.max(1, Integer.parseInt(ttl)) : 10000;
+ connectionTTL = 10000;
+ if (ttl != null && ttl.length() > 0)
+ try
+ {
+ int v = 1000 * Integer.parseInt(ttl);
+ if (v >= 0)
+ connectionTTL = v;
+ }
+ catch (NumberFormatException _)
+ {
+ // Ignore.
+ }
String mc = SystemProperties.getProperty("http.maxConnections");
- maxConnections = (mc != null && mc.length() > 0) ?
- Math.max(Integer.parseInt(mc), 1) : 5;
- if (maxConnections < 1)
- maxConnections = 1;
+ maxConnections = 5;
+ if (mc != null && mc.length() > 0)
+ try
+ {
+ int v = Integer.parseInt(mc);
+ if (v > 0)
+ maxConnections = v;
+ }
+ catch (NumberFormatException _)
+ {
+ // Ignore.
+ }
HTTPConnection c = null;
@@ -490,12 +515,23 @@ public class HTTPConnection
{
c = cc;
it.remove();
+ // Update the timeout.
+ if (c.socket != null)
+ try
+ {
+ c.socket.setSoTimeout(timeout);
+ }
+ catch (SocketException _)
+ {
+ // Ignore.
+ }
break;
}
}
if (c == null)
{
- c = new HTTPConnection(host, port, secure, connectionTimeout,
timeout);
+ c = new HTTPConnection(host, port, secure,
+ connectionTimeout, timeout);
c.setPool(this);
}
return c;
Index: gnu/java/net/protocol/http/HTTPURLConnection.java
===================================================================
RCS file:
/sources/classpath/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java,v
retrieving revision 1.27
diff -u -p -r1.27 HTTPURLConnection.java
--- gnu/java/net/protocol/http/HTTPURLConnection.java 20 Sep 2006 18:18:34
-0000 1.27
+++ gnu/java/net/protocol/http/HTTPURLConnection.java 3 Dec 2006 07:27:58
-0000
@@ -75,7 +75,7 @@ public class HTTPURLConnection
// These are package private for use in anonymous inner classes.
String proxyHostname;
- int proxyPort;
+ int proxyPort = -1;
String agent;
boolean keepAlive;
@@ -99,18 +99,21 @@ public class HTTPURLConnection
{
super(url);
requestHeaders = new Headers();
- proxyHostname = SystemProperties.getProperty("http.proxyHost");
- if (proxyHostname != null && proxyHostname.length() > 0)
+ String proxy = SystemProperties.getProperty("http.proxyHost");
+ if (proxy != null && proxy.length() > 0)
{
String port = SystemProperties.getProperty("http.proxyPort");
if (port != null && port.length() > 0)
{
- proxyPort = Integer.parseInt(port);
- }
- else
- {
- proxyHostname = null;
- proxyPort = -1;
+ try
+ {
+ proxyPort = Integer.parseInt(port);
+ proxyHostname = proxy;
+ }
+ catch (NumberFormatException _)
+ {
+ // Ignore.
+ }
}
}
agent = SystemProperties.getProperty("http.agent");
@@ -354,11 +357,14 @@ public class HTTPURLConnection
HTTPConnection connection;
if (keepAlive)
{
- connection = HTTPConnection.Pool.instance.get(host, port, secure,
getConnectTimeout(), 0);
+ connection = HTTPConnection.Pool.instance.get(host, port, secure,
+ getConnectTimeout(),
+ getReadTimeout());
}
else
{
- connection = new HTTPConnection(host, port, secure, 0,
getConnectTimeout());
+ connection = new HTTPConnection(host, port, secure,
+ getConnectTimeout(), getReadTimeout());
}
return connection;
}
@@ -662,23 +668,23 @@ public class HTTPURLConnection
}
/**
- * Set the connection timeout speed, in milliseconds, or zero if the timeout
+ * Set the read timeout, in milliseconds, or zero if the timeout
* is to be considered infinite.
*
* Overloaded.
*
*/
- public void setConnectTimeout(int timeout)
+ public void setReadTimeout(int timeout)
throws IllegalArgumentException
{
- super.setConnectTimeout( timeout );
- if( connection == null )
+ super.setReadTimeout(timeout);
+ if (connection == null)
return;
try
{
- connection.getSocket().setSoTimeout( timeout );
+ connection.getSocket().setSoTimeout(timeout);
}
- catch(IOException se)
+ catch (IOException se)
{
// Ignore socket exceptions.
}
Index: java/net/URLConnection.java
===================================================================
RCS file: /sources/classpath/classpath/java/net/URLConnection.java,v
retrieving revision 1.43
diff -u -p -r1.43 URLConnection.java
--- java/net/URLConnection.java 27 May 2006 02:05:44 -0000 1.43
+++ java/net/URLConnection.java 3 Dec 2006 07:28:02 -0000
@@ -174,9 +174,14 @@ public abstract class URLConnection
private static boolean dateformats_initialized;
/**
- * The timeout period.
+ * The connection timeout period.
*/
- private int timeout;
+ private int connectTimeout;
+
+ /**
+ * The read timeout period.
+ */
+ private int readTimeout;
/* Cached ParsePosition, used when parsing dates. */
private ParsePosition position;
@@ -216,8 +221,8 @@ public abstract class URLConnection
}
/**
- * Returns the connection timeout speed, in milliseconds, or zero if the
timeout
- * is infinite or not set.
+ * Returns the connection timeout speed, in milliseconds, or zero if
+ * the timeout is infinite or not set.
*
* @return The timeout.
*
@@ -225,7 +230,7 @@ public abstract class URLConnection
*/
public int getConnectTimeout()
{
- return timeout;
+ return connectTimeout;
}
/**
@@ -235,7 +240,7 @@ public abstract class URLConnection
*
* Throws an <code>IllegalArgumentException</code> if timeout < 0.
*
- * @param timeout - The timeout, in milliseconds.
+ * @param timeout the timeout, in milliseconds.
*
* @since 1.5
*/
@@ -244,7 +249,45 @@ public abstract class URLConnection
{
if( timeout < 0 )
throw new IllegalArgumentException("Timeout must be 0 or positive.");
- this.timeout = timeout;
+ connectTimeout = timeout;
+ }
+
+ /**
+ * Returns the read timeout, in milliseconds, or zero if the timeout
+ * is infinite or not set.
+ *
+ * @return The timeout.
+ *
+ * @see #setReadTimeout
+ *
+ * @since 1.5
+ */
+ public int getReadTimeout()
+ {
+ return readTimeout;
+ }
+
+ /**
+ * Set the read timeout, in milliseconds, or zero if the timeout
+ * is to be considered infinite. Note that in certain socket
+ * implementations/platforms this method may not have any effect.
+ *
+ * Throws an <code>IllegalArgumentException</code> if timeout < 0.
+ *
+ * @param timeout - The timeout, in milliseconds.
+ *
+ * @throws IllegalArgumentException if timeout is negative.
+ *
+ * @see #getReadTimeout
+ *
+ * @since 1.5
+ */
+ public void setReadTimeout(int timeout)
+ throws IllegalArgumentException
+ {
+ if( timeout < 0 )
+ throw new IllegalArgumentException("Timeout must be 0 or positive.");
+ readTimeout = timeout;
}
/**