From 66e5d61fba14cd936e0183ba014703c196269590 Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Tue, 25 Nov 2003 10:09:48 +0000 Subject: 2003-11-25 Michael Koch * java/net/DatagramSocket.java (factory): Made private. (closed): Removed. (DatagramSocket): Check impl argument, use constructor with SocketAddress argument. (close): Set impl to null, use isClosed(). (isClosed): Check for impl == null. (getLocalAddress): Use isClosed(). (getLocalPort): Check if socket is closed. (getSoTimeout): Likewise. (setSoTimeout): Likewise. (getSendBufferSize): Likewise. (setSendBufferSize): Likewise. (getReceiveBufferSize): Likewise. (setReceiveBufferSize): Likewise. (receive): Likewise. (send): Likewise. (bind): Likewise. (connect): Likewise. (setReuseAddress): Likewise. (getReuseAddress): Likewise. (setBroadcast): Likewise. (getBroadcast): Likewise. (setTrafficClass): Likewise. (getTrafficClass): Likewise. * java/net/MulticastSocket.java (getInterface): Check if socket is closed. (getTTL): Likewise. (getTimeToLive): Likewise. (setInterface): Likewise. (setNetworkInterface): Likewise. (getNetworkInterface): Likewise. (setLoopbackMode): Likewise. (setTTL): Likewise. (setTimeToLive): Likewise. (joinGroup): Likewise. (leaveGroup): Likewise. (send): Likewise. * java/net/ServerSocket.java (closed): Removed. (close): Check if socket is closed, set impl to null. (isClosed): Check impl == null; (ServerSocket): Check impl argument. (getInetAddress): Check if socket is bound. (getLocalPort): Likewise. (getLocalSocketAddress): Likewise. (bind): Check if socket is closed. (implAccept): Likewise. (setSoTimeout): Likewise. (getSoTimeout): Likewise. (setReuseAddress): Likewise. (getReuseAddress): Likewise. (setReceiveBufferSize): Likewise. (getReceiveBufferSize): Likewise. (toString): Make output compliant to JDK 1.4.2. * java/net/Socket.java (closed): Removed. (Socket): Fixed documentation. (connect): Check if socket is closed, changed exception text, fixed documentation. (getInputStream): Check of socket is closed and connected. (getOutputStream): Likewise. (bind): Check if socket is closed. (setTcpNoDelay): Likewise. (getTcpNoDelay): Likewise. (setSoLinger): Likewise. (getSoLinger): Likewise. (sendUrgentData): Likewise. (setOOBInline): Likewise. (getOOBInline): Likewise. (setSoTimeout): Likewise. (getSoTimeout): Likewise. (setSendBufferSize): Likewise. (getSendBufferSize): Likewise. (setReceiveBufferSize): Likewise. (getReceiveBufferSize): Likewise. (setKeepAlive): Likewise. (getKeepAlive): Likewise. (close): Likewise. (shutdownInput): Likewise. (shutdownOutput): Likewise. (getReuseAddress): Likewise. (getTrafficClass): Likewise. (setTrafficClass): Likewise. (isClosed): Check impl == null. (toString): Added missing ']'. From-SVN: r73918 --- libjava/java/net/ServerSocket.java | 81 +++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'libjava/java/net/ServerSocket.java') 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 SocketImplFactory for all * ServerSocket's. This may only be done -- cgit v1.1