Commit 62ad7de1 by Bryce McKinlay Committed by Bryce McKinlay

re PR libgcj/10868 (java.net.ServerSocket's constructors create and leak extra sockets)

	* java/net/Socket.java (Socket (SocketImpl)): Don't allow null
	SocketImpl. Update Javadoc.
	(bind): Call close() not impl.close() in event of exception.
	(connect): Likewise.
	Remove superfluous null checks throughout.
	* java/net/ServerSocket.java (ServerSocket (int, int, InetAddress)):
	Don't create an extra socket. Fix for PR libgcj/10868.
	(bind): Clean up exception handling.
	Remove superfluous null checks throughout.

From-SVN: r70219
parent fdc49e10
2003-08-07 Bryce McKinlay <bryce@mckinlay.net.nz>
* java/net/Socket.java (Socket (SocketImpl)): Don't allow null
SocketImpl. Update Javadoc.
(bind): Call close() not impl.close() in event of exception.
(connect): Likewise.
Remove superfluous null checks throughout.
* java/net/ServerSocket.java (ServerSocket (int, int, InetAddress)):
Don't create an extra socket. Fix for PR libgcj/10868.
(bind): Clean up exception handling.
Remove superfluous null checks throughout.
2003-08-07 Jacob Gladish <gladish@spinnakernet.com> 2003-08-07 Jacob Gladish <gladish@spinnakernet.com>
Bryce McKinlay <bryce@mckinlay.net.nz> Bryce McKinlay <bryce@mckinlay.net.nz>
......
...@@ -154,12 +154,6 @@ public class ServerSocket ...@@ -154,12 +154,6 @@ public class ServerSocket
{ {
this(); this();
if (impl == null)
throw new IOException("Cannot initialize Socket implementation");
// create socket
impl.create(true);
// bind/listen socket // bind/listen socket
bind (new InetSocketAddress (bindAddr, port), backlog); bind (new InetSocketAddress (bindAddr, port), backlog);
} }
...@@ -208,9 +202,6 @@ public class ServerSocket ...@@ -208,9 +202,6 @@ public class ServerSocket
if (closed) if (closed)
throw new SocketException ("ServerSocket is closed"); throw new SocketException ("ServerSocket is closed");
if (impl == null)
throw new IOException ("Cannot initialize Socket implementation");
if (! (endpoint instanceof InetSocketAddress)) if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported"); throw new IllegalArgumentException ("Address type not supported");
...@@ -220,45 +211,24 @@ public class ServerSocket ...@@ -220,45 +211,24 @@ public class ServerSocket
if (s != null) if (s != null)
s.checkListen (tmp.getPort ()); s.checkListen (tmp.getPort ());
// bind to address/port
try try
{ {
impl.bind (tmp.getAddress (), tmp.getPort ()); impl.bind (tmp.getAddress (), tmp.getPort ());
impl.listen(backlog);
} }
catch (IOException exception) catch (IOException exception)
{ {
impl.close(); close();
throw exception; throw exception;
} }
catch (RuntimeException exception) catch (RuntimeException exception)
{ {
impl.close(); close();
throw exception; throw exception;
} }
catch (Error error) catch (Error error)
{ {
impl.close(); close();
throw error;
}
// listen on socket
try
{
impl.listen(backlog);
}
catch (IOException exception)
{
impl.close();
throw exception;
}
catch (RuntimeException exception)
{
impl.close();
throw exception;
}
catch (Error error)
{
impl.close();
throw error; throw error;
} }
} }
...@@ -320,9 +290,6 @@ public class ServerSocket ...@@ -320,9 +290,6 @@ public class ServerSocket
*/ */
public Socket accept () throws IOException public Socket accept () throws IOException
{ {
if (impl == null)
throw new IOException ("Cannot initialize Socket implementation");
SecurityManager sm = System.getSecurityManager (); SecurityManager sm = System.getSecurityManager ();
if (sm != null) if (sm != null)
sm.checkListen (impl.getLocalPort ()); sm.checkListen (impl.getLocalPort ());
...@@ -466,9 +433,6 @@ public class ServerSocket ...@@ -466,9 +433,6 @@ public class ServerSocket
public void setReuseAddress (boolean on) public void setReuseAddress (boolean on)
throws SocketException throws SocketException
{ {
if (impl == null)
throw new SocketException ("Cannot initialize Socket implementation");
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
} }
...@@ -482,9 +446,6 @@ public class ServerSocket ...@@ -482,9 +446,6 @@ public class ServerSocket
public boolean getReuseAddress() public boolean getReuseAddress()
throws SocketException throws SocketException
{ {
if (impl == null)
throw new SocketException ("Cannot initialize Socket implementation");
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean)) if (!(reuseaddr instanceof Boolean))
...@@ -508,9 +469,6 @@ public class ServerSocket ...@@ -508,9 +469,6 @@ public class ServerSocket
public void setReceiveBufferSize (int size) public void setReceiveBufferSize (int size)
throws SocketException throws SocketException
{ {
if (impl == null)
throw new SocketException ("Not connected");
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0"); throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");
...@@ -531,9 +489,6 @@ public class ServerSocket ...@@ -531,9 +489,6 @@ public class ServerSocket
public int getReceiveBufferSize () public int getReceiveBufferSize ()
throws SocketException throws SocketException
{ {
if (impl == null)
throw new SocketException ("Not connected");
Object buf = impl.getOption (SocketOptions.SO_RCVBUF); Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
if (!(buf instanceof Integer)) if (!(buf instanceof Integer))
......
...@@ -82,8 +82,8 @@ public class Socket ...@@ -82,8 +82,8 @@ public class Socket
*/ */
SocketImpl impl; SocketImpl impl;
private boolean inputShutdown; private boolean inputShutdown = false;
private boolean outputShutdown; private boolean outputShutdown = false;
SocketChannel ch; // this field must have been set if created by SocketChannel SocketChannel ch; // this field must have been set if created by SocketChannel
...@@ -103,9 +103,6 @@ public class Socket ...@@ -103,9 +103,6 @@ public class Socket
impl = factory.createSocketImpl(); impl = factory.createSocketImpl();
else else
impl = new PlainSocketImpl(); impl = new PlainSocketImpl();
inputShutdown = false;
outputShutdown = false;
} }
/** /**
...@@ -115,9 +112,8 @@ public class Socket ...@@ -115,9 +112,8 @@ public class Socket
* <p> * <p>
* Additionally, this socket will be created using the supplied * Additionally, this socket will be created using the supplied
* implementation class instead the default class or one returned by a * implementation class instead the default class or one returned by a
* factory. This value can be <code>null</code>, but if it is, all instance * factory. If this value is <code>null</code>, the default Socket
* methods in <code>Socket</code> should be overridden because most of them * implementation is used.
* rely on this value being populated.
* *
* @param impl The <code>SocketImpl</code> to use for this * @param impl The <code>SocketImpl</code> to use for this
* <code>Socket</code> * <code>Socket</code>
...@@ -128,9 +124,10 @@ public class Socket ...@@ -128,9 +124,10 @@ public class Socket
*/ */
protected Socket (SocketImpl impl) throws SocketException protected Socket (SocketImpl impl) throws SocketException
{ {
this.impl = impl; if (impl == null)
this.inputShutdown = false; this.impl = new PlainSocketImpl();
this.outputShutdown = false; else
this.impl = impl;
} }
/** /**
...@@ -282,12 +279,6 @@ public class Socket ...@@ -282,12 +279,6 @@ public class Socket
{ {
this(); this();
if (raddr == null)
throw new NullPointerException ();
if (impl == null)
throw new IOException("Cannot initialize Socket implementation");
SecurityManager sm = System.getSecurityManager(); SecurityManager sm = System.getSecurityManager();
if (sm != null) if (sm != null)
sm.checkConnect(raddr.getHostName(), rport); sm.checkConnect(raddr.getHostName(), rport);
...@@ -351,17 +342,17 @@ public class Socket ...@@ -351,17 +342,17 @@ public class Socket
} }
catch (IOException exception) catch (IOException exception)
{ {
impl.close (); close ();
throw exception; throw exception;
} }
catch (RuntimeException exception) catch (RuntimeException exception)
{ {
impl.close (); close ();
throw exception; throw exception;
} }
catch (Error error) catch (Error error)
{ {
impl.close (); close ();
throw error; throw error;
} }
} }
...@@ -420,17 +411,17 @@ public class Socket ...@@ -420,17 +411,17 @@ public class Socket
} }
catch (IOException exception) catch (IOException exception)
{ {
impl.close (); close ();
throw exception; throw exception;
} }
catch (RuntimeException exception) catch (RuntimeException exception)
{ {
impl.close (); close ();
throw exception; throw exception;
} }
catch (Error error) catch (Error error)
{ {
impl.close (); close ();
throw error; throw error;
} }
} }
...@@ -443,10 +434,7 @@ public class Socket ...@@ -443,10 +434,7 @@ public class Socket
*/ */
public InetAddress getInetAddress () public InetAddress getInetAddress ()
{ {
if (impl != null) return impl.getInetAddress();
return impl.getInetAddress();
return null;
} }
/** /**
...@@ -459,9 +447,6 @@ public class Socket ...@@ -459,9 +447,6 @@ public class Socket
*/ */
public InetAddress getLocalAddress () public InetAddress getLocalAddress ()
{ {
if (impl == null)
return null;
InetAddress addr = null; InetAddress addr = null;
try try
{ {
...@@ -586,9 +571,6 @@ public class Socket ...@@ -586,9 +571,6 @@ public class Socket
*/ */
public void setTcpNoDelay (boolean on) throws SocketException public void setTcpNoDelay (boolean on) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on)); impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
} }
...@@ -606,9 +588,6 @@ public class Socket ...@@ -606,9 +588,6 @@ public class Socket
*/ */
public boolean getTcpNoDelay() throws SocketException public boolean getTcpNoDelay() throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object on = impl.getOption(SocketOptions.TCP_NODELAY); Object on = impl.getOption(SocketOptions.TCP_NODELAY);
if (on instanceof Boolean) if (on instanceof Boolean)
...@@ -636,9 +615,6 @@ public class Socket ...@@ -636,9 +615,6 @@ public class Socket
*/ */
public void setSoLinger(boolean on, int linger) throws SocketException public void setSoLinger(boolean on, int linger) throws SocketException
{ {
if (impl == null)
throw new SocketException("No socket created");
if (on == true) if (on == true)
{ {
if (linger < 0) if (linger < 0)
...@@ -673,9 +649,6 @@ public class Socket ...@@ -673,9 +649,6 @@ public class Socket
*/ */
public int getSoLinger() throws SocketException public int getSoLinger() throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object linger = impl.getOption(SocketOptions.SO_LINGER); Object linger = impl.getOption(SocketOptions.SO_LINGER);
if (linger instanceof Integer) if (linger instanceof Integer)
return(((Integer)linger).intValue()); return(((Integer)linger).intValue());
...@@ -709,9 +682,6 @@ public class Socket ...@@ -709,9 +682,6 @@ public class Socket
*/ */
public void setOOBInline (boolean on) throws SocketException public void setOOBInline (boolean on) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on)); impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
} }
...@@ -724,9 +694,6 @@ public class Socket ...@@ -724,9 +694,6 @@ public class Socket
*/ */
public boolean getOOBInline () throws SocketException public boolean getOOBInline () throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE); Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
if (buf instanceof Boolean) if (buf instanceof Boolean)
...@@ -754,9 +721,6 @@ public class Socket ...@@ -754,9 +721,6 @@ public class Socket
*/ */
public synchronized void setSoTimeout (int timeout) throws SocketException public synchronized void setSoTimeout (int timeout) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
if (timeout < 0) if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
...@@ -782,9 +746,6 @@ public class Socket ...@@ -782,9 +746,6 @@ public class Socket
*/ */
public synchronized int getSoTimeout () throws SocketException public synchronized int getSoTimeout () throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer) if (timeout instanceof Integer)
return(((Integer)timeout).intValue()); return(((Integer)timeout).intValue());
...@@ -806,9 +767,6 @@ public class Socket ...@@ -806,9 +767,6 @@ public class Socket
*/ */
public void setSendBufferSize (int size) throws SocketException public void setSendBufferSize (int size) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException("SO_SNDBUF value must be > 0"); throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
...@@ -828,9 +786,6 @@ public class Socket ...@@ -828,9 +786,6 @@ public class Socket
*/ */
public int getSendBufferSize () throws SocketException public int getSendBufferSize () throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object buf = impl.getOption(SocketOptions.SO_SNDBUF); Object buf = impl.getOption(SocketOptions.SO_SNDBUF);
if (buf instanceof Integer) if (buf instanceof Integer)
...@@ -853,9 +808,6 @@ public class Socket ...@@ -853,9 +808,6 @@ public class Socket
*/ */
public void setReceiveBufferSize (int size) throws SocketException public void setReceiveBufferSize (int size) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException("SO_RCVBUF value must be > 0"); throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
...@@ -875,9 +827,6 @@ public class Socket ...@@ -875,9 +827,6 @@ public class Socket
*/ */
public int getReceiveBufferSize () throws SocketException public int getReceiveBufferSize () throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object buf = impl.getOption(SocketOptions.SO_RCVBUF); Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
if (buf instanceof Integer) if (buf instanceof Integer)
...@@ -898,9 +847,6 @@ public class Socket ...@@ -898,9 +847,6 @@ public class Socket
*/ */
public void setKeepAlive (boolean on) throws SocketException public void setKeepAlive (boolean on) throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on)); impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
} }
...@@ -916,9 +862,6 @@ public class Socket ...@@ -916,9 +862,6 @@ public class Socket
*/ */
public boolean getKeepAlive () throws SocketException public boolean getKeepAlive () throws SocketException
{ {
if (impl == null)
throw new SocketException("Not connected");
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE); Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
if (buf instanceof Boolean) if (buf instanceof Boolean)
...@@ -1037,9 +980,6 @@ public class Socket ...@@ -1037,9 +980,6 @@ public class Socket
*/ */
public boolean getReuseAddress () throws SocketException public boolean getReuseAddress () throws SocketException
{ {
if (impl == null)
throw new SocketException ("Cannot initialize Socket implementation");
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean)) if (!(reuseaddr instanceof Boolean))
...@@ -1057,9 +997,6 @@ public class Socket ...@@ -1057,9 +997,6 @@ public class Socket
*/ */
public void setReuseAddress (boolean on) throws SocketException 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)); impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
} }
...@@ -1074,9 +1011,6 @@ public class Socket ...@@ -1074,9 +1011,6 @@ public class Socket
*/ */
public int getTrafficClass () throws SocketException public int getTrafficClass () throws SocketException
{ {
if (impl == null)
throw new SocketException ("Cannot initialize Socket implementation");
Object obj = impl.getOption(SocketOptions.IP_TOS); Object obj = impl.getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer) if (obj instanceof Integer)
...@@ -1099,9 +1033,6 @@ public class Socket ...@@ -1099,9 +1033,6 @@ public class Socket
*/ */
public void setTrafficClass (int tc) throws SocketException public void setTrafficClass (int tc) throws SocketException
{ {
if (impl == null)
throw new SocketException ("Cannot initialize Socket implementation");
if (tc < 0 || tc > 255) if (tc < 0 || tc > 255)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment