You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/28 14:05:52 UTC
svn commit: r389483 [1/2] - in /incubator/harmony/enhanced/classlib/trunk:
make/patternsets/ modules/luni/META-INF/ modules/luni/src/main/java/java/net/
modules/luni/src/main/java/org/apache/harmony/luni/net/
Author: tellison
Date: Tue Mar 28 04:05:47 2006
New Revision: 389483
URL: http://svn.apache.org/viewcvs?rev=389483&view=rev
Log:
Fix for HARMONY-216 (Refactor java.net to base on o.a.h.luni.platform)
Added:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/GenericIPMreq.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/GenericIPMreq.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainDatagramSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainMulticastSocketImpl.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainMulticastSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainServerSocketImpl.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainServerSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl2.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl2.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketImplProvider.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketInputStream.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketInputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/SocketOutputStream.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketOutputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/Socks4Message.java
- copied, changed from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socks4Message.java
Removed:
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/GenericIPMreq.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainDatagramSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainMulticastSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainServerSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/PlainSocketImpl2.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketInputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketOutputStream.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socks4Message.java
Modified:
incubator/harmony/enhanced/classlib/trunk/make/patternsets/luni.txt
incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocketImpl.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ServerSocket.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java
incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketImpl.java
Modified: incubator/harmony/enhanced/classlib/trunk/make/patternsets/luni.txt
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/make/patternsets/luni.txt?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/make/patternsets/luni.txt (original)
+++ incubator/harmony/enhanced/classlib/trunk/make/patternsets/luni.txt Tue Mar 28 04:05:47 2006
@@ -37,5 +37,6 @@
org/apache/harmony/luni/internal/locale/*
org/apache/harmony/luni/internal/net/www/*
org/apache/harmony/luni/internal/reflect/*
+org/apache/harmony/luni/net/*
org/apache/harmony/luni/platform/*
org/apache/harmony/luni/util/*
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/META-INF/MANIFEST.MF Tue Mar 28 04:05:47 2006
@@ -22,5 +22,6 @@
java.net,
java.util,
org.apache.harmony.luni.util,
+ org.apache.harmony.luni.net,
org.apache.harmony.luni.platform
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocketImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocketImpl.java?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/DatagramSocketImpl.java Tue Mar 28 04:05:47 2006
@@ -19,6 +19,10 @@
import java.io.FileDescriptor;
import java.io.IOException;
+import org.apache.harmony.luni.net.NetUtil;
+import org.apache.harmony.luni.platform.INetworkSystem;
+import org.apache.harmony.luni.platform.Platform;
+
/**
* The abstract superclass of datagram & multicast socket implementatations.
*/
@@ -79,8 +83,9 @@
*
* @return InetAddress the local address to which the socket is bound.
*/
- abstract InetAddress getLocalAddress();
-
+ InetAddress getLocalAddress() {
+ return Platform.getNetworkSystem().getSocketLocalAddress(fd, NetUtil.preferIPv6Addresses());
+ }
/**
* Answer the local port. If the socket was bound to any available port, as
* flagged by a <code>localPort</code> value of -1, query the IP stack.
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ServerSocket.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ServerSocket.java?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ServerSocket.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/ServerSocket.java Tue Mar 28 04:05:47 2006
@@ -15,9 +15,11 @@
package java.net;
-
import java.io.IOException;
+import org.apache.harmony.luni.net.SocketImplProvider;
+
+
import com.ibm.oti.util.Msg;
/**
@@ -30,452 +32,472 @@
*/
public class ServerSocket {
- SocketImpl impl;
+ SocketImpl impl;
- static SocketImplFactory factory;
+ static SocketImplFactory factory;
- private volatile boolean isCreated = false;
+ private volatile boolean isCreated = false;
- private boolean isBound = false;
+ private boolean isBound = false;
- private boolean isClosed = false;
-
- /**
- * Construct a ServerSocket, which is not bound to any port. The default
- * number of pending connections may be backlogged.
- *
- * @see Socket
- */
- public ServerSocket() throws IOException {
- impl = factory != null ? factory.createSocketImpl()
- : new PlainServerSocketImpl();
- }
-
- /**
- * Construct a ServerSocket, bound to the nominated port on the default
- * localhost. The default number of pending connections may be backlogged.
- *
- * @param aport
- * the port number to listen for connection requests on
- * @see Socket
- */
- public ServerSocket(int aport) throws IOException {
- this(aport, defaultBacklog(), InetAddress.ANY);
- }
-
- /**
- * Construct a ServerSocket, bound to the nominated port on the default
- * localhost. The number of pending connections that may be backlogged is a
- * specified.
- *
- * @param aport
- * the port number to listen for connection requests on
- * @param backlog
- * the number of pending connection requests, before requests are
- * rejected
- * @see Socket
- */
- public ServerSocket(int aport, int backlog) throws IOException {
- this(aport, backlog, InetAddress.ANY);
- }
-
- /**
- * Construct a ServerSocket, bound to the nominated local host/port. The
- * number of pending connections that may be backlogged is a specified.
- *
- * @param aport
- * the port number to listen for connection requests on
- * @param localAddr
- * the local machine address to bind on
- * @param backlog
- * the number of pending connection requests, before requests are
- * rejected
- * @see Socket
- */
- public ServerSocket(int aport, int backlog, InetAddress localAddr)
- throws IOException {
- super();
- checkListen(aport);
- impl = factory != null ? factory.createSocketImpl()
- : new PlainServerSocketImpl();
- InetAddress addr = localAddr == null ? InetAddress.ANY : localAddr;
-
- synchronized (this) {
- impl.create(true);
- isCreated = true;
- try {
- impl.bind(addr, aport);
- isBound = true;
- impl.listen(backlog > 0 ? backlog : defaultBacklog());
- } catch (IOException e) {
- close();
- throw e;
- }
- }
- }
-
- /**
- * Retrieve the first connection request and answer the 'host' socket that
- * will conduct further communications with the requesting 'client' socket.
- *
- * @return Socket the 'host' socket
- * @exception IOException
- * if an error occurs while instantiating the 'host' socket
- */
- public Socket accept() throws IOException {
- checkClosedAndCreate(false);
- if (!isBound())
- throw new SocketException(Msg.getString("K031f"));
-
- // If a SOCKS proxy is being used, accept does strange things.
- // Instead of returning a new Socket and allowing this ServerSocket
- // to be used for another accept, it actually uses the current
- // ServerSocket
- // as the accepted Socket. So, closing the returned socket will close
- // the
- // ServerSocket as well. The ServerSocket cannot be used for a second
- // accept.
- if (PlainSocketImpl.usingSocks()) {
- if (impl instanceof PlainSocketImpl) {
- try {
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkAccept(getInetAddress().getHostName(),
- getLocalPort());
- }
- } catch (SecurityException e) {
- close();
- throw e;
- }
-
- ((PlainSocketImpl) impl).socksAccept();
- return new Socket(impl);
- }
-
- throw new IOException(Msg.getString("K0041")); //$NON-NLS-1$
- }
-
- Socket aSocket = new Socket();
- try {
- synchronized (this) {
- implAccept(aSocket);
- }
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkAccept(aSocket.getInetAddress().getHostName(),
- aSocket.getPort());
- }
- } catch (SecurityException e) {
- aSocket.close();
- throw e;
- } catch (IOException e) {
- aSocket.close();
- throw e;
- }
- return aSocket;
- }
-
- /**
- * Check whether the server may listen for connection requests on
- * <code>aport</code>. Throw an exception if the port is outside the
- * valid range or does not satisfy the security policy.
- *
- * @param aPort
- * the candidate port to listen on
- */
- void checkListen(int aPort) {
- if (aPort < 0 || aPort > 65535)
- throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkListen(aPort);
- }
-
- /**
- * Close this server socket. Any attempt to connect to this socket
- * thereafter will fail.
- */
- public void close() throws IOException {
- isClosed = true;
- impl.close();
- }
-
- /**
- * Answer the default number of pending connections on a server socket.
- *
- * @return int the default number of pending connection requests
- */
- static int defaultBacklog() {
- return 50;
- }
-
- /**
- * Answer the local IP address for this server socket. Return null if the
- * socket is not bound. This is useful on multihomed hosts.
- *
- * @return InetAddress the local address
- */
- public InetAddress getInetAddress() {
- if (!isBound())
- return null;
- return impl.getInetAddress();
- }
-
- /**
- * Answer the local port for this server socket. Return -1 if the socket is
- * not bound.
- *
- * @return int the local port the server is listening on
- */
- public int getLocalPort() {
- if (!isBound())
- return -1;
- return impl.getLocalPort();
- }
-
- /**
- * Answer the time-out period of this server socket. This is the time the
- * server will wait listening for connections, before exiting.
- *
- * @return int the listening timeout
- * @exception SocketException
- * thrown if option cannot be retrieved
- */
- public synchronized int getSoTimeout() throws IOException {
- checkClosedAndCreate(true);
- return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
- }
-
- /**
- * Invoke the server socket implementation to accept a connection on the
- * newly created <code>aSocket</code>.
- *
- * @param aSocket
- * the concrete socketImpl to accept the connection request on
- * @exception IOException
- * thrown if connection cannot be accepted
- */
- protected final void implAccept(Socket aSocket) throws IOException {
- impl.accept(aSocket.impl);
- aSocket.accepted();
- }
-
- /**
- * Set the server socket implementation factory. This method may only be
- * invoked with sufficient security and only once during the application
- * lifetime.
- *
- * @param aFactory
- * the streaming socket factory to be used for further socket
- * instantiations
- * @exception IOException
- * thrown if the factory is already set
- */
- public static synchronized void setSocketFactory(SocketImplFactory aFactory)
- throws IOException {
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkSetFactory();
- if (factory == null)
- factory = aFactory;
- else
- throw new SocketException(Msg.getString("K0042")); //$NON-NLS-1$
- }
-
- /**
- * Set the listen time-out period for this server socket.
- *
- * @param timeout
- * the time to wait for a connection request
- * @exception SocketException
- * thrown if an error occurs during setting the option
- */
- public synchronized void setSoTimeout(int timeout) throws SocketException {
- checkClosedAndCreate(true);
- if (timeout >= 0) {
- impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
- } else {
- throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
- }
- }
-
- /**
- * Answers a string containing a concise, human-readable description of the
- * server socket. The <code>port</code> field is reported a zero, as there
- * is no connection formed to the server.
- *
- * @return String the description
- */
- public String toString() {
- StringBuffer result = new StringBuffer(64);
- result.append("ServerSocket["); //$NON-NLS-1$
- if (!isBound())
- return result.append("unbound]").toString(); //$NON-NLS-1$
- return result.append("addr="). //$NON-NLS-1$
- append(getInetAddress()).append(",port=0,localport="). //$NON-NLS-1$
- append(getLocalPort()).append("]"). //$NON-NLS-1$
- toString();
- }
-
- /**
- * Bind the ServerSocket to the nominated local host/port. The default
- * number of pending connections may be backlogged.
- *
- * @param localAddr
- * the local machine address and port to bind on
- *
- * @exception IllegalArgumentException
- * if the SocketAddress is not supported
- * @exception IOException
- * if the socket is already bound, or a problem occurs during
- * the bind
- */
- public void bind(SocketAddress localAddr) throws IOException {
- bind(localAddr, defaultBacklog());
- }
-
- /**
- * Bind the ServerSocket to the nominated local host/port. The number of
- * pending connections that may be backlogged is a specified.
- *
- * @param localAddr
- * the local machine address and port to bind on
- * @param backlog
- * the number of pending connection requests, before requests are
- * rejected
- *
- * @exception IllegalArgumentException
- * if the SocketAddress is not supported
- * @exception IOException
- * if the socket is already bound, or a problem occurs during
- * the bind
- */
- public void bind(SocketAddress localAddr, int backlog) throws IOException {
- checkClosedAndCreate(true);
- if (isBound())
- throw new BindException(Msg.getString("K0315")); //$NON-NLS-1$
- int port = 0;
- InetAddress addr = InetAddress.ANY;
- if (localAddr != null) {
- if (!(localAddr instanceof InetSocketAddress))
- throw new IllegalArgumentException(Msg.getString(
- "K0316", localAddr.getClass())); //$NON-NLS-1$
- InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
- if ((addr = inetAddr.getAddress()) == null)
- throw new SocketException(Msg.getString(
- "K0317", inetAddr.getHostName())); //$NON-NLS-1$
- port = inetAddr.getPort();
- }
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkListen(port);
-
- synchronized (this) {
- try {
- impl.bind(addr, port);
- isBound = true;
- impl.listen(backlog > 0 ? backlog : defaultBacklog());
- } catch (IOException e) {
- close();
- throw e;
- }
- }
- }
-
- /**
- * Answer the local SocketAddress for this server socket, or null if the
- * socket is not bound. This is useful on multihomed hosts.
- */
- public SocketAddress getLocalSocketAddress() {
- if (!isBound())
- return null;
- return new InetSocketAddress(getInetAddress(), getLocalPort());
- }
-
- /**
- * Return if the server socket is bound to a local address and port.
- */
- public boolean isBound() {
- return isBound;
- }
-
- /**
- * Return if the server socket is closed.
- */
- public boolean isClosed() {
- return isClosed;
- }
-
- /**
- * Check if the socket is closed, and throw an exception.
- */
- private void checkClosedAndCreate(boolean create) throws SocketException {
- if (isClosed())
- throw new SocketException(Msg.getString("K003d"));
-
- if (!create || isCreated)
- return;
-
- synchronized (this) {
- if (isCreated)
- return;
- try {
- impl.create(true);
- } catch (SocketException e) {
- throw e;
- } catch (IOException e) {
- throw new SocketException(e.toString());
- }
- isCreated = true;
- }
- }
-
- /**
- * Set the SO_REUSEADDR socket option.
- *
- * @param reuse
- * the socket SO_REUSEADDR option setting
- */
- public void setReuseAddress(boolean reuse) throws SocketException {
- checkClosedAndCreate(true);
- impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
- : Boolean.FALSE);
- }
-
- /**
- * Get the state of the SO_REUSEADDR socket option.
- */
- public boolean getReuseAddress() throws SocketException {
- checkClosedAndCreate(true);
- return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
- .booleanValue();
- }
-
- /**
- * Set the socket receive buffer size.
- *
- * @param size
- * the buffer size, in bytes
- *
- * @exception java.net.SocketException
- * If an error occurs while setting the size or the size is
- * invalid.
- */
- public void setReceiveBufferSize(int size) throws SocketException {
- checkClosedAndCreate(true);
- if (size >= 1)
- impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
- else
- throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
- }
-
- /**
- * Answer the socket receive buffer size (SO_RCVBUF).
- *
- * @return int socket receive buffer size
- */
- public int getReceiveBufferSize() throws SocketException {
- checkClosedAndCreate(true);
- return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
- }
+ private boolean isClosed = false;
+
+ /**
+ * Construct a ServerSocket, which is not bound to any port. The default
+ * number of pending connections may be backlogged.
+ *
+ * @see Socket
+ */
+ public ServerSocket() throws IOException {
+ impl = factory != null ? factory.createSocketImpl()
+ :SocketImplProvider.getServerSocketImpl();
+ }
+
+ protected ServerSocket(SocketImpl impl){
+ this.impl = impl;
+ }
+
+ /**
+ * Construct a ServerSocket, bound to the nominated port on the default
+ * localhost. The default number of pending connections may be backlogged.
+ *
+ * @param aport
+ * the port number to listen for connection requests on
+ * @see Socket
+ */
+ public ServerSocket(int aport) throws IOException {
+ this(aport, defaultBacklog(), InetAddress.ANY);
+ }
+
+ /**
+ * Construct a ServerSocket, bound to the nominated port on the default
+ * localhost. The number of pending connections that may be backlogged is a
+ * specified.
+ *
+ * @param aport
+ * the port number to listen for connection requests on
+ * @param backlog
+ * the number of pending connection requests, before requests are
+ * rejected
+ * @see Socket
+ */
+ public ServerSocket(int aport, int backlog) throws IOException {
+ this(aport, backlog, InetAddress.ANY);
+ }
+
+ /**
+ * Construct a ServerSocket, bound to the nominated local host/port. The
+ * number of pending connections that may be backlogged is a specified.
+ *
+ * @param aport
+ * the port number to listen for connection requests on
+ * @param localAddr
+ * the local machine address to bind on
+ * @param backlog
+ * the number of pending connection requests, before requests are
+ * rejected
+ * @see Socket
+ */
+ public ServerSocket(int aport, int backlog, InetAddress localAddr)
+ throws IOException {
+ super();
+ checkListen(aport);
+ impl = factory != null ? factory.createSocketImpl()
+ : SocketImplProvider.getServerSocketImpl();
+ InetAddress addr = localAddr == null ? InetAddress.ANY : localAddr;
+
+ synchronized (this) {
+ impl.create(true);
+ isCreated = true;
+ try {
+ impl.bind(addr, aport);
+ isBound = true;
+ impl.listen(backlog > 0 ? backlog : defaultBacklog());
+ } catch (IOException e) {
+ close();
+ throw e;
+ }
+ }
+ }
+
+ /**
+ * Retrieve the first connection request and answer the 'host' socket that
+ * will conduct further communications with the requesting 'client' socket.
+ *
+ * @return Socket the 'host' socket
+ * @exception IOException
+ * if an error occurs while instantiating the 'host' socket
+ */
+ public Socket accept() throws IOException {
+ checkClosedAndCreate(false);
+ if (!isBound())
+ throw new SocketException(Msg.getString("K031f"));
+
+ // If a SOCKS proxy is being used, accept does strange things.
+ // Instead of returning a new Socket and allowing this ServerSocket
+ // to be used for another accept, it actually uses the current
+ // ServerSocket
+ // as the accepted Socket. So, closing the returned socket will close
+ // the
+ // ServerSocket as well. The ServerSocket cannot be used for a second
+ // accept.
+ //FIXME: paulex comment this for compile
+// if (IOUtil.usingSocks()) {
+// if (impl instanceof PlainSocketImpl) {
+// try {
+// SecurityManager security = System.getSecurityManager();
+// if (security != null) {
+// security.checkAccept(getInetAddress().getHostAddress(),
+// getLocalPort());
+// }
+// } catch (SecurityException e) {
+// close();
+// throw e;
+// }
+//
+// ((PlainSocketImpl) impl).socksAccept();
+// return new Socket(impl);
+// }
+//
+// throw new IOException(Msg.getString("K0041")); //$NON-NLS-1$
+// }
+
+ Socket aSocket = new Socket();
+ try {
+ synchronized (this) {
+ implAccept(aSocket);
+ }
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
+ security.checkAccept(aSocket.getInetAddress().getHostAddress(),
+ aSocket.getPort());
+ }
+ } catch (SecurityException e) {
+ aSocket.close();
+ throw e;
+ } catch (IOException e) {
+ aSocket.close();
+ throw e;
+ }
+ return aSocket;
+ }
+
+ /**
+ * Check whether the server may listen for connection requests on
+ * <code>aport</code>. Throw an exception if the port is outside the
+ * valid range or does not satisfy the security policy.
+ *
+ * @param aPort
+ * the candidate port to listen on
+ */
+ void checkListen(int aPort) {
+ if (aPort < 0 || aPort > 65535)
+ throw new IllegalArgumentException(Msg.getString("K0325", aPort)); //$NON-NLS-1$
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkListen(aPort);
+ }
+
+ /**
+ * Close this server socket. Any attempt to connect to this socket
+ * thereafter will fail.
+ */
+ public void close() throws IOException {
+ isClosed = true;
+ impl.close();
+ }
+
+ /**
+ * Answer the default number of pending connections on a server socket.
+ *
+ * @return int the default number of pending connection requests
+ */
+ static int defaultBacklog() {
+ return 50;
+ }
+
+ /**
+ * Answer the local IP address for this server socket. Return null if the
+ * socket is not bound. This is useful on multihomed hosts.
+ *
+ * @return InetAddress the local address
+ */
+ public InetAddress getInetAddress() {
+ if (!isBound())
+ return null;
+ return impl.getInetAddress();
+ }
+
+ /**
+ * Answer the local port for this server socket. Return -1 if the socket is
+ * not bound.
+ *
+ * @return int the local port the server is listening on
+ */
+ public int getLocalPort() {
+ if (!isBound())
+ return -1;
+ return impl.getLocalPort();
+ }
+
+ /**
+ * Answer the time-out period of this server socket. This is the time the
+ * server will wait listening for connections, before exiting.
+ *
+ * @return int the listening timeout
+ * @exception SocketException
+ * thrown if option cannot be retrieved
+ */
+ public synchronized int getSoTimeout() throws IOException {
+ // TODO Bug-Reporter:check changed! checkClosedAndCreate(true);
+ if (!isCreated) {
+ synchronized (this) {
+ if (!isCreated) {
+
+ try {
+ impl.create(true);
+ } catch (SocketException e) {
+ throw e;
+ } catch (IOException e) {
+ throw new SocketException(e.toString());
+ }
+ isCreated = true;
+ }
+ }
+ }
+ return ((Integer) impl.getOption(SocketOptions.SO_TIMEOUT)).intValue();
+ }
+
+ /**
+ * Invoke the server socket implementation to accept a connection on the
+ * newly created <code>aSocket</code>.
+ *
+ * @param aSocket
+ * the concrete socketImpl to accept the connection request on
+ * @exception IOException
+ * thrown if connection cannot be accepted
+ */
+ protected final void implAccept(Socket aSocket) throws IOException {
+ impl.accept(aSocket.impl);
+ aSocket.accepted();
+ }
+
+ /**
+ * Set the server socket implementation factory. This method may only be
+ * invoked with sufficient security and only once during the application
+ * lifetime.
+ *
+ * @param aFactory
+ * the streaming socket factory to be used for further socket
+ * instantiations
+ * @exception IOException
+ * thrown if the factory is already set
+ */
+ public static synchronized void setSocketFactory(SocketImplFactory aFactory)
+ throws IOException {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkSetFactory();
+ if (factory == null)
+ factory = aFactory;
+ else
+ throw new SocketException(Msg.getString("K0042")); //$NON-NLS-1$
+ }
+
+ /**
+ * Set the listen time-out period for this server socket.
+ *
+ * @param timeout
+ * the time to wait for a connection request
+ * @exception SocketException
+ * thrown if an error occurs during setting the option
+ */
+ public synchronized void setSoTimeout(int timeout) throws SocketException {
+ checkClosedAndCreate(true);
+ if (timeout >= 0) {
+ impl.setOption(SocketOptions.SO_TIMEOUT, new Integer(timeout));
+ } else {
+ throw new IllegalArgumentException(Msg.getString("K0036")); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Answers a string containing a concise, human-readable description of the
+ * server socket. The <code>port</code> field is reported a zero, as there
+ * is no connection formed to the server.
+ *
+ * @return String the description
+ */
+ public String toString() {
+ StringBuffer result = new StringBuffer(64);
+ result.append("ServerSocket["); //$NON-NLS-1$
+ if (!isBound())
+ return result.append("unbound]").toString(); //$NON-NLS-1$
+ return result.append("addr="). //$NON-NLS-1$
+ append(getInetAddress()).append(",port=0,localport="). //$NON-NLS-1$
+ append(getLocalPort()).append("]"). //$NON-NLS-1$
+ toString();
+ }
+
+ /**
+ * Bind the ServerSocket to the nominated local host/port. The default
+ * number of pending connections may be backlogged.
+ *
+ * @param localAddr
+ * the local machine address and port to bind on
+ *
+ * @exception IllegalArgumentException
+ * if the SocketAddress is not supported
+ * @exception IOException
+ * if the socket is already bound, or a problem occurs during
+ * the bind
+ */
+ public void bind(SocketAddress localAddr) throws IOException {
+ bind(localAddr, defaultBacklog());
+ }
+
+ /**
+ * Bind the ServerSocket to the nominated local host/port. The number of
+ * pending connections that may be backlogged is a specified.
+ *
+ * @param localAddr
+ * the local machine address and port to bind on
+ * @param backlog
+ * the number of pending connection requests, before requests are
+ * rejected
+ *
+ * @exception IllegalArgumentException
+ * if the SocketAddress is not supported
+ * @exception IOException
+ * if the socket is already bound, or a problem occurs during
+ * the bind
+ */
+ public void bind(SocketAddress localAddr, int backlog) throws IOException {
+ checkClosedAndCreate(true);
+ if (isBound())
+ throw new BindException(Msg.getString("K0315")); //$NON-NLS-1$
+ int port = 0;
+ InetAddress addr = InetAddress.ANY;
+ if (localAddr != null) {
+ if (!(localAddr instanceof InetSocketAddress))
+ throw new IllegalArgumentException(Msg.getString(
+ "K0316", localAddr.getClass())); //$NON-NLS-1$
+ InetSocketAddress inetAddr = (InetSocketAddress) localAddr;
+ if ((addr = inetAddr.getAddress()) == null)
+ throw new SocketException(Msg.getString(
+ "K0317", inetAddr.getHostName())); //$NON-NLS-1$
+ port = inetAddr.getPort();
+ }
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkListen(port);
+
+ synchronized (this) {
+ try {
+ impl.bind(addr, port);
+ isBound = true;
+ impl.listen(backlog > 0 ? backlog : defaultBacklog());
+ } catch (IOException e) {
+ close();
+ throw e;
+ }
+ }
+ }
+
+ /**
+ * Answer the local SocketAddress for this server socket, or null if the
+ * socket is not bound. This is useful on multihomed hosts.
+ */
+ public SocketAddress getLocalSocketAddress() {
+ if (!isBound())
+ return null;
+ return new InetSocketAddress(getInetAddress(), getLocalPort());
+ }
+
+ /**
+ * Return if the server socket is bound to a local address and port.
+ */
+ public boolean isBound() {
+ return isBound;
+ }
+
+ /**
+ * Return if the server socket is closed.
+ */
+ public boolean isClosed() {
+ return isClosed;
+ }
+
+ /**
+ * Check if the socket is closed, and throw an exception.
+ */
+ private void checkClosedAndCreate(boolean create) throws SocketException {
+ if (isClosed())
+ throw new SocketException(Msg.getString("K003d"));
+
+ if (!create || isCreated)
+ return;
+
+ synchronized (this) {
+ if (isCreated)
+ return;
+ try {
+ impl.create(true);
+ } catch (SocketException e) {
+ throw e;
+ } catch (IOException e) {
+ throw new SocketException(e.toString());
+ }
+ isCreated = true;
+ }
+ }
+
+ /**
+ * Set the SO_REUSEADDR socket option.
+ *
+ * @param reuse
+ * the socket SO_REUSEADDR option setting
+ */
+ public void setReuseAddress(boolean reuse) throws SocketException {
+ checkClosedAndCreate(true);
+ impl.setOption(SocketOptions.SO_REUSEADDR, reuse ? Boolean.TRUE
+ : Boolean.FALSE);
+ }
+
+ /**
+ * Get the state of the SO_REUSEADDR socket option.
+ */
+ public boolean getReuseAddress() throws SocketException {
+ checkClosedAndCreate(true);
+ return ((Boolean) impl.getOption(SocketOptions.SO_REUSEADDR))
+ .booleanValue();
+ }
+
+ /**
+ * Set the socket receive buffer size.
+ *
+ * @param size
+ * the buffer size, in bytes
+ *
+ * @exception java.net.SocketException
+ * If an error occurs while setting the size or the size is
+ * invalid.
+ */
+ public void setReceiveBufferSize(int size) throws SocketException {
+ checkClosedAndCreate(true);
+ if (size >= 1)
+ impl.setOption(SocketOptions.SO_RCVBUF, new Integer(size));
+ else
+ throw new IllegalArgumentException(Msg.getString("K0035")); //$NON-NLS-1$
+ }
+
+ /**
+ * Answer the socket receive buffer size (SO_RCVBUF).
+ *
+ * @return int socket receive buffer size
+ */
+ public int getReceiveBufferSize() throws SocketException {
+ checkClosedAndCreate(true);
+ return ((Integer) impl.getOption(SocketOptions.SO_RCVBUF)).intValue();
+ }
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/Socket.java Tue Mar 28 04:05:47 2006
@@ -1,27 +1,17 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* $$COPYRIGHT$$[1998, 2005]$$ */
package java.net;
-import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.AccessController;
+import org.apache.harmony.luni.net.NetUtil;
+import org.apache.harmony.luni.net.SocketImplProvider;
+import org.apache.harmony.luni.platform.Platform;
+
import com.ibm.oti.util.Msg;
import com.ibm.oti.util.PriviAction;
@@ -57,13 +47,13 @@
static final int FLAG_SHUTDOWN = 8;
- // Fill in the JNI id caches
-
- private static native void oneTimeInitialization(boolean jcl_supports_ipv6);
-
- static {
- oneTimeInitialization(true);
- }
+// This is used in cache get/set
+//
+// private static native void oneTimeInitialization(boolean jcl_supports_ipv6);
+//
+// static {
+// oneTimeInitialization(true);
+// }
/**
* Construct a connection-oriented Socket. The Socket is created in the
@@ -73,7 +63,7 @@
*/
public Socket() {
impl = factory != null ? factory.createSocketImpl()
- : new PlainSocketImpl2();
+ : SocketImplProvider.getSocketImpl();
}
/**
@@ -284,7 +274,7 @@
* @throws IOException
* if an error occurs creating the stream
*
- * @see java.net.SocketInputStream
+ * @see org.apache.harmony.luni.net.SocketInputStream
*/
public InputStream getInputStream() throws IOException {
checkClosedAndCreate(false);
@@ -316,7 +306,7 @@
public InetAddress getLocalAddress() {
if (!isBound())
return InetAddress.ANY;
- return Socket.getSocketLocalAddressImpl(impl.fd, InetAddress
+ return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd, InetAddress
.preferIPv6Addresses());
}
@@ -340,7 +330,7 @@
* @throws IOException
* if an error occurs creating the stream
*
- * @see java.net.SocketOutputStream
+ * @see org.apache.harmony.luni.net.SocketOutputStream
*/
public OutputStream getOutputStream() throws IOException {
checkClosedAndCreate(false);
@@ -430,51 +420,51 @@
.booleanValue();
}
- static native InetAddress getSocketLocalAddressImpl(FileDescriptor aFD,
- boolean preferIPv6Addresses);
-
- /**
- * Query the IP stack for the local port to which this socket is bound.
- *
- * @param aFD
- * the socket descriptor
- * @param preferIPv6Addresses
- * address preference for nodes that support both IPv4 and IPv6
- * @return int the local port to which the socket is bound
- */
- static native int getSocketLocalPortImpl(FileDescriptor aFD,
- boolean preferIPv6Addresses);
-
- /**
- * Query the IP stack for the nominated socket option.
- *
- * @param aFD
- * the socket descriptor
- * @param opt
- * the socket option type
- * @return the nominated socket option value
- *
- * @throws SocketException
- * if the option is invalid
- */
- static native Object getSocketOptionImpl(FileDescriptor aFD, int opt)
- throws SocketException;
-
- /**
- * Set the nominated socket option in the IP stack.
- *
- * @param aFD
- * the socket descriptor
- * @param opt
- * the option selector
- * @param optVal
- * the nominated option value
- *
- * @throws SocketException
- * if the option is invalid or cannot be set
- */
- static native void setSocketOptionImpl(FileDescriptor aFD, int opt,
- Object optVal) throws SocketException;
+// static native InetAddress getSocketLocalAddressImpl(FileDescriptor aFD,
+// boolean preferIPv6Addresses);
+//
+// /**
+// * Query the IP stack for the local port to which this socket is bound.
+// *
+// * @param aFD
+// * the socket descriptor
+// * @param preferIPv6Addresses
+// * address preference for nodes that support both IPv4 and IPv6
+// * @return int the local port to which the socket is bound
+// */
+// static native int getSocketLocalPortImpl(FileDescriptor aFD,
+// boolean preferIPv6Addresses);
+//
+// /**
+// * Query the IP stack for the nominated socket option.
+// *
+// * @param aFD
+// * the socket descriptor
+// * @param opt
+// * the socket option type
+// * @return the nominated socket option value
+// *
+// * @throws SocketException
+// * if the option is invalid
+// */
+// static native Object getSocketOptionImpl(FileDescriptor aFD, int opt)
+// throws SocketException;
+//
+// /**
+// * Set the nominated socket option in the IP stack.
+// *
+// * @param aFD
+// * the socket descriptor
+// * @param opt
+// * the option selector
+// * @param optVal
+// * the nominated option value
+// *
+// * @throws SocketException
+// * if the option is invalid or cannot be set
+// */
+// static native void setSocketOptionImpl(FileDescriptor aFD, int opt,
+// Object optVal) throws SocketException;
/**
* Set the SO_KEEPALIVE option for this socket.
@@ -491,15 +481,15 @@
: Boolean.FALSE);
}
- static native int getSocketFlags();
-
- /**
- * Close the socket in the IP stack.
- *
- * @param aFD
- * the socket descriptor
- */
- static native void socketCloseImpl(FileDescriptor aFD);
+// static native int getSocketFlags();
+//
+// /**
+// * Close the socket in the IP stack.
+// *
+// * @param aFD
+// * the socket descriptor
+// */
+// static native void socketCloseImpl(FileDescriptor aFD);
/**
* Specifies the application's socket implementation factory. This may only
@@ -642,7 +632,7 @@
impl.create(streaming);
isCreated = true;
try {
- if (!streaming || !PlainSocketImpl.usingSocks())
+ if (!streaming || !NetUtil.usingSocks())
impl.bind(addr, localPort);
isBound = true;
impl.connect(dstAddress, dstPort);
@@ -708,6 +698,9 @@
if (!isConnected())
throw new SocketException(Msg.getString("K0320"));
// a connected socket must be created
+
+ /* return directly to fix a possible bug, if !create, should return here*/
+ return;
}
if (isCreated)
return;
@@ -813,7 +806,7 @@
synchronized (this) {
try {
- if (!PlainSocketImpl.usingSocks())
+ if (!NetUtil.usingSocks())
impl.bind(addr, port);
isBound = true;
} catch (IOException e) {
@@ -884,7 +877,7 @@
// checkClosedAndCreate
// this caused us to lose socket options on create
// impl.create(true);
- if (!PlainSocketImpl.usingSocks())
+ if (!NetUtil.usingSocks())
impl.bind(InetAddress.ANY, 0);
isBound = true;
}
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketImpl.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketImpl.java?rev=389483&r1=389482&r2=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketImpl.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/SocketImpl.java Tue Mar 28 04:05:47 2006
@@ -1,17 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/* $$COPYRIGHT$$[1998, 2005]$$ */
package java.net;
@@ -22,6 +9,10 @@
import java.io.InterruptedIOException;
import java.io.OutputStream;
+import org.apache.harmony.luni.platform.INetworkSystem;
+import org.apache.harmony.luni.platform.Platform;
+
+
/**
* The abstract superclass of all classes that implement streaming sockets.
*
@@ -45,19 +36,23 @@
protected FileDescriptor fd;
protected int localport;
-
- int receiveTimeout;
-
- boolean streaming = true;
-
- boolean shutdownInput = false;
-
- // Fill in the JNI id caches
- private static native void oneTimeInitialization(boolean jcl_supports_ipv6);
-
- static {
- oneTimeInitialization(true);
- }
+
+ INetworkSystem netImpl;
+
+ int receiveTimeout = 0;
+
+ boolean streaming = true;
+
+ boolean shutdownInput = false;
+
+// Used when cache mode is OK
+// Fill in the JNI id caches
+// private static native void oneTimeInitialization(boolean jcl_supports_ipv6);
+//
+// static {
+// //OSNetworkSystem.oneTimeInitializationSocket(true);
+// oneTimeInitialization(true);
+// }
/**
* Construct a connection-oriented SocketImpl.
@@ -66,6 +61,7 @@
*/
public SocketImpl() {
initializeSocket();
+ this.netImpl = Platform.getNetworkSystem();
}
/**
@@ -223,7 +219,6 @@
*/
void initializeSocket() {
fd = new FileDescriptor();
- receiveTimeout = 0;
}
/**
@@ -239,115 +234,116 @@
protected abstract void listen(int backlog) throws IOException;
- /**
- * Answer the result of attempting to accept a connection request to this
- * stream socket in the IP stack.
- *
- * @param fdServer
- * the server socket FileDescriptor
- * @param newSocket
- * the host socket a connection will be accepted on
- * @param fdnewSocket
- * the FileDescriptor for the host socket
- * @param timeout
- * the timeout that the server should listen for
- * @exception SocketException
- * if an error occurs while accepting connections
- */
- static native void acceptStreamSocketImpl(FileDescriptor fdServer,
- SocketImpl newSocket, FileDescriptor fdnewSocket, int timeout)
- throws IOException;
-
- /**
- * Answer the number of bytes available to read on the socket without
- * blocking.
- *
- * @param aFD
- * the socket FileDescriptor
- * @exception SocketException
- * if an error occurs while peeking
- */
- static native int availableStreamImpl(FileDescriptor aFD)
- throws SocketException;
-
- /**
- * Answer the result of attempting to create a stream socket in the IP
- * stack.
- *
- * @param aFD
- * the socket FileDescriptor
- * @exception SocketException
- * if an error occurs while creating the socket
- */
- static native void createStreamSocketImpl(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
- /**
- * Allocate a datagram socket in the IP stack. The socket is associated with
- * the <code>aFD</code>.
- *
- * @param aFD
- * the FileDescriptor to associate with the socket
- *
- * @exception SocketException
- * upon an allocation error
- */
-
- static native void createDatagramSocketImpl(FileDescriptor aFD,
- boolean preferIPv4Stack) throws SocketException;
-
- /**
- * Answer the result of attempting to listen on a stream socket in the IP
- * stack.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param backlog
- * the number of connection requests that may be queued before
- * requests are rejected
- * @exception SocketException
- * if an error occurs while listening
- */
- static native void listenStreamSocketImpl(FileDescriptor aFD, int backlog)
- throws SocketException;
-
- /**
- * Recieve at most <code>count</code> bytes into the buffer
- * <code>data</code> at the <code>offset</code> on the socket.
- *
- * @param aFD
- * the socket FileDescriptor
- * @param data
- * the receive buffer
- * @param offset
- * the offset into the buffer
- * @param count
- * the max number of bytes to receive
- * @param timeout
- * the max time the read operation should block waiting for data
- * @return int the actual number of bytes read
- * @exception SocketException
- * if an error occurs while reading
- */
- static native int receiveStreamImpl(FileDescriptor aFD, byte[] data,
- int offset, int count, int timeout) throws IOException;
-
- /**
- * Send <code>count</code> bytes from the buffer <code>data</code> at
- * the <code>offset</code>, on the socket.
- *
- * @param data
- * the send buffer
- * @param offset
- * the offset into the buffer
- * @param count
- * the number of bytes to receive
- * @return int the actual number of bytes sent
- * @exception SocketException
- * if an error occurs while writing
- */
- static native int sendStreamImpl(FileDescriptor fd, byte[] data,
- int offset, int count) throws IOException;
+
+// /**
+// * Answer the result of attempting to accept a connection request to this
+// * stream socket in the IP stack.
+// *
+// * @param fdServer
+// * the server socket FileDescriptor
+// * @param newSocket
+// * the host socket a connection will be accepted on
+// * @param fdnewSocket
+// * the FileDescriptor for the host socket
+// * @param timeout
+// * the timeout that the server should listen for
+// * @exception SocketException
+// * if an error occurs while accepting connections
+// */
+// static native void acceptStreamSocketImpl(FileDescriptor fdServer,
+// SocketImpl newSocket, FileDescriptor fdnewSocket, int timeout)
+// throws IOException;
+//
+// /**
+// * Answer the number of bytes available to read on the socket without
+// * blocking.
+// *
+// * @param aFD
+// * the socket FileDescriptor
+// * @exception SocketException
+// * if an error occurs while peeking
+// */
+// static native int availableStreamImpl(FileDescriptor aFD)
+// throws SocketException;
+//
+// /**
+// * Answer the result of attempting to create a stream socket in the IP
+// * stack.
+// *
+// * @param aFD
+// * the socket FileDescriptor
+// * @exception SocketException
+// * if an error occurs while creating the socket
+// */
+// static native void createStreamSocketImpl(FileDescriptor aFD,
+// boolean preferIPv4Stack) throws SocketException;
+//
+// /**
+// * Allocate a datagram socket in the IP stack. The socket is associated with
+// * the <code>aFD</code>.
+// *
+// * @param aFD
+// * the FileDescriptor to associate with the socket
+// *
+// * @exception SocketException
+// * upon an allocation error
+// */
+//
+// static native void createDatagramSocketImpl(FileDescriptor aFD,
+// boolean preferIPv4Stack) throws SocketException;
+//
+// /**
+// * Answer the result of attempting to listen on a stream socket in the IP
+// * stack.
+// *
+// * @param aFD
+// * the socket FileDescriptor
+// * @param backlog
+// * the number of connection requests that may be queued before
+// * requests are rejected
+// * @exception SocketException
+// * if an error occurs while listening
+// */
+// static native void listenStreamSocketImpl(FileDescriptor aFD, int backlog)
+// throws SocketException;
+//
+// /**
+// * Recieve at most <code>count</code> bytes into the buffer
+// * <code>data</code> at the <code>offset</code> on the socket.
+// *
+// * @param aFD
+// * the socket FileDescriptor
+// * @param data
+// * the receive buffer
+// * @param offset
+// * the offset into the buffer
+// * @param count
+// * the max number of bytes to receive
+// * @param timeout
+// * the max time the read operation should block waiting for data
+// * @return int the actual number of bytes read
+// * @exception SocketException
+// * if an error occurs while reading
+// */
+// static native int receiveStreamImpl(FileDescriptor aFD, byte[] data,
+// int offset, int count, int timeout) throws IOException;
+//
+// /**
+// * Send <code>count</code> bytes from the buffer <code>data</code> at
+// * the <code>offset</code>, on the socket.
+// *
+// * @param data
+// * the send buffer
+// * @param offset
+// * the offset into the buffer
+// * @param count
+// * the number of bytes to receive
+// * @return int the actual number of bytes sent
+// * @exception SocketException
+// * if an error occurs while writing
+// */
+// static native int sendStreamImpl(FileDescriptor fd, byte[] data,
+// int offset, int count) throws IOException;
/**
* In the IP stack, read at most <code>count</code> bytes off the socket
@@ -370,8 +366,10 @@
if (shutdownInput)
return -1;
try {
- int read = receiveStreamImpl(fd, buffer, offset, count,
- receiveTimeout);
+// int read = receiveStreamImpl(fd, buffer, offset, count,
+// receiveTimeout);
+ int read = this.netImpl.receiveStream(fd, buffer, offset, count,
+ receiveTimeout);
if (read == -1)
shutdownInput = true;
return read;
@@ -423,10 +421,12 @@
int write(byte[] buffer, int offset, int count) throws IOException {
if (!streaming) {
- PlainSocketImpl2.sendDatagramImpl2(fd, buffer, offset, count, port,
- address);
+// PlainSocketImpl2.sendDatagramImpl2(fd, buffer, offset, count, port,
+// address);
+ this.netImpl.sendDatagram2(fd, buffer, offset, count, port,address);
}
- return sendStreamImpl(fd, buffer, offset, count);
+// return sendStreamImpl(fd, buffer, offset, count);
+ return this.netImpl.sendStream(fd, buffer, offset, count);
}
/**
@@ -434,20 +434,22 @@
*/
protected void shutdownInput() throws IOException {
shutdownInput = true;
- shutdownInputImpl(fd);
+// shutdownInputImpl(fd);
+ this.netImpl.shutdownInput(fd);
}
- private native void shutdownInputImpl(FileDescriptor descriptor) throws IOException;
+// private native void shutdownInputImpl(FileDescriptor descriptor) throws IOException;
/**
* Shutdown the output portion of the socket.
*/
protected void shutdownOutput() throws IOException {
- shutdownOutputImpl(fd);
+// shutdownOutputImpl(fd);
+ this.netImpl.shutdownOutput(fd);
}
- private native void shutdownOutputImpl(FileDescriptor descriptor)
- throws IOException;
+// private native void shutdownOutputImpl(FileDescriptor descriptor)
+// throws IOException;
/**
* Connect the socket to the host/port specified by the SocketAddress with a
@@ -486,7 +488,7 @@
*/
protected abstract void sendUrgentData(int value) throws IOException;
- static native boolean supportsUrgentDataImpl(FileDescriptor fd);
-
- static native boolean sendUrgentDataImpl(FileDescriptor fd, byte value);
+// static native boolean supportsUrgentDataImpl(FileDescriptor fd);
+//
+// static native boolean sendUrgentDataImpl(FileDescriptor fd, byte value);
}
Copied: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/GenericIPMreq.java (from r389138, incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/GenericIPMreq.java)
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/GenericIPMreq.java?p2=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/GenericIPMreq.java&p1=incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/GenericIPMreq.java&r1=389138&r2=389483&rev=389483&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/net/GenericIPMreq.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/GenericIPMreq.java Tue Mar 28 04:05:47 2006
@@ -1,4 +1,4 @@
-/* Copyright 2005, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,9 +13,13 @@
* limitations under the License.
*/
-package java.net;
+package org.apache.harmony.luni.net;
+import java.net.Inet4Address;
+import java.net.Inet6Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
import java.util.Enumeration;
/**
@@ -25,78 +29,79 @@
*/
final class GenericIPMreq {
- // private members
- private InetAddress multiaddr;
+ // private members
+ private InetAddress multiaddr;
- private InetAddress interfaceAddr;
+ private InetAddress interfaceAddr;
- private boolean isIPV6Address;
+ private boolean isIPV6Address;
- private int interfaceIdx;
+ private int interfaceIdx;
- /**
- * This constructor is used to create an instance of the object
- *
- * @param addr
- * multicast address to join/leave
- *
- */
- GenericIPMreq(InetAddress addr) {
- this.multiaddr = addr;
- this.interfaceAddr = null;
- this.interfaceIdx = 0;
- init();
- }
-
- /**
- * This constructor is used to create an instance of the object
- *
- * @param addr
- * multicast address to join/leave
- * @param netInterface
- * the NetworkInterface object identifying the interface on which
- * to join/leave
- *
- */
- GenericIPMreq(InetAddress addr, NetworkInterface netInterface) {
- this.multiaddr = addr;
- if (null != netInterface) {
- this.interfaceIdx = netInterface.getIndex();
-
- // here we need to get the first IPV4 address as we only use it if
- // we
- // are settting the interface for an IPV4 multicast socket. For
- // adds/drops on
- // IPV6 addresses we use the index within the networkInterface
- this.interfaceAddr = null;
- Enumeration theAddresses = netInterface.getInetAddresses();
- if ((addr instanceof Inet4Address) && (theAddresses != null)) {
- boolean found = false;
- while ((theAddresses.hasMoreElements()) && (found != true)) {
- InetAddress theAddress = (InetAddress) theAddresses
- .nextElement();
- if (theAddress instanceof Inet4Address) {
- this.interfaceAddr = theAddress;
- found = true;
- }
- }
- }
- } else {
- // network interface is null so we just want to defer the decision
- // to
- // the system
- this.interfaceIdx = 0;
- this.interfaceAddr = null;
- }
- init();
- }
-
- /**
- * This method does any required initialization for the constructors
- */
- private void init() {
- // set the flag indicating if the multicast address is an IPV6 address
- // or not
- isIPV6Address = ((this.multiaddr != null) && (this.multiaddr instanceof Inet6Address));
- }
+ /**
+ * This constructor is used to create an instance of the object
+ *
+ * @param addr
+ * multicast address to join/leave
+ *
+ */
+ GenericIPMreq(InetAddress addr) {
+ multiaddr = addr;
+ interfaceAddr = null;
+ interfaceIdx = 0;
+ init();
+ }
+
+ /**
+ * This constructor is used to create an instance of the object
+ *
+ * @param addr
+ * multicast address to join/leave
+ * @param netInterface
+ * the NetworkInterface object identifying the interface on which
+ * to join/leave
+ *
+ */
+ GenericIPMreq(InetAddress addr, NetworkInterface netInterface) {
+ multiaddr = addr;
+ if (null != netInterface) {
+ // TODO check if necessary
+ //interfaceIdx = netInterface.getIndex();
+
+ // here we need to get the first IPV4 address as we only use it if
+ // we
+ // are settting the interface for an IPV4 multicast socket. For
+ // adds/drops on
+ // IPV6 addresses we use the index within the networkInterface
+ interfaceAddr = null;
+ Enumeration theAddresses = netInterface.getInetAddresses();
+ if ((addr instanceof Inet4Address) && (theAddresses != null)) {
+ boolean found = false;
+ while ((theAddresses.hasMoreElements()) && (found != true)) {
+ InetAddress theAddress = (InetAddress) theAddresses
+ .nextElement();
+ if (theAddress instanceof Inet4Address) {
+ interfaceAddr = theAddress;
+ found = true;
+ }
+ }
+ }
+ } else {
+ // network interface is null so we just want to defer the decision
+ // to
+ // the system
+ interfaceIdx = 0;
+ interfaceAddr = null;
+ }
+ init();
+ }
+
+ /**
+ * This method does any required initialization for the constructors
+ */
+ private void init() {
+ // set the flag indicating if the multicast address is an IPV6 address
+ // or not
+ isIPV6Address = ((multiaddr != null) && (multiaddr instanceof Inet6Address));
+ }
}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java?rev=389483&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/net/NetUtil.java Tue Mar 28 04:05:47 2006
@@ -0,0 +1,63 @@
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.harmony.luni.net;
+
+import java.security.AccessController;
+
+import com.ibm.oti.util.PriviAction;
+
+public class NetUtil {
+
+ /*
+ * FIXME: enhance performance to cache the values
+ */
+
+ /**
+ * Answer whether to use a SOCKS proxy.
+ *
+ * @return boolean
+ */
+ public static boolean usingSocks() {
+ String proxySet = (String) AccessController
+ .doPrivileged(new PriviAction("socksProxySet"));
+ if (proxySet != null) {
+ return proxySet.toLowerCase().equals("true");
+ }
+ return AccessController.doPrivileged(new PriviAction("socksProxyHost")) != null;
+ }
+
+ /**
+ * Answer whether to prefer IPV6 address
+ *
+ * @return boolean
+ */
+ public static boolean preferIPv6Addresses() {
+ String result = (String) AccessController.doPrivileged(new PriviAction(
+ "java.net.preferIPv6Addresses"));
+ return "true".equals(result);
+ }
+
+ /**
+ * Answer whether to prefer IPV4 stack
+ *
+ * @return boolean
+ */
+ public static boolean preferIPv4Stack() {
+ String result = (String) AccessController.doPrivileged(new PriviAction(
+ "java.net.preferIPv4Stack"));
+ return "true".equals(result);
+ }
+
+}