Skip to content

Commit

Permalink
Merge pull request #617 from nextcloud/backport/614/stable-2.6
Browse files Browse the repository at this point in the history
[stable-2.6] Use ArrayList instead of List, as some lists are not sortable
  • Loading branch information
tobiasKaminsky authored May 5, 2021
2 parents 5d25c88 + 400d67a commit f3fb318
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/com/nextcloud/common/IPV6PreferringDNS.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,33 @@
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import okhttp3.Dns;

/**
* Implementation of DNS which prefers IPV6 addresses (if available) over IPV4.
*/
public class IPV6PreferringDNS implements Dns {

private Map<String, List<InetAddress>> cache = new HashMap<>();
private Map<String, ArrayList<InetAddress>> cache = new HashMap<>();

@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
List<InetAddress> addresses = cache.get(hostname.toLowerCase(Locale.ROOT));
ArrayList<InetAddress> addresses = cache.get(hostname.toLowerCase(Locale.ROOT));

if (addresses != null) {
return addresses;
}

addresses = Dns.SYSTEM.lookup(hostname);
addresses = new ArrayList<>(Dns.SYSTEM.lookup(hostname));
Collections.sort(addresses, (address1, address2) -> {
if(address1 instanceof Inet4Address) {
if (address1 instanceof Inet4Address) {
return 1;
} else {
return -1;
Expand All @@ -38,4 +39,4 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {

return addresses;
}
}
}

0 comments on commit f3fb318

Please sign in to comment.