One true way to query "java.net.preferIPv6Addresses".

Also stop querying and passing this boolean to code that doesn't care. (We
sort our InetAddress[]s appropriately, so most code doesn't need to worry
about this.)

Bug: http://code.google.com/p/android/issues/detail?id=5903
diff --git a/libcore/luni/src/main/java/java/net/DatagramSocketImpl.java b/libcore/luni/src/main/java/java/net/DatagramSocketImpl.java
index 12b2079..7a29256 100644
--- a/libcore/luni/src/main/java/java/net/DatagramSocketImpl.java
+++ b/libcore/luni/src/main/java/java/net/DatagramSocketImpl.java
@@ -90,8 +90,7 @@
      * @return the local address to which the socket is bound.
      */
     InetAddress getLocalAddress() {
-        return Platform.getNetworkSystem().getSocketLocalAddress(fd,
-                NetUtil.preferIPv6Addresses());
+        return Platform.getNetworkSystem().getSocketLocalAddress(fd);
     }
 
     /**
diff --git a/libcore/luni/src/main/java/java/net/InetAddress.java b/libcore/luni/src/main/java/java/net/InetAddress.java
index 75d27ca..fccfc4c 100644
--- a/libcore/luni/src/main/java/java/net/InetAddress.java
+++ b/libcore/luni/src/main/java/java/net/InetAddress.java
@@ -196,7 +196,7 @@
     /**
      * Converts an array of byte arrays representing raw IP addresses of a host
      * to an array of InetAddress objects, sorting to respect the value of the
-     * system preferIPv6Addresses preference.
+     * system property {@code "java.net.preferIPv6Addresses"}.
      *
      * @param rawAddresses the raw addresses to convert.
      * @param hostName the hostname corresponding to the IP address.
@@ -205,7 +205,7 @@
     static InetAddress[] bytesToInetAddresses(byte[][] rawAddresses,
             String hostName) {
         // Sort the raw byte arrays.
-        Comparator<byte[]> comparator = preferIPv6Addresses()
+        Comparator<byte[]> comparator = NetUtil.preferIPv6Addresses()
                 ? LONGEST_FIRST : SHORTEST_FIRST;
         Arrays.sort(rawAddresses, comparator);
 
@@ -259,7 +259,7 @@
     static InetAddress[] getAllByNameImpl(String host, boolean returnUnshared)
             throws UnknownHostException {
         if (host == null || 0 == host.length()) {
-            if (preferIPv6Addresses()) {
+            if (NetUtil.preferIPv6Addresses()) {
                 return new InetAddress[] { Inet6Address.LOOPBACK,
                                            Inet4Address.LOOPBACK };
             } else {
@@ -530,38 +530,16 @@
     // static int inetAddr(String host) throws UnknownHostException
     // END android-removed
 
-    /**
-     * Convert a string containing an IPv4 Internet Protocol dotted address into
-     * a binary address. Note, the special case of '255.255.255.255' throws an
-     * exception, so this value should not be used as an argument. See also
-     * inetAddr(String).
-     */
     // BEGIN android-removed
     // static native int inetAddrImpl(String host) throws UnknownHostException;
     // END android-removed
 
-    /**
-     * Convert a binary address into a string containing an Ipv4 Internet
-     * Protocol dotted address.
-     */
     // BEGIN android-removed
     // static native String inetNtoaImpl(int hipAddr);
     // END android-removed
 
     // BEGIN android-removed
-    /**
-     * Query the IP stack for the host address. The host is in string name form.
-     *
-     * @param name
-     *            the host name to lookup
-     * @param preferIPv6Address
-     *            address preference if underlying platform is V4/V6
-     * @return InetAddress the host address
-     * @throws UnknownHostException
-     *             if an error occurs during lookup
-     */
-    // static native InetAddress getHostByNameImpl(String name,
-    //         boolean preferIPv6Address) throws UnknownHostException;
+    // static native InetAddress getHostByNameImpl(String name) throws UnknownHostException;
     // END android-removed
 
     static String getHostNameInternal(String host, boolean isCheck) throws UnknownHostException {
@@ -1142,12 +1120,6 @@
         return value;
     }
 
