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/DatagramSocket.java | 93 +++++++++++++++++++++--------------- 1 file changed, 54 insertions(+), 39 deletions(-) (limited to 'libjava/java/net/DatagramSocket.java') 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. @@ -84,11 +84,6 @@ public class DatagramSocket */ private int remotePort = -1; - /** - * Indicates when the socket is closed. - */ - private boolean closed = false; - /** * Creates a DatagramSocket from a specified * DatagramSocketImpl 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); -- cgit v1.2.3