aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/net/Socket.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/net/Socket.java')
-rw-r--r--libjava/java/net/Socket.java123
1 files changed, 23 insertions, 100 deletions
diff --git a/libjava/java/net/Socket.java b/libjava/java/net/Socket.java
index ef88de5c151..a5397bf7a49 100644
--- a/libjava/java/net/Socket.java
+++ b/libjava/java/net/Socket.java
@@ -1,5 +1,5 @@
/* Socket.java -- Client socket implementation
- Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
+ Copyright (C) 1998, 1999, 2000, 2002, 2003 Free Software Foundation, Inc.
This file is part of GNU Classpath.
@@ -35,8 +35,10 @@ this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */
+
package java.net;
+import gnu.java.net.PlainSocketImpl;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
@@ -82,10 +84,8 @@ public class Socket
*/
SocketImpl impl;
- private boolean inputShutdown;
- private boolean outputShutdown;
-
- SocketChannel ch; // this field must have been set if created by SocketChannel
+ private boolean inputShutdown = false;
+ private boolean outputShutdown = false;
private boolean closed = false;
@@ -103,9 +103,6 @@ public class Socket
impl = factory.createSocketImpl();
else
impl = new PlainSocketImpl();
-
- inputShutdown = false;
- outputShutdown = false;
}
/**
@@ -115,9 +112,8 @@ public class Socket
* <p>
* Additionally, this socket will be created using the supplied
* implementation class instead the default class or one returned by a
- * factory. This value can be <code>null</code>, but if it is, all instance
- * methods in <code>Socket</code> should be overridden because most of them
- * rely on this value being populated.
+ * factory. If this value is <code>null</code>, the default Socket
+ * implementation is used.
*
* @param impl The <code>SocketImpl</code> to use for this
* <code>Socket</code>
@@ -128,9 +124,10 @@ public class Socket
*/
protected Socket (SocketImpl impl) throws SocketException
{
- this.impl = impl;
- this.inputShutdown = false;
- this.outputShutdown = false;
+ if (impl == null)
+ this.impl = new PlainSocketImpl();
+ else
+ this.impl = impl;
}
/**
@@ -282,12 +279,6 @@ public class Socket
{
this();
- if (raddr == null)
- throw new NullPointerException ();
-
- if (impl == null)
- throw new IOException("Cannot initialize Socket implementation");
-
SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkConnect(raddr.getHostName(), rport);
@@ -305,15 +296,6 @@ public class Socket
// that default. JDK 1.2 doc infers not to do a bind.
}
- /*
- * This method may only be used by java.nio.channels.ServerSocketChannel.accept and
- * java.nio.channels.SocketChannel.open.
- */
- void setChannel (SocketChannel ch)
- {
- this.ch = ch;
- }
-
/**
* Binds the socket to the givent local address/port
*
@@ -351,17 +333,17 @@ public class Socket
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
@@ -408,7 +390,8 @@ public class Socket
if (! (endpoint instanceof InetSocketAddress))
throw new IllegalArgumentException ("Address type not supported");
- if (ch != null && !ch.isBlocking ())
+ if (getChannel() != null
+ && !getChannel().isBlocking ())
throw new IllegalBlockingModeException ();
if (!isBound ())
@@ -420,17 +403,17 @@ public class Socket
}
catch (IOException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (RuntimeException exception)
{
- impl.close ();
+ close ();
throw exception;
}
catch (Error error)
{
- impl.close ();
+ close ();
throw error;
}
}
@@ -443,10 +426,7 @@ public class Socket
*/
public InetAddress getInetAddress ()
{
- if (impl != null)
- return impl.getInetAddress();
-
- return null;
+ return impl.getInetAddress();
}
/**
@@ -459,9 +439,6 @@ public class Socket
*/
public InetAddress getLocalAddress ()
{
- if (impl == null)
- return null;
-
InetAddress addr = null;
try
{
@@ -586,9 +563,6 @@ public class Socket
*/
public void setTcpNoDelay (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
}
@@ -606,9 +580,6 @@ public class Socket
*/
public boolean getTcpNoDelay() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object on = impl.getOption(SocketOptions.TCP_NODELAY);
if (on instanceof Boolean)
@@ -636,9 +607,6 @@ public class Socket
*/
public void setSoLinger(boolean on, int linger) throws SocketException
{
- if (impl == null)
- throw new SocketException("No socket created");
-
if (on == true)
{
if (linger < 0)
@@ -673,9 +641,6 @@ public class Socket
*/
public int getSoLinger() throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object linger = impl.getOption(SocketOptions.SO_LINGER);
if (linger instanceof Integer)
return(((Integer)linger).intValue());
@@ -709,9 +674,6 @@ public class Socket
*/
public void setOOBInline (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
}
@@ -724,9 +686,6 @@ public class Socket
*/
public boolean getOOBInline () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE);
if (buf instanceof Boolean)
@@ -754,9 +713,6 @@ public class Socket
*/
public synchronized void setSoTimeout (int timeout) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
@@ -782,9 +738,6 @@ public class Socket
*/
public synchronized int getSoTimeout () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer)
return(((Integer)timeout).intValue());
@@ -806,9 +759,6 @@ public class Socket
*/
public void setSendBufferSize (int size) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
@@ -828,9 +778,6 @@ public class Socket
*/
public int getSendBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_SNDBUF);
if (buf instanceof Integer)
@@ -853,9 +800,6 @@ public class Socket
*/
public void setReceiveBufferSize (int size) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
if (size <= 0)
throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
@@ -875,9 +819,6 @@ public class Socket
*/
public int getReceiveBufferSize () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_RCVBUF);
if (buf instanceof Integer)
@@ -898,9 +839,6 @@ public class Socket
*/
public void setKeepAlive (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
}
@@ -916,9 +854,6 @@ public class Socket
*/
public boolean getKeepAlive () throws SocketException
{
- if (impl == null)
- throw new SocketException("Not connected");
-
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE);
if (buf instanceof Boolean)
@@ -937,8 +872,8 @@ public class Socket
if (impl != null)
impl.close();
- if (ch != null)
- ch.close();
+ if (getChannel() != null)
+ getChannel().close();
closed = true;
}
@@ -1025,7 +960,7 @@ public class Socket
*/
public SocketChannel getChannel()
{
- return ch;
+ return null;
}
/**
@@ -1037,9 +972,6 @@ public class Socket
*/
public boolean getReuseAddress () throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean))
@@ -1057,9 +989,6 @@ public class Socket
*/
public void setReuseAddress (boolean on) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
}
@@ -1074,9 +1003,6 @@ public class Socket
*/
public int getTrafficClass () throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
Object obj = impl.getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer)
@@ -1099,9 +1025,6 @@ public class Socket
*/
public void setTrafficClass (int tc) throws SocketException
{
- if (impl == null)
- throw new SocketException ("Cannot initialize Socket implementation");
-
if (tc < 0 || tc > 255)
throw new IllegalArgumentException();