Commit 66e5d61f by Michael Koch Committed by Michael Koch

2003-11-25 Michael Koch <konqueror@gmx.de>

	* 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
parent dcb5fe8b
2003-11-25 Michael Koch <konqueror@gmx.de>
* 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 ']'.
2003-11-24 Tom Tromey <tromey@redhat.com> 2003-11-24 Tom Tromey <tromey@redhat.com>
* Makefile.in: Rebuilt. * Makefile.in: Rebuilt.
......
...@@ -67,7 +67,7 @@ public class DatagramSocket ...@@ -67,7 +67,7 @@ public class DatagramSocket
* This is the user DatagramSocketImplFactory for this class. If this * This is the user DatagramSocketImplFactory for this class. If this
* variable is null, a default factory is used. * variable is null, a default factory is used.
*/ */
static DatagramSocketImplFactory factory; private static DatagramSocketImplFactory factory;
/** /**
* This is the implementation object used by this socket. * This is the implementation object used by this socket.
...@@ -85,11 +85,6 @@ public class DatagramSocket ...@@ -85,11 +85,6 @@ public class DatagramSocket
private int remotePort = -1; private int remotePort = -1;
/** /**
* Indicates when the socket is closed.
*/
private boolean closed = false;
/**
* Creates a <code>DatagramSocket</code> from a specified * Creates a <code>DatagramSocket</code> from a specified
* <code>DatagramSocketImpl</code> instance * <code>DatagramSocketImpl</code> instance
* *
...@@ -100,6 +95,9 @@ public class DatagramSocket ...@@ -100,6 +95,9 @@ public class DatagramSocket
*/ */
protected DatagramSocket (DatagramSocketImpl impl) protected DatagramSocket (DatagramSocketImpl impl)
{ {
if (impl == null)
throw new NullPointerException("impl may not be null");
this.impl = impl; this.impl = impl;
this.remoteAddress = null; this.remoteAddress = null;
this.remotePort = -1; this.remotePort = -1;
...@@ -115,7 +113,7 @@ public class DatagramSocket ...@@ -115,7 +113,7 @@ public class DatagramSocket
*/ */
public DatagramSocket() throws SocketException public DatagramSocket() throws SocketException
{ {
this(0, null); this(new InetSocketAddress(0));
} }
/** /**
...@@ -130,7 +128,7 @@ public class DatagramSocket ...@@ -130,7 +128,7 @@ public class DatagramSocket
*/ */
public DatagramSocket(int port) throws SocketException public DatagramSocket(int port) throws SocketException
{ {
this(port, null); this(new InetSocketAddress(port));
} }
/** /**
...@@ -226,12 +224,12 @@ public class DatagramSocket ...@@ -226,12 +224,12 @@ public class DatagramSocket
*/ */
public void close() public void close()
{ {
if (!closed) if (!isClosed())
{ {
impl.close(); impl.close();
impl = null;
remoteAddress = null; remoteAddress = null;
remotePort = -1; remotePort = -1;
closed = true;
} }
} }
...@@ -270,8 +268,7 @@ public class DatagramSocket ...@@ -270,8 +268,7 @@ public class DatagramSocket
*/ */
public InetAddress getLocalAddress() public InetAddress getLocalAddress()
{ {
if (impl == null if (isClosed())
|| closed)
return null; return null;
InetAddress localAddr; InetAddress localAddr;
...@@ -303,6 +300,9 @@ public class DatagramSocket ...@@ -303,6 +300,9 @@ public class DatagramSocket
*/ */
public int getLocalPort() public int getLocalPort()
{ {
if (isClosed())
return -1;
return impl.getLocalPort(); return impl.getLocalPort();
} }
...@@ -318,8 +318,8 @@ public class DatagramSocket ...@@ -318,8 +318,8 @@ public class DatagramSocket
*/ */
public synchronized int getSoTimeout() throws SocketException public synchronized int getSoTimeout() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
...@@ -342,6 +342,9 @@ public class DatagramSocket ...@@ -342,6 +342,9 @@ public class DatagramSocket
*/ */
public synchronized void setSoTimeout(int timeout) throws SocketException public synchronized void setSoTimeout(int timeout) throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (timeout < 0) if (timeout < 0)
throw new IllegalArgumentException("Invalid timeout: " + timeout); throw new IllegalArgumentException("Invalid timeout: " + timeout);
...@@ -361,8 +364,8 @@ public class DatagramSocket ...@@ -361,8 +364,8 @@ public class DatagramSocket
*/ */
public int getSendBufferSize() throws SocketException public int getSendBufferSize() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object obj = impl.getOption(SocketOptions.SO_SNDBUF); Object obj = impl.getOption(SocketOptions.SO_SNDBUF);
...@@ -386,6 +389,9 @@ public class DatagramSocket ...@@ -386,6 +389,9 @@ public class DatagramSocket
*/ */
public void setSendBufferSize(int size) throws SocketException public void setSendBufferSize(int size) throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (size < 0) if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0"); throw new IllegalArgumentException("Buffer size is less than 0");
...@@ -405,8 +411,8 @@ public class DatagramSocket ...@@ -405,8 +411,8 @@ public class DatagramSocket
*/ */
public int getReceiveBufferSize() throws SocketException public int getReceiveBufferSize() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object obj = impl.getOption(SocketOptions.SO_RCVBUF); Object obj = impl.getOption(SocketOptions.SO_RCVBUF);
...@@ -430,8 +436,8 @@ public class DatagramSocket ...@@ -430,8 +436,8 @@ public class DatagramSocket
*/ */
public void setReceiveBufferSize(int size) throws SocketException public void setReceiveBufferSize(int size) throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
if (size < 0) if (size < 0)
throw new IllegalArgumentException("Buffer size is less than 0"); throw new IllegalArgumentException("Buffer size is less than 0");
...@@ -514,12 +520,13 @@ public class DatagramSocket ...@@ -514,12 +520,13 @@ public class DatagramSocket
*/ */
public synchronized void receive(DatagramPacket p) throws IOException public synchronized void receive(DatagramPacket p) throws IOException
{ {
if (impl == null) if (isClosed())
throw new IOException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
if (remoteAddress != null && remoteAddress.isMulticastAddress ()) if (remoteAddress != null
throw new IOException ( && remoteAddress.isMulticastAddress())
"Socket connected to a multicast address my not receive"); throw new IOException
("Socket connected to a multicast address my not receive");
if (getChannel() != null if (getChannel() != null
&& !getChannel().isBlocking ()) && !getChannel().isBlocking ())
...@@ -549,6 +556,9 @@ public class DatagramSocket ...@@ -549,6 +556,9 @@ public class DatagramSocket
*/ */
public void send(DatagramPacket p) throws IOException public void send(DatagramPacket p) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
// JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api. // JDK1.2: Don't do security checks if socket is connected; see jdk1.2 api.
SecurityManager s = System.getSecurityManager(); SecurityManager s = System.getSecurityManager();
if (s != null && !isConnected ()) if (s != null && !isConnected ())
...@@ -593,6 +603,9 @@ public class DatagramSocket ...@@ -593,6 +603,9 @@ public class DatagramSocket
public void bind (SocketAddress address) public void bind (SocketAddress address)
throws SocketException throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (! (address instanceof InetSocketAddress)) if (! (address instanceof InetSocketAddress))
throw new IllegalArgumentException (); throw new IllegalArgumentException ();
...@@ -612,7 +625,7 @@ public class DatagramSocket ...@@ -612,7 +625,7 @@ public class DatagramSocket
*/ */
public boolean isClosed() public boolean isClosed()
{ {
return closed; return impl == null;
} }
/** /**
...@@ -637,6 +650,8 @@ public class DatagramSocket ...@@ -637,6 +650,8 @@ public class DatagramSocket
*/ */
public void connect (SocketAddress address) throws SocketException public void connect (SocketAddress address) throws SocketException
{ {
if (isClosed())
if ( !(address instanceof InetSocketAddress) ) if ( !(address instanceof InetSocketAddress) )
throw new IllegalArgumentException ( throw new IllegalArgumentException (
"SocketAddress is not InetSocketAddress"); "SocketAddress is not InetSocketAddress");
...@@ -721,8 +736,8 @@ public class DatagramSocket ...@@ -721,8 +736,8 @@ public class DatagramSocket
*/ */
public void setReuseAddress(boolean on) throws SocketException public void setReuseAddress(boolean on) throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
} }
...@@ -736,8 +751,8 @@ public class DatagramSocket ...@@ -736,8 +751,8 @@ public class DatagramSocket
*/ */
public boolean getReuseAddress() throws SocketException public boolean getReuseAddress() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object obj = impl.getOption (SocketOptions.SO_REUSEADDR); Object obj = impl.getOption (SocketOptions.SO_REUSEADDR);
...@@ -758,8 +773,8 @@ public class DatagramSocket ...@@ -758,8 +773,8 @@ public class DatagramSocket
*/ */
public void setBroadcast(boolean on) throws SocketException public void setBroadcast(boolean on) throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on)); impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on));
} }
...@@ -773,8 +788,8 @@ public class DatagramSocket ...@@ -773,8 +788,8 @@ public class DatagramSocket
*/ */
public boolean getBroadcast() throws SocketException public boolean getBroadcast() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object obj = impl.getOption (SocketOptions.SO_BROADCAST); Object obj = impl.getOption (SocketOptions.SO_BROADCAST);
...@@ -799,8 +814,8 @@ public class DatagramSocket ...@@ -799,8 +814,8 @@ public class DatagramSocket
public void setTrafficClass(int tc) public void setTrafficClass(int tc)
throws SocketException throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ("Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
if (tc < 0 || tc > 255) if (tc < 0 || tc > 255)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
...@@ -819,8 +834,8 @@ public class DatagramSocket ...@@ -819,8 +834,8 @@ public class DatagramSocket
*/ */
public int getTrafficClass() throws SocketException public int getTrafficClass() throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException( "Cannot initialize Socket implementation"); throw new SocketException("socket is closed");
Object obj = impl.getOption(SocketOptions.IP_TOS); Object obj = impl.getOption(SocketOptions.IP_TOS);
......
...@@ -125,6 +125,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -125,6 +125,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public InetAddress getInterface() throws SocketException public InetAddress getInterface() throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF); return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
} }
...@@ -143,6 +146,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -143,6 +146,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public byte getTTL() throws IOException public byte getTTL() throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
// Use getTTL here rather than getTimeToLive in case we're using an impl // Use getTTL here rather than getTimeToLive in case we're using an impl
// other than the default PlainDatagramSocketImpl and it doesn't have // other than the default PlainDatagramSocketImpl and it doesn't have
// getTimeToLive yet. // getTimeToLive yet.
...@@ -161,6 +167,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -161,6 +167,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public int getTimeToLive() throws IOException public int getTimeToLive() throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
return impl.getTimeToLive(); return impl.getTimeToLive();
} }
...@@ -175,6 +184,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -175,6 +184,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void setInterface(InetAddress addr) throws SocketException public void setInterface(InetAddress addr) throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
impl.setOption(SocketOptions.IP_MULTICAST_IF, addr); impl.setOption(SocketOptions.IP_MULTICAST_IF, addr);
} }
...@@ -192,9 +204,8 @@ public class MulticastSocket extends DatagramSocket ...@@ -192,9 +204,8 @@ public class MulticastSocket extends DatagramSocket
public void setNetworkInterface(NetworkInterface netIf) public void setNetworkInterface(NetworkInterface netIf)
throws SocketException throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ( throw new SocketException("socket is closed");
"MulticastSocket: Cant access socket implementation");
Enumeration e = netIf.getInetAddresses (); Enumeration e = netIf.getInetAddresses ();
...@@ -219,9 +230,8 @@ public class MulticastSocket extends DatagramSocket ...@@ -219,9 +230,8 @@ public class MulticastSocket extends DatagramSocket
public NetworkInterface getNetworkInterface() public NetworkInterface getNetworkInterface()
throws SocketException throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ( throw new SocketException("socket is closed");
"MulticastSocket: Cant access socket implementation");
InetAddress address = InetAddress address =
(InetAddress) impl.getOption (SocketOptions.IP_MULTICAST_IF); (InetAddress) impl.getOption (SocketOptions.IP_MULTICAST_IF);
...@@ -246,9 +256,8 @@ public class MulticastSocket extends DatagramSocket ...@@ -246,9 +256,8 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void setLoopbackMode(boolean disable) throws SocketException public void setLoopbackMode(boolean disable) throws SocketException
{ {
if (impl == null) if (isClosed())
throw new SocketException ( throw new SocketException("socket is closed");
"MulticastSocket: Cant access socket implementation");
impl.setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable)); impl.setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable));
} }
...@@ -262,6 +271,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -262,6 +271,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public boolean getLoopbackMode() throws SocketException public boolean getLoopbackMode() throws SocketException
{ {
if (isClosed())
throw new SocketException("socket is closed");
Object obj = impl.getOption (SocketOptions.IP_MULTICAST_LOOP); Object obj = impl.getOption (SocketOptions.IP_MULTICAST_LOOP);
if (obj instanceof Boolean) if (obj instanceof Boolean)
...@@ -284,6 +296,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -284,6 +296,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void setTTL(byte ttl) throws IOException public void setTTL(byte ttl) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
// Use setTTL here rather than setTimeToLive in case we're using an impl // Use setTTL here rather than setTimeToLive in case we're using an impl
// other than the default PlainDatagramSocketImpl and it doesn't have // other than the default PlainDatagramSocketImpl and it doesn't have
// setTimeToLive yet. // setTimeToLive yet.
...@@ -302,6 +317,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -302,6 +317,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void setTimeToLive(int ttl) throws IOException public void setTimeToLive(int ttl) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (ttl <= 0 || ttl > 255) if (ttl <= 0 || ttl > 255)
throw new IllegalArgumentException("Invalid ttl: " + ttl); throw new IllegalArgumentException("Invalid ttl: " + ttl);
...@@ -319,6 +337,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -319,6 +337,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void joinGroup(InetAddress mcastaddr) throws IOException public void joinGroup(InetAddress mcastaddr) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (! mcastaddr.isMulticastAddress()) if (! mcastaddr.isMulticastAddress())
throw new IOException("Not a Multicast address"); throw new IOException("Not a Multicast address");
...@@ -340,6 +361,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -340,6 +361,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public void leaveGroup(InetAddress mcastaddr) throws IOException public void leaveGroup(InetAddress mcastaddr) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (! mcastaddr.isMulticastAddress()) if (! mcastaddr.isMulticastAddress())
throw new IOException("Not a Multicast address"); throw new IOException("Not a Multicast address");
...@@ -371,6 +395,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -371,6 +395,9 @@ public class MulticastSocket extends DatagramSocket
public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
throws IOException throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
if (! (mcastaddr instanceof InetSocketAddress)) if (! (mcastaddr instanceof InetSocketAddress))
throw new IllegalArgumentException ("SocketAddress type not supported"); throw new IllegalArgumentException ("SocketAddress type not supported");
...@@ -406,6 +433,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -406,6 +433,9 @@ public class MulticastSocket extends DatagramSocket
public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
throws IOException throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
InetSocketAddress tmp = (InetSocketAddress) mcastaddr; InetSocketAddress tmp = (InetSocketAddress) mcastaddr;
if (! tmp.getAddress ().isMulticastAddress ()) if (! tmp.getAddress ().isMulticastAddress ())
...@@ -434,6 +464,9 @@ public class MulticastSocket extends DatagramSocket ...@@ -434,6 +464,9 @@ public class MulticastSocket extends DatagramSocket
*/ */
public synchronized void send(DatagramPacket p, byte ttl) throws IOException public synchronized void send(DatagramPacket p, byte ttl) throws IOException
{ {
if (isClosed())
throw new SocketException("socket is closed");
SecurityManager s = System.getSecurityManager(); SecurityManager s = System.getSecurityManager();
if (s != null) if (s != null)
{ {
......
...@@ -73,8 +73,6 @@ public class ServerSocket ...@@ -73,8 +73,6 @@ public class ServerSocket
*/ */
private SocketImpl impl; private SocketImpl impl;
private boolean closed = false;
/* /*
* This constructor is only used by java.nio. * This constructor is only used by java.nio.
*/ */
...@@ -82,6 +80,9 @@ public class ServerSocket ...@@ -82,6 +80,9 @@ public class ServerSocket
//ServerSocket (PlainSocketImpl impl) throws IOException //ServerSocket (PlainSocketImpl impl) throws IOException
ServerSocket (SocketImpl impl) throws IOException ServerSocket (SocketImpl impl) throws IOException
{ {
if (impl == null)
throw new NullPointerException("impl may not be null");
this.impl = impl; this.impl = impl;
this.impl.create (true); this.impl.create (true);
} }
...@@ -208,8 +209,8 @@ public class ServerSocket ...@@ -208,8 +209,8 @@ public class ServerSocket
*/ */
public void bind (SocketAddress endpoint, int backlog) throws IOException public void bind (SocketAddress endpoint, int backlog) throws IOException
{ {
if (closed) if (isClosed())
throw new SocketException ("ServerSocket is closed"); throw new SocketException("ServerSocket is closed");
if (! (endpoint instanceof InetSocketAddress)) if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported"); throw new IllegalArgumentException ("Address type not supported");
...@@ -249,12 +250,16 @@ public class ServerSocket ...@@ -249,12 +250,16 @@ public class ServerSocket
*/ */
public InetAddress getInetAddress() public InetAddress getInetAddress()
{ {
if (!isBound())
return null;
try try
{ {
return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR); return (InetAddress) impl.getOption (SocketOptions.SO_BINDADDR);
} }
catch (SocketException e) catch (SocketException e)
{ {
// This never happens as we are bound.
return null; return null;
} }
} }
...@@ -266,6 +271,9 @@ public class ServerSocket ...@@ -266,6 +271,9 @@ public class ServerSocket
*/ */
public int getLocalPort() public int getLocalPort()
{ {
if (!isBound())
return -1;
return impl.getLocalPort(); return impl.getLocalPort();
} }
...@@ -276,12 +284,10 @@ public class ServerSocket ...@@ -276,12 +284,10 @@ public class ServerSocket
*/ */
public SocketAddress getLocalSocketAddress() public SocketAddress getLocalSocketAddress()
{ {
InetAddress addr = getInetAddress(); if (!isBound())
return null;
if (addr != null)
return new InetSocketAddress (getInetAddress(), getLocalPort()); return new InetSocketAddress(getInetAddress(), getLocalPort());
return null;
} }
/** /**
...@@ -303,10 +309,9 @@ public class ServerSocket ...@@ -303,10 +309,9 @@ public class ServerSocket
if (sm != null) if (sm != null)
sm.checkListen (impl.getLocalPort ()); sm.checkListen (impl.getLocalPort ());
Socket s = new Socket(); Socket socket = new Socket();
implAccept (s); implAccept (socket);
return socket;
return s;
} }
/** /**
...@@ -322,14 +327,17 @@ public class ServerSocket ...@@ -322,14 +327,17 @@ public class ServerSocket
* *
* @since 1.1 * @since 1.1
*/ */
protected final void implAccept (Socket s) protected final void implAccept (Socket socket)
throws IOException throws IOException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
if (getChannel() != null if (getChannel() != null
&& !getChannel().isBlocking()) && !getChannel().isBlocking())
throw new IllegalBlockingModeException(); throw new IllegalBlockingModeException();
impl.accept(s.impl); impl.accept(socket.getImpl());
} }
/** /**
...@@ -339,12 +347,15 @@ public class ServerSocket ...@@ -339,12 +347,15 @@ public class ServerSocket
*/ */
public void close () throws IOException public void close () throws IOException
{ {
impl.close (); if (!isClosed())
{
impl.close();
if (getChannel() != null) if (getChannel() != null)
getChannel().close (); getChannel().close();
closed = true; impl = null;
}
} }
/** /**
...@@ -387,7 +398,7 @@ public class ServerSocket ...@@ -387,7 +398,7 @@ public class ServerSocket
*/ */
public boolean isClosed() public boolean isClosed()
{ {
return closed; return impl == null;
} }
/** /**
...@@ -404,6 +415,9 @@ public class ServerSocket ...@@ -404,6 +415,9 @@ public class ServerSocket
*/ */
public void setSoTimeout (int timeout) throws SocketException public void setSoTimeout (int timeout) throws SocketException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
if (timeout < 0) if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
...@@ -424,6 +438,9 @@ public class ServerSocket ...@@ -424,6 +438,9 @@ public class ServerSocket
*/ */
public int getSoTimeout () throws IOException public int getSoTimeout () throws IOException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (!(timeout instanceof Integer)) if (!(timeout instanceof Integer))
...@@ -442,6 +459,9 @@ public class ServerSocket ...@@ -442,6 +459,9 @@ public class ServerSocket
public void setReuseAddress (boolean on) public void setReuseAddress (boolean on)
throws SocketException throws SocketException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
} }
...@@ -455,6 +475,9 @@ public class ServerSocket ...@@ -455,6 +475,9 @@ public class ServerSocket
public boolean getReuseAddress() public boolean getReuseAddress()
throws SocketException throws SocketException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean)) if (!(reuseaddr instanceof Boolean))
...@@ -478,6 +501,9 @@ public class ServerSocket ...@@ -478,6 +501,9 @@ public class ServerSocket
public void setReceiveBufferSize (int size) public void setReceiveBufferSize (int size)
throws SocketException throws SocketException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException ("SO_RCVBUF value must be > 0"); throw new IllegalArgumentException ("SO_RCVBUF value must be > 0");
...@@ -498,6 +524,9 @@ public class ServerSocket ...@@ -498,6 +524,9 @@ public class ServerSocket
public int getReceiveBufferSize () public int getReceiveBufferSize ()
throws SocketException throws SocketException
{ {
if (isClosed())
throw new SocketException("ServerSocket is closed");
Object buf = impl.getOption (SocketOptions.SO_RCVBUF); Object buf = impl.getOption (SocketOptions.SO_RCVBUF);
if (!(buf instanceof Integer)) if (!(buf instanceof Integer))
...@@ -513,11 +542,15 @@ public class ServerSocket ...@@ -513,11 +542,15 @@ public class ServerSocket
*/ */
public String toString () 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 * Sets the <code>SocketImplFactory</code> for all
* <code>ServerSocket</code>'s. This may only be done * <code>ServerSocket</code>'s. This may only be done
......
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