Trivial tidying up of networking code.
Removing cruft and renaming things.
Change-Id: I317726c40a1addf3a1ad9d905239438424082549
diff --git a/libcore/luni/src/main/java/java/net/InetAddress.java b/libcore/luni/src/main/java/java/net/InetAddress.java
index 31be34e..85b4d0b 100644
--- a/libcore/luni/src/main/java/java/net/InetAddress.java
+++ b/libcore/luni/src/main/java/java/net/InetAddress.java
@@ -524,22 +524,6 @@
private static native String getnameinfo(byte[] addr);
// END android-changed
- // BEGIN android-removed
- // static int inetAddr(String host) throws UnknownHostException
- // END android-removed
-
- // BEGIN android-removed
- // static native int inetAddrImpl(String host) throws UnknownHostException;
- // END android-removed
-
- // BEGIN android-removed
- // static native String inetNtoaImpl(int hipAddr);
- // END android-removed
-
- // BEGIN android-removed
- // static native InetAddress getHostByNameImpl(String name) throws UnknownHostException;
- // END android-removed
-
static String getHostNameInternal(String host, boolean isCheck) throws UnknownHostException {
if (host == null || 0 == host.length()) {
return Inet4Address.LOOPBACK.getHostAddress();
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java b/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
index e6268c2..e7c6c77 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainSocketImpl.java
@@ -161,7 +161,7 @@
protected synchronized int available() throws IOException {
// we need to check if the input has been shutdown. If so
// we should return that there is no data to be read
- if (shutdownInput == true) {
+ if (shutdownInput) {
return 0;
}
return Platform.getFileSystem().ioctlAvailable(fd);
@@ -191,9 +191,7 @@
@Override
protected void connect(String aHost, int aPort) throws IOException {
- // BEGIN android-changed: remove useless IPv6 check.
- connect(netImpl.getHostByName(aHost), aPort);
- // END android-changed
+ connect(InetAddress.getByName(aHost), aPort);
}
@Override
@@ -290,7 +288,7 @@
// server during the bind.
return;
}
- netImpl.listenStreamSocket(fd, backlog);
+ netImpl.listen(fd, backlog);
}
@Override
@@ -348,9 +346,7 @@
if (null == proxyName) {
proxyName = addr.getAddress().getHostAddress();
}
- // BEGIN android-changed: remove useless IPv6 check.
- return netImpl.getHostByName(proxyName);
- // END android-changed
+ return InetAddress.getByName(proxyName);
}
/**
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
index 7ffb30c..64da5f7 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
@@ -45,9 +45,6 @@
*/
public final int SOCKET_CONNECT_STEP_CHECK = 1;
- /*
- * socket accept
- */
public void accept(FileDescriptor fdServer, SocketImpl newSocket,
FileDescriptor fdnewSocket, int timeout) throws IOException;
@@ -63,30 +60,17 @@
public int write(FileDescriptor fd, byte[] data, int offset, int count)
throws IOException;
- // BEGIN android-changed
- // added offset parameter
public int writeDirect(FileDescriptor fd, int address, int offset, int count)
throws IOException;
- // END android-changed
- // BEGIN android-removed
- // public int writev(FileDescriptor fd, Object[] buffers, int[] offsets,
- // int[] counts, int length) throws IOException;
- // END android-removed
+ public void setNonBlocking(FileDescriptor aFD, boolean block) throws IOException;
- public void setNonBlocking(FileDescriptor aFD, boolean block)
- throws IOException;
-
- // BEGIN android-changed (we always throw on error, the value returned was always 0)
public void connect(FileDescriptor aFD, int trafficClass,
InetAddress inetAddress, int port) throws IOException;
- // END android-changed
- // BEGIN android-changed
public int connectWithTimeout(FileDescriptor aFD, int timeout,
int trafficClass, InetAddress hostname, int port, int step,
byte[] context) throws IOException;
- // END android-changed
public int sendDatagram(FileDescriptor fd, byte[] data, int offset,
int length, int port, boolean bindToDevice, int trafficClass,
@@ -129,11 +113,6 @@
public void connectDatagram(FileDescriptor aFD, int port, int trafficClass,
InetAddress inetAddress) throws SocketException;
- // BEGIN android-removed
- // public int receiveStream(FileDescriptor aFD, byte[] data, int offset,
- // int count, int timeout) throws IOException;
- // END android-removed
-
public void shutdownInput(FileDescriptor descriptor) throws IOException;
public void shutdownOutput(FileDescriptor descriptor) throws IOException;
@@ -142,20 +121,13 @@
public void sendUrgentData(FileDescriptor fd, byte value);
- // BEGIN android-removed
- // public void acceptStreamSocket(FileDescriptor fdServer,
- // SocketImpl newSocket, FileDescriptor fdnewSocket, int timeout)
- // throws IOException;
- // END android-removed
-
public void createServerStreamSocket(FileDescriptor aFD, boolean preferIPv4Stack)
throws SocketException;
public void createStreamSocket(FileDescriptor aFD, boolean preferIPv4Stack)
throws SocketException;
- public void listenStreamSocket(FileDescriptor aFD, int backlog)
- throws SocketException;
+ public void listen(FileDescriptor aFD, int backlog) throws SocketException;
public void connectStreamWithTimeoutSocket(FileDescriptor aFD, int aport,
int timeout, int trafficClass, InetAddress inetAddress)
@@ -164,9 +136,7 @@
public int sendDatagram2(FileDescriptor fd, byte[] data, int offset,
int length, int port, InetAddress inetAddress) throws IOException;
- // BEGIN android-changed: remove useless IPv6 check.
public InetAddress getSocketLocalAddress(FileDescriptor aFD);
- // END android-changed
/**
* Select the given file descriptors for read and write operations.
@@ -202,7 +172,6 @@
int numReadable, int numWritable, long timeout, int[] flags)
throws SocketException;
- // BEGIN android-changed: remove useless IPv6 check.
/*
* Query the IP stack for the local port to which this socket is bound.
*
@@ -210,7 +179,6 @@
* @return int the local port to which the socket is bound
*/
public int getSocketLocalPort(FileDescriptor aFD);
- // END android-changed
/*
* Query the IP stack for the nominated socket option.
@@ -234,36 +202,16 @@
public void setSocketOption(FileDescriptor aFD, int opt, Object optVal)
throws SocketException;
- /*
- * Close the socket in the IP stack.
- *
- * @param aFD the socket descriptor
- */
+ // TODO: change OSFileSystem.close to take a FileDescriptor, and lose this duplicate.
public void socketClose(FileDescriptor aFD) throws IOException;
- public InetAddress getHostByAddr(byte[] addr) throws UnknownHostException;
-
- // BEGIN android-changed: remove useless IPv6 check.
- public InetAddress getHostByName(String addr) throws UnknownHostException;
- // END android-changed
-
+ // TODO: change the single caller so that recvConnectedDatagram
+ // can mutate the InetAddress as a side-effect.
public void setInetAddress(InetAddress sender, byte[] address);
- // BEGIN android-added
- public String byteArrayToIpString(byte[] address)
- throws UnknownHostException;
+ public String byteArrayToIpString(byte[] address) throws UnknownHostException;
- public byte[] ipStringToByteArray(String address)
- throws UnknownHostException;
- // END android-added
-
- // BEGIN android-removed
- // public boolean isReachableByICMP(InetAddress dest,InetAddress source,int ttl,int timeout);
- // END android-removed
+ public byte[] ipStringToByteArray(String address) throws UnknownHostException;
public Channel inheritedChannel();
-
- // BEGIN android-removed: we do this statically, when we start the VM.
- // public void oneTimeInitialization(boolean jcl_supports_ipv6);
- // END android-removed
}
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
index 8d7ce58..4f2d47d 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
@@ -15,10 +15,6 @@
* limitations under the License.
*/
-// BEGIN android-note
-// Address length was changed from long to int for performance reasons.
-// END android-note
-
package org.apache.harmony.luni.platform;
import java.io.FileDescriptor;
@@ -29,19 +25,13 @@
import java.net.SocketImpl;
import java.net.UnknownHostException;
import java.nio.channels.Channel;
-// BEGIN android-removed
-// import java.nio.channels.SelectableChannel;
-// END android-removed
/**
* This wraps native code that implements the INetworkSystem interface.
+ * Address length was changed from long to int for performance reasons.
*/
final class OSNetworkSystem implements INetworkSystem {
-
- private static final int ERRORCODE_SOCKET_TIMEOUT = -209;
- private static final int ERRORCODE_SOCKET_INTERRUPTED = -208;
-
- private static OSNetworkSystem singleton = new OSNetworkSystem();
+ private static final OSNetworkSystem singleton = new OSNetworkSystem();
public static OSNetworkSystem getOSNetworkSystem() {
return singleton;
@@ -55,12 +45,10 @@
public native void bind(FileDescriptor fd, InetAddress inetAddress, int port) throws SocketException;
- // BEGIN android-changed (removed unused return value and useless native method)
public void connect(FileDescriptor fd, int trafficClass,
InetAddress inetAddress, int port) throws IOException{
connectStreamWithTimeoutSocket(fd, port, 0, trafficClass, inetAddress);
}
- // END android-changed
public native void connectDatagram(FileDescriptor fd, int port,
int trafficClass, InetAddress inetAddress) throws SocketException;
@@ -69,11 +57,9 @@
int port, int timeout, int trafficClass, InetAddress inetAddress)
throws IOException;
- // BEGIN android-changed: changed context from Long to byte[]
public native int connectWithTimeout(FileDescriptor fd, int timeout,
int trafficClass, InetAddress inetAddress, int port, int step,
byte[] context) throws IOException;
- // END android-changed
// TODO: preferIPv4Stack is ignored.
public native void createDatagramSocket(FileDescriptor fd, boolean preferIPv4Stack)
@@ -89,26 +75,9 @@
public native void disconnectDatagram(FileDescriptor fd) throws SocketException;
- // TODO: rewrite callers to skip the middleman.
- public InetAddress getHostByAddr(byte[] ipAddress) throws UnknownHostException {
- // BEGIN android-changed
- // Wallpaper fix for http://b/1851257. This is a layering violation,
- // but at least the method has the right return type.
- // TODO: Fix the socket code to remove this method altogether.
- return InetAddress.getByAddress(ipAddress);
- // END android-changed
- }
+ public native String byteArrayToIpString(byte[] address) throws UnknownHostException;
- // TODO: rewrite callers to skip the middleman.
- public InetAddress getHostByName(String hostName) throws UnknownHostException {
- return InetAddress.getByName(hostName);
- }
-
- public native String byteArrayToIpString(byte[] address)
- throws UnknownHostException;
-
- public native byte[] ipStringToByteArray(String address)
- throws UnknownHostException;
+ public native byte[] ipStringToByteArray(String address) throws UnknownHostException;
public native InetAddress getSocketLocalAddress(FileDescriptor fd);
@@ -121,19 +90,17 @@
return null;
}
- public native void listenStreamSocket(FileDescriptor fd, int backlog) throws SocketException;
+ public native void listen(FileDescriptor fd, int backlog) throws SocketException;
public native int peekDatagram(FileDescriptor fd, InetAddress sender, int receiveTimeout)
throws IOException;
public int read(FileDescriptor fd, byte[] data, int offset, int count,
int timeout) throws IOException {
- // BEGIN android-added safety!
if (offset < 0 || count < 0 || offset > data.length - count) {
throw new IllegalArgumentException("data.length=" + data.length + " offset=" + offset +
" count=" + count);
}
- // END android-added
return readSocketImpl(fd, data, offset, count, timeout);
}
@@ -194,16 +161,12 @@
return true;
}
- // BEGIN android-changed: handle errors in native code
return selectImpl(readFDs, writeFDs, numReadable, numWritable, flags, timeout);
- // END android-changed
}
- // BEGIN android-changed: return type (we throw in native code, with descriptive errors)
static native boolean selectImpl(FileDescriptor[] readfd,
FileDescriptor[] writefd, int cread, int cwirte, int[] flags,
long timeout);
- // END android-changed
// TODO: bindToDevice is unused.
public native int sendConnectedDatagram(FileDescriptor fd, byte[] data,
diff --git a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
index f38f29c..2344d14 100644
--- a/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
+++ b/libcore/luni/src/main/native/org_apache_harmony_luni_platform_OSNetworkSystem.cpp
@@ -1794,17 +1794,15 @@
}
}
-static void osNetworkSystem_listenStreamSocket(JNIEnv* env, jobject,
- jobject fileDescriptor, jint backlog) {
- int handle;
- if (!jniGetFd(env, fileDescriptor, handle)) {
+static void osNetworkSystem_listen(JNIEnv* env, jobject, jobject fileDescriptor, jint backlog) {
+ int fd;
+ if (!jniGetFd(env, fileDescriptor, fd)) {
return;
}
- int rc = listen(handle, backlog);
+ int rc = listen(fd, backlog);
if (rc == -1) {
jniThrowSocketException(env, errno);
- return;
}
}
@@ -2853,7 +2851,7 @@
{ "getSocketLocalPort", "(Ljava/io/FileDescriptor;)I", (void*) osNetworkSystem_getSocketLocalPort },
{ "getSocketOption", "(Ljava/io/FileDescriptor;I)Ljava/lang/Object;", (void*) osNetworkSystem_getSocketOption },
{ "ipStringToByteArray", "(Ljava/lang/String;)[B", (void*) osNetworkSystem_ipStringToByteArray },
- { "listenStreamSocket", "(Ljava/io/FileDescriptor;I)V", (void*) osNetworkSystem_listenStreamSocket },
+ { "listen", "(Ljava/io/FileDescriptor;I)V", (void*) osNetworkSystem_listen },
{ "peekDatagram", "(Ljava/io/FileDescriptor;Ljava/net/InetAddress;I)I", (void*) osNetworkSystem_peekDatagram },
{ "readDirect", "(Ljava/io/FileDescriptor;III)I", (void*) osNetworkSystem_readDirect },
{ "readSocketImpl", "(Ljava/io/FileDescriptor;[BIII)I", (void*) osNetworkSystem_readSocketImpl },