aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/ServerSocket.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/net/ServerSocket.java')
-rw-r--r--libjava/java/net/ServerSocket.java81
1 files changed, 57 insertions, 24 deletions
diff --git a/libjava/java/net/ServerSocket.java b/libjava/java/net/ServerSocket.java
index f4d1ecc..d1ea4d6 100644
--- a/libjava/java/net/ServerSocket.java
+++ b/libjava/java/net/ServerSocket.java
@@ -73,8 +73,6 @@ public class ServerSocket
*/
private SocketImpl impl;
- private boolean closed = false;
-
/*
* This constructor is only used by java.nio.
*/
@@ -82,6 +80,9 @@ public class ServerSocket
//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);
}
@@ -208,8 +209,8 @@ public class ServerSocket
*/
public void bind (SocketAddress endpoint, int backlog) throws IOException
{
- if (closed)
- throw new SocketException ("ServerSocket is closed");
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported");
@@ -249,12 +250,16 @@ public class ServerSocket
*/
public InetAddress getInetAddress()
{
+ if (!isBound())
+ return null;
+
try
{
return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
}
catch (SocketException e)
{
+ // This never happens as we are bound.
return null;
}
}
@@ -266,6 +271,9 @@ public class ServerSocket
*/
public int getLocalPort()
{
+ if (!isBound())
+ return -1;
+
return impl.getLocalPort();
}
@@ -276,12 +284,10 @@ public class ServerSocket
*/
public SocketAddress getLocalSocketAddress()
{
- InetAddress addr = getInetAddress();
-
- if (addr != null)
- return new InetSocketAddress (getInetAddress(), getLocalPort());
-
- return null;
+ if (!isBound())
+ return null;
+
+ return new InetSocketAddress(getInetAddress(), getLocalPort());
}
/**
@@ -303,10 +309,9 @@ public class ServerSocket
if (sm != null)
sm.checkListen (impl.getLocalPort ());
- Socket s = new Socket();
- implAccept (s);
-
- return s;
+ Socket socket = new Socket();
+ implAccept (socket);
+ return socket;
}
/**
@@ -322,14 +327,17 @@ public class ServerSocket
*
* @since 1.1
*/
- protected final void implAccept (Socket s)
+ protected final void implAccept (Socket socket)
throws IOException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
if (getChannel() != null
&& !getChannel().isBlocking())
throw new IllegalBlockingModeException();
- impl.accept(s.impl);
+ impl.accept(socket.getImpl());
}
/**
@@ -339,12 +347,15 @@ public class ServerSocket
*/
public void close () throws IOException
{
- impl.close ();
+ if (!isClosed())
+ {
+ impl.close();
- if (getChannel() != null)
- getChannel().close ();
+ if (getChannel() != null)
+ getChannel().close();
- closed = true;
+ impl = null;
+ }
}
/**
@@ -387,7 +398,7 @@ public class ServerSocket
*/
public boolean isClosed()
{
- return closed;
+ return impl == null;
}
/**
@@ -404,6 +415,9 @@ public class ServerSocket
*/
public void setSoTimeout (int timeout) throws SocketException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
@@ -424,6 +438,9 @@ public class ServerSocket
*/
public int getSoTimeout () throws IOException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (!(timeout instanceof Integer))
@@ -442,6 +459,9 @@ public class ServerSocket
public void setReuseAddress (boolean on)
throws SocketException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
@@ -455,6 +475,9 @@ public class ServerSocket
public boolean getReuseAddress()
throws SocketException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean))
@@ -478,6 +501,9 @@ public class ServerSocket
public void setReceiveBufferSize (int size)
throws SocketException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
if (size <= 0)
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");
@@ -498,6 +524,9 @@ public class ServerSocket
public int getReceiveBufferSize ()
throws SocketException
{
+ if (isClosed())
+ throw new SocketException("ServerSocket is closed");
+
Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
if (!(buf instanceof Integer))
@@ -513,11 +542,15 @@ public class ServerSocket
*/
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
-
/**
* Sets the <code>SocketImplFactory</code> for all
* <code>ServerSocket</code>'s. This may only be done