diff options
Diffstat (limited to 'libjava/java/net/DatagramSocket.java')
| -rw-r--r-- | libjava/java/net/DatagramSocket.java | 93 |
1 files changed, 54 insertions, 39 deletions
diff --git a/libjava/java/net/DatagramSocket.java b/libjava/java/net/DatagramSocket.java index 59344455e27..766c717111a 100644 --- a/libjava/java/net/DatagramSocket.java +++ b/libjava/java/net/DatagramSocket.java @@ -67,7 +67,7 @@ public class DatagramSocket * This is the user DatagramSocketImplFactory for this class. If this * variable is null, a default factory is used. */ - static DatagramSocketImplFactory factory; + private static DatagramSocketImplFactory factory; /** * This is the implementation object used by this socket. @@ -85,11 +85,6 @@ public class DatagramSocket private int remotePort = -1; /** - * Indicates when the socket is closed. - */ - private boolean closed = false; - - /** * Creates a <code>DatagramSocket</code> from a specified * <code>DatagramSocketImpl</code> instance * @@ -100,6 +95,9 @@ public class DatagramSocket */ protected DatagramSocket (DatagramSocketImpl impl) { + if (impl == null) + throw new NullPointerException("impl may not be null"); + this.impl = impl; this.remoteAddress = null; this.remotePort = -1; @@ -115,7 +113,7 @@ public class DatagramSocket */ public DatagramSocket() throws SocketException { - this(0, null); + this(new InetSocketAddress(0)); } /** @@ -130,7 +128,7 @@ public class DatagramSocket */ public DatagramSocket(int port) throws SocketException { - this(port, null); + this(new InetSocketAddress(port)); } /** @@ -226,12 +224,12 @@ public class DatagramSocket */ public void close() { - if (!closed) + if (!isClosed()) { impl.close(); + impl = null; remoteAddress = null; remotePort = -1; - closed = true; } } @@ -270,8 +268,7 @@ public class DatagramSocket */ public InetAddress getLocalAddress() { - if (impl == null - || closed) + if (isClosed()) return null; InetAddress localAddr; @@ -303,6 +300,9 @@ public class DatagramSocket */ public int getLocalPort() { + if (isClosed()) + return -1; + return impl.getLocalPort(); } @@ -318,8 +318,8 @@ public class DatagramSocket */ public synchronized int getSoTimeout() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); @@ -342,6 +342,9 @@ public class DatagramSocket */ public synchronized void setSoTimeout(int timeout) throws SocketException { + if (isClosed()) + throw new SocketException("socket is closed"); + if (timeout < 0) throw new IllegalArgumentException("Invalid timeout: " + timeout); @@ -361,8 +364,8 @@ public class DatagramSocket */ public int getSendBufferSize() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object obj = impl.getOption(SocketOptions.SO_SNDBUF); @@ -386,6 +389,9 @@ public class DatagramSocket */ public void setSendBufferSize(int size) throws SocketException { + if (isClosed()) + throw new SocketException("socket is closed"); + if (size < 0) throw new IllegalArgumentException("Buffer size is less than 0"); @@ -405,8 +411,8 @@ public class DatagramSocket */ public int getReceiveBufferSize() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object obj = impl.getOption(SocketOptions.SO_RCVBUF); @@ -430,8 +436,8 @@ public class DatagramSocket */ public void setReceiveBufferSize(int size) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); if (size < 0) throw new IllegalArgumentException("Buffer size is less than 0"); @@ -514,12 +520,13 @@ public class DatagramSocket */ 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"); + if (isClosed()) + throw new SocketException("socket is closed"); + + if (remoteAddress != null + && remoteAddress.isMulticastAddress()) + throw new IOException + ("Socket connected to a multicast address my not receive"); if (getChannel() != null && !getChannel().isBlocking ()) @@ -549,6 +556,9 @@ public class DatagramSocket */ 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. SecurityManager s = System.getSecurityManager(); if (s != null && !isConnected ()) @@ -593,6 +603,9 @@ public class DatagramSocket public void bind (SocketAddress address) throws SocketException { + if (isClosed()) + throw new SocketException("socket is closed"); + if (! (address instanceof InetSocketAddress)) throw new IllegalArgumentException (); @@ -612,7 +625,7 @@ public class DatagramSocket */ public boolean isClosed() { - return closed; + return impl == null; } /** @@ -637,6 +650,8 @@ public class DatagramSocket */ public void connect (SocketAddress address) throws SocketException { + if (isClosed()) + if ( !(address instanceof InetSocketAddress) ) throw new IllegalArgumentException ( "SocketAddress is not InetSocketAddress"); @@ -721,8 +736,8 @@ public class DatagramSocket */ public void setReuseAddress(boolean on) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); } @@ -736,8 +751,8 @@ public class DatagramSocket */ public boolean getReuseAddress() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object obj = impl.getOption (SocketOptions.SO_REUSEADDR); @@ -758,8 +773,8 @@ public class DatagramSocket */ public void setBroadcast(boolean on) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); impl.setOption (SocketOptions.SO_BROADCAST, new Boolean (on)); } @@ -773,8 +788,8 @@ public class DatagramSocket */ public boolean getBroadcast() throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object obj = impl.getOption (SocketOptions.SO_BROADCAST); @@ -799,8 +814,8 @@ public class DatagramSocket public void setTrafficClass(int tc) throws SocketException { - if (impl == null) - throw new SocketException ("Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); if (tc < 0 || tc > 255) throw new IllegalArgumentException(); @@ -819,8 +834,8 @@ public class DatagramSocket */ public int getTrafficClass() throws SocketException { - if (impl == null) - throw new SocketException( "Cannot initialize Socket implementation"); + if (isClosed()) + throw new SocketException("socket is closed"); Object obj = impl.getOption(SocketOptions.IP_TOS); |
