This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
Patch: java.net
- From: Michael Koch <konqueror at gmx dot de>
- To: java-patches at gcc dot gnu dot org
- Date: Wed, 19 Nov 2003 12:32:28 +0100
- Subject: Patch: java.net
Hi list,
in memoriam of PR 13102 I wrote the attached little patch to fix similar bugs
in java.net.ServerSocket and java.net.DatagramSocket.
Please review and comment.
Michael
Index: ChangeLog
===================================================================
RCS file: /cvs/gcc/gcc/libjava/ChangeLog,v
retrieving revision 1.2344
diff -u -b -B -r1.2344 ChangeLog
--- ChangeLog 19 Nov 2003 08:34:19 -0000 1.2344
+++ ChangeLog 19 Nov 2003 11:26:44 -0000
@@ -1,5 +1,20 @@
2003-11-19 Michael Koch <konqueror@gmx.de>
+ * java/net/DatagramSocket.java
+ (implCreated): New variable.
+ (DatagramSocket): Make sure impl is not null.
+ (getImpl): New method.
+ (various): Use getImpl() instead of impl. Removed check for
+ impl == null. impl may never be null.
+ * java/net/ServerSocket.java
+ (ServerSocket): Ensure impl is not null. impl may never be null.
+ (close): Check if socket is not already closed.
+ (toString): Made it more compliant to SUNs JDK.
+ * java/net/Socket.java
+ (toString): Added missing "]".
+
+2003-11-19 Michael Koch <konqueror@gmx.de>
+
* java/net/Socket.java
(implCreated): New variable that indicates created impl.
(getImpl): New method.
Index: java/net/DatagramSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/DatagramSocket.java,v
retrieving revision 1.29
diff -u -b -B -r1.29 DatagramSocket.java
--- java/net/DatagramSocket.java 19 Sep 2003 07:24:59 -0000 1.29
+++ java/net/DatagramSocket.java 19 Nov 2003 11:26:44 -0000
@@ -74,6 +74,8 @@
*/
DatagramSocketImpl impl;
+ private boolean implCreated = false;
+
/**
* This is the address we are "connected" to
*/
@@ -100,6 +102,9 @@
*/
protected DatagramSocket (DatagramSocketImpl impl)
{
+ if (impl == null)
+ throw new NullPointerException("impl may not be null");
+
this.impl = impl;
this.remoteAddress = null;
this.remotePort = -1;
@@ -168,28 +173,27 @@
propVal + "DatagramSocketImpl");
impl = new PlainDatagramSocketImpl();
}
- impl.create();
if (laddr == null)
laddr = InetAddress.ANY_IF;
try
{
- impl.bind (port, laddr);
+ getImpl().bind (port, laddr);
}
catch (SocketException exception)
{
- impl.close ();
+ getImpl().close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ getImpl().close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ getImpl().close ();
throw error;
}
}
@@ -213,19 +217,48 @@
((InetSocketAddress) address).getAddress ());
}
+ private DatagramSocketImpl getImpl()
+ throws SocketException
+ {
+ try
+ {
+ if (!implCreated)
+ {
+ impl.create();
+ implCreated = true;
+ }
+
+ return impl;
+ }
+ catch (IOException e)
+ {
+ throw new SocketException(e.getMessage());
+ }
+ }
+
/**
* Closes this datagram socket.
*/
public void close()
{
- if (!closed)
+ if (!isClosed())
+ {
+ try
+ {
+ getImpl().close();
+ }
+ catch (SocketException e)
+ {
+ // Ignore this case, just close the socket in finally clause.
+ }
+ finally
{
- impl.close();
remoteAddress = null;
remotePort = -1;
closed = true;
}
}
+ }
/**
* This method returns the remote address to which this socket is
@@ -257,20 +290,19 @@
/**
* Returns the local address this datagram socket is bound to.
- *
+
* @since 1.1
*/
public InetAddress getLocalAddress()
{
- if (impl == null
- || closed)
+ if (!isClosed())
return null;
InetAddress localAddr;
try
{
- localAddr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
+ localAddr = (InetAddress) getImpl().getOption (SocketOptions.SO_BINDADDR);
SecurityManager s = System.getSecurityManager();
if (s != null)
@@ -295,7 +327,17 @@
*/
public int getLocalPort()
{
- return impl.getLocalPort();
+ if (isClosed())
+ return -1;
+
+ try
+ {
+ return getImpl().getLocalPort();
+ }
+ catch (SocketException e)
+ {
+ return 0;
+ }
}
/**
@@ -310,10 +352,7 @@
*/
public synchronized int getSoTimeout() throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
+ Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return ((Integer)timeout).intValue();
@@ -337,7 +376,7 @@
if (timeout < 0)
throw new IllegalArgumentException("Invalid timeout: " + timeout);
- impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
+ getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
}
/**
@@ -353,10 +392,7 @@
*/
public int getSendBufferSize() throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- Object obj = impl.getOption(SocketOptions.SO_SNDBUF);
+ Object obj = getImpl().getOption(SocketOptions.SO_SNDBUF);
if (obj instanceof Integer)
return(((Integer)obj).intValue());
@@ -381,7 +417,7 @@
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
- impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size));
+ getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
}
/**
@@ -397,10 +433,7 @@
*/
public int getReceiveBufferSize() throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
+ Object obj = getImpl().getOption(SocketOptions.SO_RCVBUF);
if (obj instanceof Integer)
return(((Integer)obj).intValue());
@@ -422,13 +455,10 @@
*/
public void setReceiveBufferSize(int size) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0");
- impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
+ getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
}
/**
@@ -461,7 +491,7 @@
try
{
- impl.connect (address, port);
+ getImpl().connect (address, port);
remoteAddress = address;
remotePort = port;
}
@@ -480,10 +510,23 @@
*/
public void disconnect()
{
- impl.disconnect();
+ if (!isConnected())
+ return;
+
+ try
+ {
+ getImpl().disconnect();
+ }
+ catch (SocketException e)
+ {
+ // This cannot happen as we are connected.
+ }
+ finally
+ {
remoteAddress = null;
remotePort = -1;
}
+ }
/**
* Reads a datagram packet from the socket. Note that this method
@@ -506,9 +549,6 @@
*/
public synchronized void receive(DatagramPacket p) throws IOException
{
- if (impl == null)
- throw new IOException ("Cannot initialize Socket implementation");
-
if (remoteAddress != null && remoteAddress.isMulticastAddress ())
throw new IOException (
"Socket connected to a multicast address my not receive");
@@ -517,7 +557,7 @@
&& !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
- impl.receive(p);
+ getImpl().receive(p);
SecurityManager s = System.getSecurityManager();
if (s != null && isConnected ())
@@ -567,7 +607,7 @@
&& !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
- impl.send(p);
+ getImpl().send(p);
}
/**
@@ -594,7 +634,7 @@
if (s != null)
s.checkListen(tmp.getPort ());
- impl.bind (tmp.getPort (), tmp.getAddress ());
+ getImpl().bind (tmp.getPort (), tmp.getAddress ());
}
/**
@@ -646,7 +686,7 @@
{
try
{
- Object bindaddr = impl.getOption (SocketOptions.SO_BINDADDR);
+ Object bindaddr = getImpl().getOption (SocketOptions.SO_BINDADDR);
}
catch (SocketException e)
{
@@ -688,18 +728,20 @@
*/
public SocketAddress getLocalSocketAddress()
{
+ int port;
InetAddress addr;
try
{
- addr = (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
+ addr = (InetAddress) getImpl().getOption (SocketOptions.SO_BINDADDR);
+ port = getImpl().localPort;
}
catch (SocketException e)
{
return null;
}
- return new InetSocketAddress (addr, impl.localPort);
+ return new InetSocketAddress (addr, port);
}
/**
@@ -713,10 +755,7 @@
*/
public void setReuseAddress(boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
+ getImpl().setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
/**
@@ -728,10 +767,7 @@
*/
public boolean getReuseAddress() throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);
+ Object obj = getImpl().getOption (SocketOptions.SO_REUSEADDR);
if (obj instanceof Boolean)
return(((Boolean) obj).booleanValue ());
@@ -750,10 +786,7 @@
*/
public void setBroadcast(boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
+ getImpl().setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
}
/**
@@ -765,10 +798,7 @@
*/
public boolean getBroadcast() throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
- Object obj = impl.getOption (SocketOptions.SO_BROADCAST);
+ Object obj = getImpl().getOption (SocketOptions.SO_BROADCAST);
if (obj instanceof Boolean)
return ((Boolean) obj).booleanValue ();
@@ -791,13 +821,10 @@
public void setTrafficClass(int tc)
throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
if (tc < 0 || tc > 255)
throw new IllegalArgumentException();
- impl.setOption (SocketOptions.IP_TOS, new Integer (tc));
+ getImpl().setOption (SocketOptions.IP_TOS, new Integer (tc));
}
/**
@@ -811,10 +838,7 @@
*/
public int getTrafficClass() throws SocketException
{
- if (impl == null)
- throw new SocketException( "Cannot initialize Socket implementation");
-
- Object obj = impl.getOption(SocketOptions.IP_TOS);
+ Object obj = getImpl().getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer)
return ((Integer) obj).intValue ();
Index: java/net/ServerSocket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/ServerSocket.java,v
retrieving revision 1.28
diff -u -b -B -r1.28 ServerSocket.java
--- java/net/ServerSocket.java 11 Oct 2003 18:01:35 -0000 1.28
+++ java/net/ServerSocket.java 19 Nov 2003 11:26:44 -0000
@@ -82,6 +82,9 @@
//ServerSocket (PlainSocketImpl impl) throws IOException
ServerSocket (SocketImpl impl) throws IOException
{
+ if (impl == null)
+ throw new NullPointerException("impl may not be null");
+
this.impl = impl;
this.impl.create (true);
}
@@ -339,13 +342,16 @@
*/
public void close () throws IOException
{
- impl.close ();
+ if (!closed)
+ {
+ impl.close();
if (getChannel() != null)
getChannel().close ();
closed = true;
}
+ }
/**
* Returns the unique ServerSocketChannel object
@@ -513,7 +519,13 @@
*/
public String toString ()
{
- return "ServerSocket" + impl.toString();
+ if (!isBound())
+ return "ServerSocket[unbound]";
+
+ return ("ServerSocket[addr=" + impl.getInetAddress()
+ + ",port=" + impl.getPort()
+ + ",localport=" + impl.getLocalPort()
+ + "]");
}
// Class methods
Index: java/net/Socket.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/Socket.java,v
retrieving revision 1.29
diff -u -b -B -r1.29 Socket.java
--- java/net/Socket.java 19 Nov 2003 08:34:21 -0000 1.29
+++ java/net/Socket.java 19 Nov 2003 11:26:44 -0000
@@ -958,7 +958,8 @@
if (isConnected())
return ("Socket[addr=" + getImpl().getInetAddress()
+ ",port=" + getImpl().getPort()
- + ",localport=" + getImpl().getLocalPort());
+ + ",localport=" + getImpl().getLocalPort()
+ + "]");
}
catch (SocketException e)
{