More socket leak prevention measures just in case

This commit is contained in:
Risto Lahtela 2020-05-13 10:38:06 +03:00
parent 1159f67f28
commit bc7e84677f

View File

@ -20,6 +20,7 @@ import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
@ -75,12 +76,16 @@ public class IP2CGeolocator implements Geolocator {
HttpURLConnection connection = null; HttpURLConnection connection = null;
try { try {
connection = (HttpURLConnection) new URL("http://ip2c.org/" + address).openConnection(); connection = (HttpURLConnection) new URL("http://ip2c.org/" + address).openConnection();
connection.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(1L));
connection.setReadTimeout((int) TimeUnit.MINUTES.toMillis(1L));
connection.setUseCaches(false); connection.setUseCaches(false);
connection.connect(); connection.connect();
try ( try (
InputStream in = connection.getInputStream() InputStream in = connection.getInputStream();
OutputStream out = connection.getOutputStream()
) { ) {
String answer = readAnswer(in); String answer = readAnswer(in);
out.close();
return resolveIP(answer); return resolveIP(answer);
} }
} finally { } finally {