Skip to content

Commit

Permalink
Added methods to check database type and date
Browse files Browse the repository at this point in the history
  • Loading branch information
renfei committed Dec 2, 2024
1 parent 0f20191 commit 289129d
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Or download the zip file in the warehouse and decompress it by yourself.
<dependency>
<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<dependency>
<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<name>ip2location</name>
<url>http://www.renfei.net</url>
<description>IP2Location Java Component enables applications to get info from IP address such as the visitor’s country, region, city, latitude, longitude, ZIP code, ISP name, domain name, time zone, connection speed, IDD code, area code, weather station code, weather station name, MCC, MNC, mobile brand name, elevation and usage type.</description>
Expand Down
114 changes: 75 additions & 39 deletions src/main/java/net/renfei/ip2location/IP2Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,55 @@ public IP2Location() {

}

interface FileLike {

interface Supplier {
FileLike open() throws IOException;

boolean isValid();
}

int read(byte[] buffer) throws IOException;

int read(byte b[], int off, int len) throws IOException;

void seek(long pos) throws IOException;

void close() throws IOException;
}

/**
* This function returns the package version.
*
* @return Package version
*/
public String GetPackageVersion() {
if (_MetaData == null) {
return "";
}
if (_MetaData.getDBType() == 0) {
return "";
} else {
return String.valueOf(_MetaData.getDBType());
}
}

/**
* This function returns the IP database version.
*
* @return IP database version
*/
public String GetDatabaseVersion() {
if (_MetaData == null) {
return "";
}
if (_MetaData.getDBYear() == 0) {
return "";
} else {
return "20" + _MetaData.getDBYear() + "." + _MetaData.getDBMonth() + "." + _MetaData.getDBDay();
}
}

