Skip to content

Commit 8010e01

Browse files
committed
safer handling of resources
1 parent 517b16d commit 8010e01

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

app/src/main/java/net/kollnig/missioncontrol/details/CountriesFragment.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,29 +97,30 @@ public synchronized Map<String, Integer> getHostCountriesCount(int uid) {
9797
DatabaseHelper dh = DatabaseHelper.getInstance(getContext());
9898
try (Cursor cursor = dh.getHosts(uid)) {
9999
InputStream database = context.getAssets().open("GeoLite2-Country.mmdb");
100-
DatabaseReader reader = new DatabaseReader.Builder(database).withCache(new CHMCache()).build();
101-
102-
if (cursor.moveToFirst()) {
103-
do {
104-
String host = cursor.getString(cursor.getColumnIndexOrThrow("daddr"));
105-
if (findTracker(host) == null)
106-
continue;
107-
108-
InetAddress ipAddress = InetAddress.getByName(host);
109-
CountryResponse response = reader.country(ipAddress);
110-
111-
Country country = response.getCountry();
112-
String code = country.getIsoCode();
113-
if (code == null)
114-
continue;
115-
116-
Integer count = countryToCount.get(code);
117-
if (count == null) {
118-
countryToCount.put(code, 1);
119-
} else {
120-
countryToCount.put(code, count + 1);
121-
}
122-
} while (cursor.moveToNext());
100+
try (DatabaseReader reader = new DatabaseReader.Builder(database).withCache(new CHMCache()).build()) {
101+
102+
if (cursor.moveToFirst()) {
103+
do {
104+
String host = cursor.getString(cursor.getColumnIndexOrThrow("daddr"));
105+
if (findTracker(host) == null)
106+
continue;
107+
108+
InetAddress ipAddress = InetAddress.getByName(host);
109+
CountryResponse response = reader.country(ipAddress);
110+
111+
Country country = response.getCountry();
112+
String code = country.getIsoCode();
113+
if (code == null)
114+
continue;
115+
116+
Integer count = countryToCount.get(code);
117+
if (count == null) {
118+
countryToCount.put(code, 1);
119+
} else {
120+
countryToCount.put(code, count + 1);
121+
}
122+
} while (cursor.moveToNext());
123+
}
123124
}
124125
} catch (IOException | GeoIp2Exception e) {
125126
e.printStackTrace();

0 commit comments

Comments
 (0)