diff options
author | Michael Koch <konqueror@gmx.de> | 2003-11-25 10:09:48 +0000 |
---|---|---|
committer | Michael Koch <mkoch@gcc.gnu.org> | 2003-11-25 10:09:48 +0000 |
commit | 66e5d61fba14cd936e0183ba014703c196269590 (patch) | |
tree | 5913f733c032c45af5ae571b8e9319e4d4101b1c /libjava/java/net/Socket.java | |
parent | dcb5fe8b43cd9eaddbfe4bfae93653e817867420 (diff) | |
download | gcc-66e5d61fba14cd936e0183ba014703c196269590.zip gcc-66e5d61fba14cd936e0183ba014703c196269590.tar.gz gcc-66e5d61fba14cd936e0183ba014703c196269590.tar.bz2 |
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
Diffstat (limited to 'libjava/java/net/Socket.java')
-rw-r--r-- | libjava/java/net/Socket.java | 147 |
1 files changed, 112 insertions, 35 deletions
diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java index 14ee5b3..1b443d0 100644 --- a/libjava/java/net/Socket.java +++ b/libjava/java/net/Socket.java @@ -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 () |