import java.io.BufferedReader;
import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.IDN;
import java.util.HashSet;
public class PublicSuffixes {
- HashSet<String> suffixes = new HashSet<>();
+ private HashSet<String> suffixes = new HashSet<>();
- HashSet<String> wildcards = new HashSet<>();
+ private HashSet<String> wildcards = new HashSet<>();
- HashSet<String> exceptions = new HashSet<>();
+ private HashSet<String> exceptions = new HashSet<>();
- private static final String url = "https://publicsuffix.org/list/effective_tld_names.dat";
+ static final String url = "https://publicsuffix.org/list/effective_tld_names.dat";
private static PublicSuffixes instance;
- private static void generateDefault() throws IOException {
- try (BufferedReader br = new BufferedReader(new InputStreamReader(PublicSuffixes.class.getResourceAsStream("effective_tld_names.dat"), "UTF-8"))) {
- instance = new PublicSuffixes(br);
+ private static PublicSuffixes generateDefault() throws IOException {
+ InputStream res = PublicSuffixes.class.getResourceAsStream("effective_tld_names.dat");
+
+ if (null == res) {
+ throw new Error("Public Suffix List could not be loaded.");
+ }
+
+ try (BufferedReader br = new BufferedReader(new InputStreamReader(res, "UTF-8"))) {
+ return new PublicSuffixes(br);
}
}
- public static PublicSuffixes getInstance() {
+ public synchronized static PublicSuffixes getInstance() {
if (instance == null) {
try {
- generateDefault();
+ instance = generateDefault();
} catch (IOException e) {
throw new Error(e);
}
if (line.isEmpty()) {
continue;
}
- line = line.split("\\s", 2)[0];
+ String[] lineParts = line.split("\\s", 2);
+ if (lineParts.length == 0) {
+ throw new Error("split had strange behavior");
+ }
+ line = lineParts[0];
if (line.startsWith("*.")) {
String data = line.substring(2);
if (data.contains("*") || data.contains("!")) {
}
return false;
}
+
}