-    static boolean preferIPv6Addresses() {
-        String result = AccessController.doPrivileged(new PriviAction<String>(
-                "java.net.preferIPv6Addresses")); //$NON-NLS-1$
-        return "true".equals(result); //$NON-NLS-1$
-    }
-
     private static final ObjectStreamField[] serialPersistentFields = {
             new ObjectStreamField("address", Integer.TYPE), //$NON-NLS-1$
             new ObjectStreamField("family", Integer.TYPE), //$NON-NLS-1$
diff --git a/libcore/luni/src/main/java/java/net/MulticastSocket.java b/libcore/luni/src/main/java/java/net/MulticastSocket.java
index 60d967b..22cefb9 100644
--- a/libcore/luni/src/main/java/java/net/MulticastSocket.java
+++ b/libcore/luni/src/main/java/java/net/MulticastSocket.java
@@ -20,6 +20,7 @@
 import java.io.IOException;
 import java.util.Enumeration;
 
+import org.apache.harmony.luni.net.NetUtil;
 import org.apache.harmony.luni.net.PlainDatagramSocketImpl;
 import org.apache.harmony.luni.util.Msg;
 
@@ -142,8 +143,7 @@
             // interface with only the any address. We do this to be
             // compatible
             InetAddress theAddresses[] = new InetAddress[1];
-            if ((Socket.preferIPv4Stack() == false)
-                    && (InetAddress.preferIPv6Addresses() == true)) {
+            if (!Socket.preferIPv4Stack() && NetUtil.preferIPv6Addresses()) {
                 theAddresses[0] = Inet6Address.ANY;
             } else {
                 theAddresses[0] = Inet4Address.ANY;
diff --git a/libcore/luni/src/main/java/java/net/Socket.java b/libcore/luni/src/main/java/java/net/Socket.java
index 6ecc548..2962b62 100644
--- a/libcore/luni/src/main/java/java/net/Socket.java
+++ b/libcore/luni/src/main/java/java/net/Socket.java
@@ -202,7 +202,7 @@
      * <p><strong>Implementation note:</strong> this implementation tries each
      * IP address for the given hostname until it either connects successfully
      * or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
-     * order specified by {@code preferIPv6Addresses}.
+     * order specified by the system property {@code "java.net.preferIPv6Addresses"}.
      *
      * @param dstName
      *            the target host name or IP address to connect to.
@@ -233,7 +233,7 @@
      * <p><strong>Implementation note:</strong> this implementation tries each
      * IP address for the given hostname until it either connects successfully
      * or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
-     * order specified by {@code preferIPv6Addresses}.
+     * order specified by the system property {@code "java.net.preferIPv6Addresses"}.
      *
      * @param dstName
      *            the target host name or IP address to connect to.
@@ -266,7 +266,7 @@
      * <p><strong>Implementation note:</strong> this implementation tries each
      * IP address for the given hostname until it either connects successfully
      * or it exhausts the set. It will try both IPv4 and IPv6 addresses in the
-     * order specified by {@code preferIPv6Addresses}.
+     * order specified by the system property {@code "java.net.preferIPv6Addresses"}.
      *
      * @param hostName
      *            the target host name or IP address to connect to.
@@ -483,8 +483,7 @@
         if (!isBound()) {
             return Inet4Address.ANY;
         }
-        return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd,
-                InetAddress.preferIPv6Addresses());
+        return Platform.getNetworkSystem().getSocketLocalAddress(impl.fd);
     }
 
     /**
diff --git a/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java b/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
index cbecab5..4d75faf 100644
--- a/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
+++ b/libcore/luni/src/main/java/org/apache/harmony/luni/net/PlainDatagramSocketImpl.java
@@ -121,7 +121,7 @@
         if (0 != port) {
             localPort = port;
         } else {
-            localPort = netImpl.getSocketLocalPort(fd, NetUtil.preferIPv6Addresses());
+            localPort = netImpl.getSocketLocalPort(fd);
         }
 
         try {
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 6773fd4..09b61da 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
@@ -187,8 +187,7 @@
         if (0 != aPort) {
             localport = aPort;
         } else {
-            localport = netImpl.getSocketLocalPort(fd, NetUtil
-                    .preferIPv6Addresses());
+            localport = netImpl.getSocketLocalPort(fd);
         }
     }
 
@@ -210,9 +209,9 @@
 
     @Override
     protected void connect(String aHost, int aPort) throws IOException {
-        InetAddress anAddr = netImpl.getHostByName(aHost, NetUtil
-                .preferIPv6Addresses());
-        connect(anAddr, aPort);
+        // BEGIN android-changed: remove useless IPv6 check.
+        connect(netImpl.getHostByName(aHost), aPort);
+        // END android-changed
     }
 
     @Override
@@ -378,10 +377,9 @@
         if (null == proxyName) {
             proxyName = addr.getAddress().getHostAddress();
         }
-
-        InetAddress anAddr = netImpl.getHostByName(proxyName, NetUtil
-                .preferIPv6Addresses());
-        return anAddr;
+        // BEGIN android-changed: remove useless IPv6 check.
+        return netImpl.getHostByName(proxyName);
+        // END android-changed
     }
 
     /**
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 3a5d737..bc56f68 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
@@ -166,8 +166,9 @@
     public int sendDatagram2(FileDescriptor fd, byte[] data, int offset,
             int length, int port, InetAddress inetAddress) throws IOException;
 
-    public InetAddress getSocketLocalAddress(FileDescriptor aFD,
-            boolean preferIPv6Addresses);
+    // 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.
@@ -203,15 +204,15 @@
             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.
      * 
-     * @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
+     * @param aFD the socket descriptor
+     * @return int the local port to which the socket is bound
      */
-    public int getSocketLocalPort(FileDescriptor aFD,
-            boolean preferIPv6Addresses);
+    public int getSocketLocalPort(FileDescriptor aFD);
+    // END android-changed
 
     /*
      * Query the IP stack for the nominated socket option.
@@ -246,8 +247,9 @@
 
     public InetAddress getHostByAddr(byte[] addr) throws UnknownHostException;
 
-    public InetAddress getHostByName(String addr, boolean preferIPv6Addresses)
-            throws UnknownHostException;
+    // BEGIN android-changed: remove useless IPv6 check.
+    public InetAddress getHostByName(String addr) throws UnknownHostException;
+    // END android-changed
 
     public void setInetAddress(InetAddress sender, byte[] address);
 
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 708f254..7c5cf66 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
@@ -220,18 +220,14 @@
     //         throws UnknownHostException;
     // END android-removed
 
-    // BEGIN android-removed
-    public InetAddress getHostByName(String hostName,
-            boolean preferIPv6Addresses) throws UnknownHostException {
-        // BEGIN android-changed
-        // Wallpaper fix for http://b/1851257.
+    // BEGIN android-changed: remove useless IPv6 check.
+    public InetAddress getHostByName(String hostName) throws UnknownHostException {
         return InetAddress.getByName(hostName);
-        // END android-changed
     }
+    // END android-changed
 
     // BEGIN android-removed
-    // static native InetAddress getHostByNameImpl(String addr,
-    //         boolean preferIPv6Addresses) throws UnknownHostException;
+    // static native InetAddress getHostByNameImpl(String addr) throws UnknownHostException;
     // END android-removed
 
     public int getSocketFlags() {
@@ -246,29 +242,15 @@
 
     static native int getSocketFlagsImpl();
 
-    public InetAddress getSocketLocalAddress(FileDescriptor fd,
-            boolean preferIPv6Addresses) {
-        return getSocketLocalAddressImpl(fd, preferIPv6Addresses);
+    public InetAddress getSocketLocalAddress(FileDescriptor fd) {
+        return getSocketLocalAddressImpl(fd);
     }
-    static native InetAddress getSocketLocalAddressImpl(FileDescriptor aFD,
-            boolean preferIPv6Addresses);
+    static native InetAddress getSocketLocalAddressImpl(FileDescriptor aFD);
 
-    /**
-     * 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 the local port to which the socket is bound
-     */
-    public int getSocketLocalPort(FileDescriptor aFD,
-            boolean preferIPv6Addresses) {
-        return getSocketLocalPortImpl(aFD, preferIPv6Addresses);
+    public int getSocketLocalPort(FileDescriptor aFD) {
+        return getSocketLocalPortImpl(aFD);
     }
-
-    static native int getSocketLocalPortImpl(FileDescriptor aFD,
-            boolean preferIPv6Addresses);
+    static native int getSocketLocalPortImpl(FileDescriptor aFD);
 
     /**
      * Query the IP stack for the nominated socket option.
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 ac44c28..07eb381 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
@@ -2375,7 +2375,7 @@
 }
 
 static jobject osNetworkSystem_getSocketLocalAddressImpl(JNIEnv* env,
-        jclass, jobject fileDescriptor, jboolean preferIPv6Addresses) {
+        jclass, jobject fileDescriptor) {
     // LOGD("ENTER getSocketLocalAddressImpl");
 
     int fd;
@@ -2398,7 +2398,7 @@
 }
 
 static jint osNetworkSystem_getSocketLocalPortImpl(JNIEnv* env, jclass,
-        jobject fileDescriptor, jboolean preferIPv6Addresses) {
+        jobject fileDescriptor) {
     // LOGD("ENTER getSocketLocalPortImpl");
 
     int fd;
@@ -2955,8 +2955,8 @@
     { "shutdownOutputImpl",                "(Ljava/io/FileDescriptor;)V",                                              (void*) osNetworkSystem_shutdownOutputImpl                 },
     { "sendDatagramImpl2",                 "(Ljava/io/FileDescriptor;[BIIILjava/net/InetAddress;)I",                   (void*) osNetworkSystem_sendDatagramImpl2                  },
     { "selectImpl",                        "([Ljava/io/FileDescriptor;[Ljava/io/FileDescriptor;II[IJ)Z",               (void*) osNetworkSystem_selectImpl                         },
-    { "getSocketLocalAddressImpl",         "(Ljava/io/FileDescriptor;Z)Ljava/net/InetAddress;",                        (void*) osNetworkSystem_getSocketLocalAddressImpl          },
-    { "getSocketLocalPortImpl",            "(Ljava/io/FileDescriptor;Z)I",                                             (void*) osNetworkSystem_getSocketLocalPortImpl             },
+    { "getSocketLocalAddressImpl",         "(Ljava/io/FileDescriptor;)Ljava/net/InetAddress;",                         (void*) osNetworkSystem_getSocketLocalAddressImpl          },
+    { "getSocketLocalPortImpl",            "(Ljava/io/FileDescriptor;)I",                                              (void*) osNetworkSystem_getSocketLocalPortImpl             },
     { "getSocketOptionImpl",               "(Ljava/io/FileDescriptor;I)Ljava/lang/Object;",                            (void*) osNetworkSystem_getSocketOptionImpl                },
     { "setSocketOptionImpl",               "(Ljava/io/FileDescriptor;ILjava/lang/Object;)V",                           (void*) osNetworkSystem_setSocketOptionImpl                },
     { "getSocketFlagsImpl",                "()I",                                                                      (void*) osNetworkSystem_getSocketFlagsImpl                 },
diff --git a/libcore/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java b/libcore/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
index 109a819..b3b5fbd 100644
--- a/libcore/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
+++ b/libcore/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
@@ -40,7 +40,6 @@
 import java.nio.channels.NotYetConnectedException;
 import java.nio.channels.spi.SelectorProvider;
 
-import org.apache.harmony.luni.net.NetUtil;
 import org.apache.harmony.luni.net.PlainDatagramSocketImpl;
 import org.apache.harmony.luni.platform.FileDescriptorHandler;
 import org.apache.harmony.luni.platform.INetworkSystem;
@@ -135,8 +134,7 @@
      * @see DatagramSocket
      */
     InetAddress getLocalAddress() {
-        return networkSystem.getSocketLocalAddress(fd, NetUtil
-                .preferIPv6Addresses());
+        return networkSystem.getSocketLocalAddress(fd);
     }
 
     /**
diff --git a/libcore/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java b/libcore/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
index cf6d839..40915f6 100644
--- a/libcore/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
+++ b/libcore/nio/src/main/java/org/apache/harmony/nio/internal/SocketChannelImpl.java
@@ -298,8 +298,8 @@
         }
 
         // set local port
-        localPort = networkSystem.getSocketLocalPort(fd, false);
-        localAddress = networkSystem.getSocketLocalAddress(fd, false);
+        localPort = networkSystem.getSocketLocalPort(fd);
+        localAddress = networkSystem.getSocketLocalAddress(fd);
 
         // set the connected address.
         connectAddress = inetSocketAddress;
@@ -344,7 +344,7 @@
                     HY_PORT_SOCKET_STEP_CHECK, connectContext);
             finished = (result == CONNECT_SUCCESS);
             isBound = finished;
-            localAddress = networkSystem.getSocketLocalAddress(fd, false);
+            localAddress = networkSystem.getSocketLocalAddress(fd);
         } catch (ConnectException e) {
             if (isOpen()) {
                 close();
@@ -733,8 +733,7 @@
                 throw new AlreadyConnectedException();
             }
             super.connect(remoteAddr, timeout);
-            channel.localAddress = networkSystem.getSocketLocalAddress(
-                    channel.fd, false);
+            channel.localAddress = networkSystem.getSocketLocalAddress(channel.fd);
             if (super.isConnected()) {
                 channel.setConnected();
                 channel.isBound = super.isBound();