Skip to content

Commit 157cc39

Browse files
Added methods to check database type and date
1 parent 4e1b317 commit 157cc39

9 files changed

Lines changed: 2622 additions & 2585 deletions

File tree

com/ip2location/Country.java

Lines changed: 80 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,81 @@
1-
package com.ip2location;
2-
3-
import java.io.FileReader;
4-
import java.io.IOException;
5-
import java.util.ArrayList;
6-
import java.util.HashMap;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.io.File;
10-
import com.opencsv.*;
11-
import com.opencsv.exceptions.*;
12-
13-
/**
14-
* This class parses country information CSV and returns the country information.
15-
* <p>
16-
* Copyright (c) 2002-2023 IP2Location.com
17-
* <p>
18-
*
19-
* @author IP2Location.com
20-
* @version 8.11.2
21-
*/
22-
public class Country {
23-
private final Map<String, Map<String, String>> records = new HashMap<>();
24-
25-
/**
26-
* This constructor reads the country information CSV and store the parsed info.
27-
*
28-
* @param CSVFile The full path to the country information CSV file.
29-
*/
30-
public Country(String CSVFile) throws IOException, CsvValidationException {
31-
Map<String, String> line;
32-
File file = new File(CSVFile);
33-
if (!file.exists()) {
34-
throw new IOException("The CSV file '" + CSVFile + "' is not found.");
35-
}
36-
FileReader fr = new FileReader(file);
37-
if (fr.read() == -1) {
38-
throw new IOException("Unable to read '" + CSVFile + "'.");
39-
}
40-
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file));
41-
while ((line = reader.readMap()) != null) {
42-
if (line.containsKey("country_code")) {
43-
records.put(line.get("country_code"), line);
44-
} else {
45-
throw new IOException("Invalid country information CSV file.");
46-
}
47-
}
48-
}
49-
50-
/**
51-
* This function gets the country information for the supplied country code.
52-
*
53-
* @param CountryCode ISO-3166 country code
54-
* @return Map
55-
*/
56-
public Map<String, String> GetCountryInfo(String CountryCode) throws IOException {
57-
if (records.isEmpty()) {
58-
throw new IOException("No record available.");
59-
} else {
60-
return records.getOrDefault(CountryCode, null);
61-
}
62-
}
63-
64-
/**
65-
* This function gets the country information for all countries.
66-
*
67-
* @return List
68-
*/
69-
public List<Map<String, String>> GetCountryInfo() throws IOException {
70-
List<Map<String, String>> results = new ArrayList<>();
71-
if (records.isEmpty()) {
72-
throw new IOException("No record available.");
73-
} else {
74-
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) {
75-
results.add(entry.getValue());
76-
}
77-
}
78-
return results;
79-
}
80-
1+
package com.ip2location;
2+
3+
import java.io.FileReader;
4+
import java.io.IOException;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.io.File;
10+
import com.opencsv.*;
11+
import com.opencsv.exceptions.*;
12+
13+
/**
14+
* This class parses country information CSV and returns the country information.
15+
* <p>
16+
* Copyright (c) 2002-2024 IP2Location.com
17+
* <p>
18+
*
19+
* @author IP2Location.com
20+
* @version 8.12.0
21+
*/
22+
public class Country {
23+
private final Map<String, Map<String, String>> records = new HashMap<>();
24+
25+
/**
26+
* This constructor reads the country information CSV and store the parsed info.
27+
*
28+
* @param CSVFile The full path to the country information CSV file.
29+
*/
30+
public Country(String CSVFile) throws IOException, CsvValidationException {
31+
Map<String, String> line;
32+
File file = new File(CSVFile);
33+
if (!file.exists()) {
34+
throw new IOException("The CSV file '" + CSVFile + "' is not found.");
35+
}
36+
FileReader fr = new FileReader(file);
37+
if (fr.read() == -1) {
38+
throw new IOException("Unable to read '" + CSVFile + "'.");
39+
}
40+
CSVReaderHeaderAware reader = new CSVReaderHeaderAware(new FileReader(file));
41+
while ((line = reader.readMap()) != null) {
42+
if (line.containsKey("country_code")) {
43+
records.put(line.get("country_code"), line);
44+
} else {
45+
throw new IOException("Invalid country information CSV file.");
46+
}
47+
}
48+
}
49+
50+
/**
51+
* This function gets the country information for the supplied country code.
52+
*
53+
* @param CountryCode ISO-3166 country code
54+
* @return Map
55+
*/
56+
public Map<String, String> GetCountryInfo(String CountryCode) throws IOException {
57+
if (records.isEmpty()) {
58+
throw new IOException("No record available.");
59+
} else {
60+
return records.getOrDefault(CountryCode, null);
61+
}
62+
}
63+
64+
/**
65+
* This function gets the country information for all countries.
66+
*
67+
* @return List
68+
*/
69+
public List<Map<String, String>> GetCountryInfo() throws IOException {
70+
List<Map<String, String>> results = new ArrayList<>();
71+
if (records.isEmpty()) {
72+
throw new IOException("No record available.");
73+
} else {
74+
for (Map.Entry<String, Map<String, String>> entry : records.entrySet()) {
75+
results.add(entry.getValue());
76+
}
77+
}
78+
return results;
79+
}
80+
8181
}

com/ip2location/Http.java

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
package com.ip2location;
2-
3-
import java.io.BufferedReader;
4-
import java.io.InputStreamReader;
5-
import java.net.HttpURLConnection;
6-
import java.net.URL;
7-
8-
class Http {
9-
public static String get(URL url) {
10-
try {
11-
java.lang.System.setProperty("https.protocols", "TLSv1.2");
12-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
13-
conn.setRequestMethod("GET");
14-
conn.setRequestProperty("Accept", "application/json");
15-
16-
if (conn.getResponseCode() != 200) {
17-
return ("Failed : HTTP error code : " + conn.getResponseCode());
18-
}
19-
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
20-
21-
String output;
22-
StringBuilder resultFromHttp = new StringBuilder();
23-
while ((output = br.readLine()) != null) {
24-
resultFromHttp.append(output).append("\n");
25-
}
26-
27-
br.close();
28-
conn.disconnect();
29-
return resultFromHttp.toString();
30-
} catch (Exception e) {
31-
throw new RuntimeException(e);
32-
}
33-
}
34-
35-
}
1+
package com.ip2location;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
class Http {
9+
public static String get(URL url) {
10+
try {
11+
java.lang.System.setProperty("https.protocols", "TLSv1.2");
12+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
13+
conn.setRequestMethod("GET");
14+
conn.setRequestProperty("Accept", "application/json");
15+
16+
if (conn.getResponseCode() != 200) {
17+
return ("Failed : HTTP error code : " + conn.getResponseCode());
18+
}
19+
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
20+
21+
String output;
22+
StringBuilder resultFromHttp = new StringBuilder();
23+
while ((output = br.readLine()) != null) {
24+
resultFromHttp.append(output).append("\n");
25+
}
26+
27+
br.close();
28+
conn.disconnect();
29+
return resultFromHttp.toString();
30+
} catch (Exception e) {
31+
throw new RuntimeException(e);
32+
}
33+
}
34+
35+
}

0 commit comments

Comments
 (0)