/**
* This function can be used to pre-load the BIN file.
*
Expand Down Expand Up @@ -174,6 +223,7 @@ public boolean isValid() {
return DBPath.length() > 0;
}
};

LoadBIN();
}

Expand Down Expand Up @@ -221,23 +271,6 @@ public boolean isValid() {
LoadBIN();
}

interface FileLike {

interface Supplier {
FileLike open() throws IOException;

boolean isValid();
}

int read(byte[] buffer) throws IOException;

int read(byte b[], int off, int len) throws IOException;

void seek(long pos) throws IOException;

void close() throws IOException;
}

/**
* This function destroys the mapped bytes.
*/
Expand Down Expand Up @@ -456,7 +489,7 @@ public IPResult IPQuery(String IPAddress) throws IOException {
byte[] fullrow = null;

try {
if (IPAddress.length() == 0) {
if (IPAddress == null || IPAddress.length() == 0) {
record.status = "EMPTY_IP_ADDRESS";
return record;
}
Expand Down Expand Up @@ -588,7 +621,7 @@ public IPResult IPQuery(String IPAddress) throws IOException {
mydatabuffer.order(ByteOrder.LITTLE_ENDIAN);
} else {
row = new byte[rowlen];
System.arraycopy(fullrow, firstcol, row, 0, rowlen); // extract the actual row data
System.arraycopy(fullrow, firstcol, row, (int) 0, rowlen); // extract the actual row data
}

if (COUNTRY_ENABLED) {
Expand Down Expand Up @@ -774,8 +807,9 @@ private String[] expandIPV6(final String myIP, final int myiptype) {
String[] myarr = mymatch.replaceAll("^:+", "").replaceAll(":+$", "").split(":");

int len = myarr.length;
StringBuilder bf = new StringBuilder(32);
for (String unpadded : myarr) {
StringBuffer bf = new StringBuffer(32);
for (int x = 0; x < len; x++) {
String unpadded = myarr[x];
bf.append(padme.substring(unpadded.length())).append(unpadded); // safe padding for JDK 1.4
}
long mylong = new BigInteger(bf.toString(), 16).longValue();
Expand Down Expand Up @@ -815,22 +849,23 @@ private String[] expandIPV6(final String myIP, final int myiptype) {
String part1hex = Integer.toHexString(part1);
String part2hex = Integer.toHexString(part2);

String bf = v6part +
padme.substring(part1hex.length()) +
part1hex +
":" +
padme.substring(part2hex.length()) +
part2hex;
StringBuffer bf = new StringBuffer(v6part.length() + 9);
bf.append(v6part);
bf.append(padme.substring(part1hex.length()));
bf.append(part1hex);
bf.append(":");
bf.append(padme.substring(part2hex.length()));
bf.append(part2hex);

myIP2 = bf.toUpperCase();
myIP2 = bf.toString().toUpperCase();

String[] myarr = myIP2.split("::");

String[] leftside = myarr[0].split(":");

StringBuilder bf2 = new StringBuilder(40);
StringBuilder bf3 = new StringBuilder(40);
StringBuilder bf4 = new StringBuilder(40);
StringBuffer bf2 = new StringBuffer(40);
StringBuffer bf3 = new StringBuffer(40);
StringBuffer bf4 = new StringBuffer(40);

len = leftside.length;
int totalsegments = 0;
Expand Down Expand Up @@ -885,8 +920,9 @@ private String[] expandIPV6(final String myIP, final int myiptype) {
String[] myarr = mymatch.replaceAll("^:+", "").replaceAll(":+$", "").split(":");

int len = myarr.length;
StringBuilder bf = new StringBuilder(32);
for (String unpadded : myarr) {
StringBuffer bf = new StringBuffer(32);
for (int x = 0; x < len; x++) {
String unpadded = myarr[x];
bf.append(padme.substring(unpadded.length())).append(unpadded); // safe padding for JDK 1.4
}

Expand All @@ -908,9 +944,9 @@ private String[] expandIPV6(final String myIP, final int myiptype) {

String[] leftside = myarr[0].split(":");

StringBuilder bf2 = new StringBuilder(40);
StringBuilder bf3 = new StringBuilder(40);
StringBuilder bf4 = new StringBuilder(40);
StringBuffer bf2 = new StringBuffer(40);
StringBuffer bf3 = new StringBuffer(40);
StringBuffer bf4 = new StringBuffer(40);

int len = leftside.length;
int totalsegments = 0;
Expand Down Expand Up @@ -985,7 +1021,7 @@ private byte[] readRow(final long position, final long mylen, final ByteBuffer m
mybuffer.get(row, 0, (int) mylen);
} else {
filehandle.seek(position - 1);
filehandle.read(row, 0, (int) mylen);
filehandle.read(row, (int) 0, (int) mylen);
}
return row;
}
Expand Down Expand Up @@ -1026,7 +1062,7 @@ private BigInteger read128(final long position, final ByteBuffer mybuffer, final
private BigInteger read32Row(byte[] row, final int from) {
final int len = 4;
byte[] buf = new byte[len];
System.arraycopy(row, from, buf, 0, len);
System.arraycopy(row, from, buf, (int) 0, len);
reverse(buf);
return new BigInteger(1, buf);
}
Expand All @@ -1039,7 +1075,7 @@ private BigInteger read32(final long position, final ByteBuffer mybuffer, final
final int bsize = 4;
filehandle.seek(position - 1);
byte[] buf = new byte[bsize];
filehandle.read(buf, 0, bsize);
filehandle.read(buf, (int) 0, bsize);
reverse(buf);
return new BigInteger(1, buf);
}
Expand Down
63 changes: 30 additions & 33 deletions src/main/java/net/renfei/ip2location/IPResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class IPResult {
String as;
String status;
boolean delay = false;
String version = "Version 8.11.0";
String version = "Version 8.11.2";

IPResult(String ipstring) {
ip_address = ipstring;
Expand Down Expand Up @@ -293,38 +293,35 @@ public String getVersion() {
*
* @return the result in a formatted string.
*/
@Override
public String toString() {
return "IPResult{" +
"ip_address='" + ip_address + '\'' +
", country_short='" + country_short + '\'' +
", country_long='" + country_long + '\'' +
", region='" + region + '\'' +
", city='" + city + '\'' +
", isp='" + isp + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", domain='" + domain + '\'' +
", zipcode='" + zipcode + '\'' +
", netspeed='" + netspeed + '\'' +
", timezone='" + timezone + '\'' +
", iddcode='" + iddcode + '\'' +
", areacode='" + areacode + '\'' +
", weatherstationcode='" + weatherstationcode + '\'' +
", weatherstationname='" + weatherstationname + '\'' +
", mcc='" + mcc + '\'' +
", mnc='" + mnc + '\'' +
", mobilebrand='" + mobilebrand + '\'' +
", elevation=" + elevation +
", usagetype='" + usagetype + '\'' +
", addresstype='" + addresstype + '\'' +
", category='" + category + '\'' +
", district='" + district + '\'' +
", asn='" + asn + '\'' +
", as='" + as + '\'' +
", status='" + status + '\'' +
", delay=" + delay +
", version='" + version + '\'' +
'}';
String NL = System.getProperty("line.separator");
StringBuffer buf = new StringBuffer("IP2LocationRecord:" + NL);
buf.append("\tIP Address = " + ip_address + NL);
buf.append("\tCountry Short = " + country_short + NL);
buf.append("\tCountry Long = " + country_long + NL);
buf.append("\tRegion = " + region + NL);
buf.append("\tCity = " + city + NL);
buf.append("\tISP = " + isp + NL);
buf.append("\tLatitude = " + latitude + NL);
buf.append("\tLongitude = " + longitude + NL);
buf.append("\tDomain = " + domain + NL);
buf.append("\tZipCode = " + zipcode + NL);
buf.append("\tTimeZone = " + timezone + NL);
buf.append("\tNetSpeed = " + netspeed + NL);
buf.append("\tIDDCode = " + iddcode + NL);
buf.append("\tAreaCode = " + areacode + NL);
buf.append("\tWeatherStationCode = " + weatherstationcode + NL);
buf.append("\tWeatherStationName = " + weatherstationname + NL);
buf.append("\tMCC = " + mcc + NL);
buf.append("\tMNC = " + mnc + NL);
buf.append("\tMobileBrand = " + mobilebrand + NL);
buf.append("\tElevation = " + elevation + NL);
buf.append("\tUsageType = " + usagetype + NL);
buf.append("\tAddressType = " + addresstype + NL);
buf.append("\tCategory = " + category + NL);
buf.append("\tDistrict = " + district + NL);
buf.append("\tASN = " + asn + NL);
buf.append("\tAS = " + as + NL);
return buf.toString();
}
}

0 comments on commit 289129d

Please sign in to comment.