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
This commit is contained in:
committed by
Michael Koch
parent
dcb5fe8b43
commit
66e5d61fba
@@ -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 <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);
|
||||
|
||||
|
||||
@@ -125,6 +125,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public InetAddress getInterface() throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
return (InetAddress) impl.getOption(SocketOptions.IP_MULTICAST_IF);
|
||||
}
|
||||
|
||||
@@ -143,6 +146,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
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
|
||||
// other than the default PlainDatagramSocketImpl and it doesn't have
|
||||
// getTimeToLive yet.
|
||||
@@ -161,6 +167,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public int getTimeToLive() throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
return impl.getTimeToLive();
|
||||
}
|
||||
|
||||
@@ -175,6 +184,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public void setInterface(InetAddress addr) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
impl.setOption(SocketOptions.IP_MULTICAST_IF, addr);
|
||||
}
|
||||
|
||||
@@ -192,9 +204,8 @@ public class MulticastSocket extends DatagramSocket
|
||||
public void setNetworkInterface(NetworkInterface netIf)
|
||||
throws SocketException
|
||||
{
|
||||
if (impl == null)
|
||||
throw new SocketException (
|
||||
"MulticastSocket: Cant access socket implementation");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Enumeration e = netIf.getInetAddresses ();
|
||||
|
||||
@@ -219,9 +230,8 @@ public class MulticastSocket extends DatagramSocket
|
||||
public NetworkInterface getNetworkInterface()
|
||||
throws SocketException
|
||||
{
|
||||
if (impl == null)
|
||||
throw new SocketException (
|
||||
"MulticastSocket: Cant access socket implementation");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
InetAddress address =
|
||||
(InetAddress) impl.getOption (SocketOptions.IP_MULTICAST_IF);
|
||||
@@ -246,9 +256,8 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public void setLoopbackMode(boolean disable) throws SocketException
|
||||
{
|
||||
if (impl == null)
|
||||
throw new SocketException (
|
||||
"MulticastSocket: Cant access socket implementation");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
impl.setOption (SocketOptions.IP_MULTICAST_LOOP, new Boolean (disable));
|
||||
}
|
||||
@@ -262,6 +271,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public boolean getLoopbackMode() throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object obj = impl.getOption (SocketOptions.IP_MULTICAST_LOOP);
|
||||
|
||||
if (obj instanceof Boolean)
|
||||
@@ -284,6 +296,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
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
|
||||
// other than the default PlainDatagramSocketImpl and it doesn't have
|
||||
// setTimeToLive yet.
|
||||
@@ -302,6 +317,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public void setTimeToLive(int ttl) throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (ttl <= 0 || ttl > 255)
|
||||
throw new IllegalArgumentException("Invalid ttl: " + ttl);
|
||||
|
||||
@@ -319,6 +337,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public void joinGroup(InetAddress mcastaddr) throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (! mcastaddr.isMulticastAddress())
|
||||
throw new IOException("Not a Multicast address");
|
||||
|
||||
@@ -340,6 +361,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public void leaveGroup(InetAddress mcastaddr) throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (! mcastaddr.isMulticastAddress())
|
||||
throw new IOException("Not a Multicast address");
|
||||
|
||||
@@ -371,6 +395,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf)
|
||||
throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (! (mcastaddr instanceof InetSocketAddress))
|
||||
throw new IllegalArgumentException ("SocketAddress type not supported");
|
||||
|
||||
@@ -406,6 +433,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf)
|
||||
throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
InetSocketAddress tmp = (InetSocketAddress) mcastaddr;
|
||||
|
||||
if (! tmp.getAddress ().isMulticastAddress ())
|
||||
@@ -434,6 +464,9 @@ public class MulticastSocket extends DatagramSocket
|
||||
*/
|
||||
public synchronized void send(DatagramPacket p, byte ttl) throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
if (s != null)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
|
||||
+112
-35
@@ -68,29 +68,22 @@ import java.nio.channels.IllegalBlockingModeException;
|
||||
*/
|
||||
public class Socket
|
||||
{
|
||||
|
||||
// Class Variables
|
||||
|
||||
/**
|
||||
* This is the user SocketImplFactory for this class. If this variable is
|
||||
* null, a default factory is used.
|
||||
*/
|
||||
static SocketImplFactory factory;
|
||||
|
||||
// Instance Variables
|
||||
|
||||
/**
|
||||
* The implementation object to which calls are redirected
|
||||
*/
|
||||
SocketImpl impl;
|
||||
private SocketImpl impl;
|
||||
|
||||
private boolean implCreated = false;
|
||||
|
||||
private boolean inputShutdown = false;
|
||||
private boolean outputShutdown = false;
|
||||
|
||||
private boolean closed = false;
|
||||
|
||||
/**
|
||||
* Initializes a new instance of <code>Socket</code> object without
|
||||
* connecting to a remote host. This useful for subclasses of socket that
|
||||
@@ -175,7 +168,7 @@ public class Socket
|
||||
*
|
||||
* @param host The name of the remote host to connect to.
|
||||
* @param port The remote port to connect to.
|
||||
* @param loadAddr The local address to bind to.
|
||||
* @param localAddr The local address to bind to.
|
||||
* @param localPort The local port to bind to.
|
||||
*
|
||||
* @exception SecurityException If the <code>SecurityManager</code>
|
||||
@@ -298,7 +291,8 @@ public class Socket
|
||||
// that default. JDK 1.2 doc infers not to do a bind.
|
||||
}
|
||||
|
||||
private SocketImpl getImpl()
|
||||
// This has to be accessible from java.net.ServerSocket.
|
||||
SocketImpl getImpl()
|
||||
throws SocketException
|
||||
{
|
||||
try
|
||||
@@ -331,8 +325,8 @@ public class Socket
|
||||
*/
|
||||
public void bind (SocketAddress bindpoint) throws IOException
|
||||
{
|
||||
if (closed)
|
||||
throw new SocketException ("Socket is closed");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
// XXX: JDK 1.4.1 API documentation says that if bindpoint is null the
|
||||
// socket will be bound to an ephemeral port and a valid local address.
|
||||
@@ -390,6 +384,8 @@ public class Socket
|
||||
* until established or an error occurs.
|
||||
*
|
||||
* @param endpoint The address to connect to
|
||||
* @param timeout The length of the timeout in milliseconds, or
|
||||
* 0 to indicate no timeout.
|
||||
*
|
||||
* @exception IOException If an error occurs
|
||||
* @exception IllegalArgumentException If the address type is not supported
|
||||
@@ -402,11 +398,11 @@ public class Socket
|
||||
public void connect (SocketAddress endpoint, int timeout)
|
||||
throws IOException
|
||||
{
|
||||
if (closed)
|
||||
throw new SocketException ("Socket is closed");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (! (endpoint instanceof InetSocketAddress))
|
||||
throw new IllegalArgumentException ("Address type not supported");
|
||||
throw new IllegalArgumentException("unsupported address type");
|
||||
|
||||
if (getChannel() != null
|
||||
&& !getChannel().isBlocking ())
|
||||
@@ -598,10 +594,13 @@ public class Socket
|
||||
*/
|
||||
public InputStream getInputStream () throws IOException
|
||||
{
|
||||
if (getImpl() != null)
|
||||
return getImpl().getInputStream();
|
||||
|
||||
throw new IOException("Not connected");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (!isConnected())
|
||||
throw new IOException("not connected");
|
||||
|
||||
return getImpl().getInputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -613,10 +612,13 @@ public class Socket
|
||||
*/
|
||||
public OutputStream getOutputStream () throws IOException
|
||||
{
|
||||
if (getImpl() != null)
|
||||
return getImpl().getOutputStream();
|
||||
|
||||
throw new IOException("Not connected");
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (!isConnected())
|
||||
throw new IOException("not connected");
|
||||
|
||||
return getImpl().getOutputStream();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -630,6 +632,9 @@ public class Socket
|
||||
*/
|
||||
public void setTcpNoDelay (boolean on) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
|
||||
}
|
||||
|
||||
@@ -647,6 +652,9 @@ public class Socket
|
||||
*/
|
||||
public boolean getTcpNoDelay() throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object on = getImpl().getOption(SocketOptions.TCP_NODELAY);
|
||||
|
||||
if (on instanceof Boolean)
|
||||
@@ -674,6 +682,9 @@ public class Socket
|
||||
*/
|
||||
public void setSoLinger(boolean on, int linger) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (on == true)
|
||||
{
|
||||
if (linger < 0)
|
||||
@@ -708,6 +719,9 @@ public class Socket
|
||||
*/
|
||||
public int getSoLinger() throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object linger = getImpl().getOption(SocketOptions.SO_LINGER);
|
||||
|
||||
if (linger instanceof Integer)
|
||||
@@ -728,6 +742,9 @@ public class Socket
|
||||
*/
|
||||
public void sendUrgentData (int data) throws IOException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().sendUrgentData (data);
|
||||
}
|
||||
|
||||
@@ -742,18 +759,26 @@ public class Socket
|
||||
*/
|
||||
public void setOOBInline (boolean on) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current setting of the SO_OOBINLINE option for this socket
|
||||
*
|
||||
* @return True if SO_OOBINLINE is set, false otherwise.
|
||||
*
|
||||
* @exception SocketException If an error occurs
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean getOOBInline () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object buf = getImpl().getOption(SocketOptions.SO_OOBINLINE);
|
||||
|
||||
if (buf instanceof Boolean)
|
||||
@@ -781,6 +806,9 @@ public class Socket
|
||||
*/
|
||||
public synchronized void setSoTimeout (int timeout) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (timeout < 0)
|
||||
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
|
||||
|
||||
@@ -806,6 +834,9 @@ public class Socket
|
||||
*/
|
||||
public synchronized int getSoTimeout () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);
|
||||
if (timeout instanceof Integer)
|
||||
return(((Integer)timeout).intValue());
|
||||
@@ -827,6 +858,9 @@ public class Socket
|
||||
*/
|
||||
public void setSendBufferSize (int size) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (size <= 0)
|
||||
throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
|
||||
|
||||
@@ -846,6 +880,9 @@ public class Socket
|
||||
*/
|
||||
public int getSendBufferSize () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);
|
||||
|
||||
if (buf instanceof Integer)
|
||||
@@ -868,6 +905,9 @@ public class Socket
|
||||
*/
|
||||
public void setReceiveBufferSize (int size) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (size <= 0)
|
||||
throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
|
||||
|
||||
@@ -887,6 +927,9 @@ public class Socket
|
||||
*/
|
||||
public int getReceiveBufferSize () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);
|
||||
|
||||
if (buf instanceof Integer)
|
||||
@@ -907,6 +950,9 @@ public class Socket
|
||||
*/
|
||||
public void setKeepAlive (boolean on) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
|
||||
}
|
||||
|
||||
@@ -922,6 +968,9 @@ public class Socket
|
||||
*/
|
||||
public boolean getKeepAlive () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object buf = getImpl().getOption(SocketOptions.SO_KEEPALIVE);
|
||||
|
||||
if (buf instanceof Boolean)
|
||||
@@ -937,13 +986,15 @@ public class Socket
|
||||
*/
|
||||
public synchronized void close () throws IOException
|
||||
{
|
||||
if (getImpl() != null)
|
||||
getImpl().close();
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().close();
|
||||
|
||||
if (getChannel() != null)
|
||||
getChannel().close();
|
||||
|
||||
closed = true;
|
||||
impl = null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -958,7 +1009,8 @@ public class Socket
|
||||
if (isConnected())
|
||||
return ("Socket[addr=" + getImpl().getInetAddress()
|
||||
+ ",port=" + getImpl().getPort()
|
||||
+ ",localport=" + getImpl().getLocalPort());
|
||||
+ ",localport=" + getImpl().getLocalPort()
|
||||
+ "]");
|
||||
}
|
||||
catch (SocketException e)
|
||||
{
|
||||
@@ -968,8 +1020,6 @@ public class Socket
|
||||
return "Socket[unconnected]";
|
||||
}
|
||||
|
||||
// Class Methods
|
||||
|
||||
/**
|
||||
* Sets the <code>SocketImplFactory</code>. This may be done only once per
|
||||
* virtual machine. Subsequent attempts will generate a
|
||||
@@ -1010,9 +1060,10 @@ public class Socket
|
||||
*/
|
||||
public void shutdownInput() throws IOException
|
||||
{
|
||||
if (getImpl() != null)
|
||||
getImpl().shutdownInput();
|
||||
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().shutdownInput();
|
||||
inputShutdown = true;
|
||||
}
|
||||
|
||||
@@ -1025,9 +1076,10 @@ public class Socket
|
||||
*/
|
||||
public void shutdownOutput() throws IOException
|
||||
{
|
||||
if (getImpl() != null)
|
||||
getImpl().shutdownOutput();
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
getImpl().shutdownOutput();
|
||||
outputShutdown = true;
|
||||
}
|
||||
|
||||
@@ -1046,12 +1098,17 @@ public class Socket
|
||||
/**
|
||||
* Checks if the SO_REUSEADDR option is enabled
|
||||
*
|
||||
* @return True if SO_REUSEADDR is set, false otherwise.
|
||||
*
|
||||
* @exception SocketException If an error occurs
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean getReuseAddress () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object reuseaddr = getImpl().getOption (SocketOptions.SO_REUSEADDR);
|
||||
|
||||
if (!(reuseaddr instanceof Boolean))
|
||||
@@ -1063,6 +1120,8 @@ public class Socket
|
||||
/**
|
||||
* Enables/Disables the SO_REUSEADDR option
|
||||
*
|
||||
* @param reuseAddress True if SO_REUSEADDR should be set.
|
||||
*
|
||||
* @exception SocketException If an error occurs
|
||||
*
|
||||
* @since 1.4
|
||||
@@ -1075,6 +1134,8 @@ public class Socket
|
||||
/**
|
||||
* Returns the current traffic class
|
||||
*
|
||||
* @return The current traffic class.
|
||||
*
|
||||
* @exception SocketException If an error occurs
|
||||
*
|
||||
* @see Socket#setTrafficClass(int tc)
|
||||
@@ -1083,6 +1144,9 @@ public class Socket
|
||||
*/
|
||||
public int getTrafficClass () throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
Object obj = getImpl().getOption(SocketOptions.IP_TOS);
|
||||
|
||||
if (obj instanceof Integer)
|
||||
@@ -1105,6 +1169,9 @@ public class Socket
|
||||
*/
|
||||
public void setTrafficClass (int tc) throws SocketException
|
||||
{
|
||||
if (isClosed())
|
||||
throw new SocketException("socket is closed");
|
||||
|
||||
if (tc < 0 || tc > 255)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
@@ -1114,6 +1181,8 @@ public class Socket
|
||||
/**
|
||||
* Checks if the socket is connected
|
||||
*
|
||||
* @return True if socket is connected, false otherwise.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean isConnected ()
|
||||
@@ -1131,6 +1200,8 @@ public class Socket
|
||||
/**
|
||||
* Checks if the socket is already bound.
|
||||
*
|
||||
* @return True if socket is bound, false otherwise.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean isBound ()
|
||||
@@ -1141,16 +1212,20 @@ public class Socket
|
||||
/**
|
||||
* Checks if the socket is closed.
|
||||
*
|
||||
* @return True if socket is closed, false otherwise.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean isClosed ()
|
||||
{
|
||||
return closed;
|
||||
return impl == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the socket's input stream is shutdown
|
||||
*
|
||||
* @return True if input is shut down.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean isInputShutdown ()
|
||||
@@ -1161,6 +1236,8 @@ public class Socket
|
||||
/**
|
||||
* Checks if the socket's output stream is shutdown
|
||||
*
|
||||
* @return True if output is shut down.
|
||||
*
|
||||
* @since 1.4
|
||||
*/
|
||||
public boolean isOutputShutdown ()
|
||||
|
||||
Reference in New Issue
Block a user