Commit edfe1ac7 by Michael Koch Committed by Michael Koch

2003-11-19 Michael Koch <konqueror@gmx.de>

	* java/net/Socket.java
	(implCreated): New variable that indicates created impl.
	(getImpl): New method.
	(toString): Return more SUN compliant string representation.
	(various): Use getImpl() instead of impl.

From-SVN: r73732
parent 90db942b
2003-11-19 Michael Koch <konqueror@gmx.de>
* java/net/Socket.java
(implCreated): New variable that indicates created impl.
(getImpl): New method.
(toString): Return more SUN compliant string representation.
(various): Use getImpl() instead of impl.
2003-11-19 Andreas Tobler <a.tobler@schweiz.ch> 2003-11-19 Andreas Tobler <a.tobler@schweiz.ch>
* lib/libjava.exp: Add DYLD_LIBRARY_PATH for darwin. Look for * lib/libjava.exp: Add DYLD_LIBRARY_PATH for darwin. Look for
......
...@@ -84,6 +84,8 @@ public class Socket ...@@ -84,6 +84,8 @@ public class Socket
*/ */
SocketImpl impl; SocketImpl impl;
private boolean implCreated = false;
private boolean inputShutdown = false; private boolean inputShutdown = false;
private boolean outputShutdown = false; private boolean outputShutdown = false;
...@@ -296,6 +298,25 @@ public class Socket ...@@ -296,6 +298,25 @@ public class Socket
// that default. JDK 1.2 doc infers not to do a bind. // that default. JDK 1.2 doc infers not to do a bind.
} }
private SocketImpl getImpl()
throws SocketException
{
try
{
if (!implCreated)
{
impl.create(true);
implCreated = true;
}
}
catch (IOException e)
{
throw new SocketException(e.getMessage());
}
return impl;
}
/** /**
* Binds the socket to the givent local address/port * Binds the socket to the givent local address/port
* *
...@@ -323,13 +344,10 @@ public class Socket ...@@ -323,13 +344,10 @@ public class Socket
InetSocketAddress tmp = (InetSocketAddress) bindpoint; InetSocketAddress tmp = (InetSocketAddress) bindpoint;
// create socket
impl.create (true);
// bind to address/port // bind to address/port
try try
{ {
impl.bind (tmp.getAddress(), tmp.getPort()); getImpl().bind (tmp.getAddress(), tmp.getPort());
} }
catch (IOException exception) catch (IOException exception)
{ {
...@@ -399,7 +417,7 @@ public class Socket ...@@ -399,7 +417,7 @@ public class Socket
try try
{ {
impl.connect (endpoint, timeout); getImpl().connect (endpoint, timeout);
} }
catch (IOException exception) catch (IOException exception)
{ {
...@@ -426,7 +444,19 @@ public class Socket ...@@ -426,7 +444,19 @@ public class Socket
*/ */
public InetAddress getInetAddress () public InetAddress getInetAddress ()
{ {
return impl.getInetAddress(); if (!isConnected())
return null;
try
{
return getImpl().getInetAddress();
}
catch (SocketException e)
{
// This cannot happen as we are connected.
}
return null;
} }
/** /**
...@@ -440,9 +470,10 @@ public class Socket ...@@ -440,9 +470,10 @@ public class Socket
public InetAddress getLocalAddress () public InetAddress getLocalAddress ()
{ {
InetAddress addr = null; InetAddress addr = null;
try try
{ {
addr = (InetAddress)impl.getOption(SocketOptions.SO_BINDADDR); addr = (InetAddress) getImpl().getOption(SocketOptions.SO_BINDADDR);
} }
catch(SocketException e) catch(SocketException e)
{ {
...@@ -472,8 +503,18 @@ public class Socket ...@@ -472,8 +503,18 @@ public class Socket
*/ */
public int getPort () public int getPort ()
{ {
if (impl != null) if (!isConnected())
return impl.getPort(); return 0;
try
{
if (getImpl() != null)
return getImpl().getPort();
}
catch (SocketException e)
{
// This cannot happen as we are connected.
}
return -1; return -1;
} }
...@@ -486,8 +527,18 @@ public class Socket ...@@ -486,8 +527,18 @@ public class Socket
*/ */
public int getLocalPort () public int getLocalPort ()
{ {
if (impl != null) if (!isBound())
return impl.getLocalPort(); return -1;
try
{
if (getImpl() != null)
return getImpl().getLocalPort();
}
catch (SocketException e)
{
// This cannot happen as we are bound.
}
return -1; return -1;
} }
...@@ -500,12 +551,20 @@ public class Socket ...@@ -500,12 +551,20 @@ public class Socket
*/ */
public SocketAddress getLocalSocketAddress() public SocketAddress getLocalSocketAddress()
{ {
InetAddress addr = getLocalAddress (); if (!isBound())
if (addr == null)
return null; return null;
return new InetSocketAddress (addr, impl.getLocalPort()); InetAddress addr = getLocalAddress ();
try
{
return new InetSocketAddress (addr, getImpl().getLocalPort());
}
catch (SocketException e)
{
// This cannot happen as we are bound.
return null;
}
} }
/** /**
...@@ -519,7 +578,15 @@ public class Socket ...@@ -519,7 +578,15 @@ public class Socket
if (!isConnected ()) if (!isConnected ())
return null; return null;
return new InetSocketAddress (impl.getInetAddress (), impl.getPort ()); try
{
return new InetSocketAddress (getImpl().getInetAddress (), getImpl().getPort ());
}
catch (SocketException e)
{
// This cannot happen as we are connected.
return null;
}
} }
/** /**
...@@ -531,8 +598,8 @@ public class Socket ...@@ -531,8 +598,8 @@ public class Socket
*/ */
public InputStream getInputStream () throws IOException public InputStream getInputStream () throws IOException
{ {
if (impl != null) if (getImpl() != null)
return(impl.getInputStream()); return getImpl().getInputStream();
throw new IOException("Not connected"); throw new IOException("Not connected");
} }
...@@ -546,8 +613,8 @@ public class Socket ...@@ -546,8 +613,8 @@ public class Socket
*/ */
public OutputStream getOutputStream () throws IOException public OutputStream getOutputStream () throws IOException
{ {
if (impl != null) if (getImpl() != null)
return impl.getOutputStream(); return getImpl().getOutputStream();
throw new IOException("Not connected"); throw new IOException("Not connected");
} }
...@@ -563,7 +630,7 @@ public class Socket ...@@ -563,7 +630,7 @@ public class Socket
*/ */
public void setTcpNoDelay (boolean on) throws SocketException public void setTcpNoDelay (boolean on) throws SocketException
{ {
impl.setOption(SocketOptions.TCP_NODELAY, new Boolean(on)); getImpl().setOption(SocketOptions.TCP_NODELAY, new Boolean(on));
} }
/** /**
...@@ -580,7 +647,7 @@ public class Socket ...@@ -580,7 +647,7 @@ public class Socket
*/ */
public boolean getTcpNoDelay() throws SocketException public boolean getTcpNoDelay() throws SocketException
{ {
Object on = impl.getOption(SocketOptions.TCP_NODELAY); Object on = getImpl().getOption(SocketOptions.TCP_NODELAY);
if (on instanceof Boolean) if (on instanceof Boolean)
return(((Boolean)on).booleanValue()); return(((Boolean)on).booleanValue());
...@@ -615,11 +682,11 @@ public class Socket ...@@ -615,11 +682,11 @@ public class Socket
if (linger > 65535) if (linger > 65535)
linger = 65535; linger = 65535;
impl.setOption(SocketOptions.SO_LINGER, new Integer(linger)); getImpl().setOption(SocketOptions.SO_LINGER, new Integer(linger));
} }
else else
{ {
impl.setOption(SocketOptions.SO_LINGER, new Boolean(false)); getImpl().setOption(SocketOptions.SO_LINGER, new Boolean(false));
} }
} }
...@@ -641,7 +708,8 @@ public class Socket ...@@ -641,7 +708,8 @@ public class Socket
*/ */
public int getSoLinger() throws SocketException public int getSoLinger() throws SocketException
{ {
Object linger = impl.getOption(SocketOptions.SO_LINGER); Object linger = getImpl().getOption(SocketOptions.SO_LINGER);
if (linger instanceof Integer) if (linger instanceof Integer)
return(((Integer)linger).intValue()); return(((Integer)linger).intValue());
else else
...@@ -660,7 +728,7 @@ public class Socket ...@@ -660,7 +728,7 @@ public class Socket
*/ */
public void sendUrgentData (int data) throws IOException public void sendUrgentData (int data) throws IOException
{ {
impl.sendUrgentData (data); getImpl().sendUrgentData (data);
} }
/** /**
...@@ -674,7 +742,7 @@ public class Socket ...@@ -674,7 +742,7 @@ public class Socket
*/ */
public void setOOBInline (boolean on) throws SocketException public void setOOBInline (boolean on) throws SocketException
{ {
impl.setOption(SocketOptions.SO_OOBINLINE, new Boolean(on)); getImpl().setOption(SocketOptions.SO_OOBINLINE, new Boolean(on));
} }
/** /**
...@@ -686,7 +754,7 @@ public class Socket ...@@ -686,7 +754,7 @@ public class Socket
*/ */
public boolean getOOBInline () throws SocketException public boolean getOOBInline () throws SocketException
{ {
Object buf = impl.getOption(SocketOptions.SO_OOBINLINE); Object buf = getImpl().getOption(SocketOptions.SO_OOBINLINE);
if (buf instanceof Boolean) if (buf instanceof Boolean)
return(((Boolean)buf).booleanValue()); return(((Boolean)buf).booleanValue());
...@@ -716,7 +784,7 @@ public class Socket ...@@ -716,7 +784,7 @@ public class Socket
if (timeout < 0) if (timeout < 0)
throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0"); throw new IllegalArgumentException("SO_TIMEOUT value must be >= 0");
impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout)); getImpl().setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
} }
/** /**
...@@ -738,7 +806,7 @@ public class Socket ...@@ -738,7 +806,7 @@ public class Socket
*/ */
public synchronized int getSoTimeout () throws SocketException public synchronized int getSoTimeout () throws SocketException
{ {
Object timeout = impl.getOption(SocketOptions.SO_TIMEOUT); Object timeout = getImpl().getOption(SocketOptions.SO_TIMEOUT);
if (timeout instanceof Integer) if (timeout instanceof Integer)
return(((Integer)timeout).intValue()); return(((Integer)timeout).intValue());
else else
...@@ -762,7 +830,7 @@ public class Socket ...@@ -762,7 +830,7 @@ public class Socket
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException("SO_SNDBUF value must be > 0"); throw new IllegalArgumentException("SO_SNDBUF value must be > 0");
impl.setOption(SocketOptions.SO_SNDBUF, new Integer(size)); getImpl().setOption(SocketOptions.SO_SNDBUF, new Integer(size));
} }
/** /**
...@@ -778,7 +846,7 @@ public class Socket ...@@ -778,7 +846,7 @@ public class Socket
*/ */
public int getSendBufferSize () throws SocketException public int getSendBufferSize () throws SocketException
{ {
Object buf = impl.getOption(SocketOptions.SO_SNDBUF); Object buf = getImpl().getOption(SocketOptions.SO_SNDBUF);
if (buf instanceof Integer) if (buf instanceof Integer)
return(((Integer)buf).intValue()); return(((Integer)buf).intValue());
...@@ -803,7 +871,7 @@ public class Socket ...@@ -803,7 +871,7 @@ public class Socket
if (size <= 0) if (size <= 0)
throw new IllegalArgumentException("SO_RCVBUF value must be > 0"); throw new IllegalArgumentException("SO_RCVBUF value must be > 0");
impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size)); getImpl().setOption(SocketOptions.SO_RCVBUF, new Integer(size));
} }
/** /**
...@@ -819,7 +887,7 @@ public class Socket ...@@ -819,7 +887,7 @@ public class Socket
*/ */
public int getReceiveBufferSize () throws SocketException public int getReceiveBufferSize () throws SocketException
{ {
Object buf = impl.getOption(SocketOptions.SO_RCVBUF); Object buf = getImpl().getOption(SocketOptions.SO_RCVBUF);
if (buf instanceof Integer) if (buf instanceof Integer)
return(((Integer)buf).intValue()); return(((Integer)buf).intValue());
...@@ -839,7 +907,7 @@ public class Socket ...@@ -839,7 +907,7 @@ public class Socket
*/ */
public void setKeepAlive (boolean on) throws SocketException public void setKeepAlive (boolean on) throws SocketException
{ {
impl.setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on)); getImpl().setOption(SocketOptions.SO_KEEPALIVE, new Boolean(on));
} }
/** /**
...@@ -854,7 +922,7 @@ public class Socket ...@@ -854,7 +922,7 @@ public class Socket
*/ */
public boolean getKeepAlive () throws SocketException public boolean getKeepAlive () throws SocketException
{ {
Object buf = impl.getOption(SocketOptions.SO_KEEPALIVE); Object buf = getImpl().getOption(SocketOptions.SO_KEEPALIVE);
if (buf instanceof Boolean) if (buf instanceof Boolean)
return(((Boolean)buf).booleanValue()); return(((Boolean)buf).booleanValue());
...@@ -869,8 +937,8 @@ public class Socket ...@@ -869,8 +937,8 @@ public class Socket
*/ */
public synchronized void close () throws IOException public synchronized void close () throws IOException
{ {
if (impl != null) if (getImpl() != null)
impl.close(); getImpl().close();
if (getChannel() != null) if (getChannel() != null)
getChannel().close(); getChannel().close();
...@@ -885,7 +953,19 @@ public class Socket ...@@ -885,7 +953,19 @@ public class Socket
*/ */
public String toString () public String toString ()
{ {
return("Socket " + impl); try
{
if (isConnected())
return ("Socket[addr=" + getImpl().getInetAddress()
+ ",port=" + getImpl().getPort()
+ ",localport=" + getImpl().getLocalPort());
}
catch (SocketException e)
{
// This cannot happen as we are connected.
}
return "Socket[unconnected]";
} }
// Class Methods // Class Methods
...@@ -930,8 +1010,8 @@ public class Socket ...@@ -930,8 +1010,8 @@ public class Socket
*/ */
public void shutdownInput() throws IOException public void shutdownInput() throws IOException
{ {
if (impl != null) if (getImpl() != null)
impl.shutdownInput(); getImpl().shutdownInput();
inputShutdown = true; inputShutdown = true;
} }
...@@ -945,8 +1025,8 @@ public class Socket ...@@ -945,8 +1025,8 @@ public class Socket
*/ */
public void shutdownOutput() throws IOException public void shutdownOutput() throws IOException
{ {
if (impl != null) if (getImpl() != null)
impl.shutdownOutput(); getImpl().shutdownOutput();
outputShutdown = true; outputShutdown = true;
} }
...@@ -972,7 +1052,7 @@ public class Socket ...@@ -972,7 +1052,7 @@ public class Socket
*/ */
public boolean getReuseAddress () throws SocketException public boolean getReuseAddress () throws SocketException
{ {
Object reuseaddr = impl.getOption (SocketOptions.SO_REUSEADDR); Object reuseaddr = getImpl().getOption (SocketOptions.SO_REUSEADDR);
if (!(reuseaddr instanceof Boolean)) if (!(reuseaddr instanceof Boolean))
throw new SocketException ("Internal Error"); throw new SocketException ("Internal Error");
...@@ -989,7 +1069,7 @@ public class Socket ...@@ -989,7 +1069,7 @@ public class Socket
*/ */
public void setReuseAddress (boolean on) throws SocketException public void setReuseAddress (boolean on) throws SocketException
{ {
impl.setOption (SocketOptions.SO_REUSEADDR, new Boolean (on)); getImpl().setOption (SocketOptions.SO_REUSEADDR, new Boolean (on));
} }
/** /**
...@@ -1003,7 +1083,7 @@ public class Socket ...@@ -1003,7 +1083,7 @@ public class Socket
*/ */
public int getTrafficClass () throws SocketException public int getTrafficClass () throws SocketException
{ {
Object obj = impl.getOption(SocketOptions.IP_TOS); Object obj = getImpl().getOption(SocketOptions.IP_TOS);
if (obj instanceof Integer) if (obj instanceof Integer)
return ((Integer) obj).intValue (); return ((Integer) obj).intValue ();
...@@ -1028,7 +1108,7 @@ public class Socket ...@@ -1028,7 +1108,7 @@ public class Socket
if (tc < 0 || tc > 255) if (tc < 0 || tc > 255)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
impl.setOption (SocketOptions.IP_TOS, new Integer (tc)); getImpl().setOption (SocketOptions.IP_TOS, new Integer (tc));
} }
/** /**
...@@ -1038,7 +1118,14 @@ public class Socket ...@@ -1038,7 +1118,14 @@ public class Socket
*/ */
public boolean isConnected () public boolean isConnected ()
{ {
return impl.getInetAddress () != null; try
{
return getImpl().getInetAddress () != null;
}
catch (SocketException e)
{
return false;
}
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment