我试图执行基于时间间隔的搜索,我从文件加载并试图找到我的ip地址所在的间隔。以下是我的代码。此代码只适用于很长时间,但不适用于整数版本不是很长的IP地址。TreeMap floorEntry函数不能使用整数值(Treemap定义为long)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class RangeBasedSearchAsn {
public static class AsnInfo {
private long asn;
private String ipSubnet;
private String isp;
@Override
public String toString() {
return "Here are the details:\n"
+ this.asn + " " + this.ipSubnet + " " + this.isp ;
}
public AsnInfo(long asn, String ipSubnet, String isp) {
this.asn = asn;
this.ipSubnet = ipSubnet;
this.isp = isp;
}
public long getAsn() {
return asn;
}
public void setAsn(long asn) {
this.asn = asn;
}
public String getIpSubnet() {
return ipSubnet;
}
public void setIpSubnet(String ipSubnet) {
this.ipSubnet = ipSubnet;
}
public String getIsp() {
return isp;
}
public void setIsp(String isp) {
this.isp = isp;
}
}
public static class Range {
private long upper;
private AsnInfo asnInfo;
public Range(long upper, AsnInfo value) {
this.upper = upper;
this.asnInfo = value;
}
public long getUpper() {
return upper;
}
public void setUpper(long upper) {
this.upper = upper;
}
public AsnInfo getValue() {
return asnInfo;
}
public void setValue(AsnInfo value) {
this.asnInfo = value;
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
long key = 848163455L;
NavigableMap<Long, Range> asnTreeMap = new TreeMap<>();
System.out.println(System.currentTimeMillis());
System.out.println("Loading isp Map.");
FileInputStream inputStream = null;
Scanner sc = null;
try {
inputStream = new FileInputStream("C:\\Talend\\TalendTestArea\\rbl_ipv4_zone.txt");
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
String line = sc.nextLine();
StringTokenizer st = new StringTokenizer(line, ";");
while (st.hasMoreTokens() && st.countTokens() == 7) {
st.nextToken();
st.nextToken();
long token1 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token1);
long token2 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token2);
long token3 = Long.parseLong(st.nextToken());
System.out.println("here is token1:" + token3);
asnTreeMap.put(token1, new Range(token2, new AsnInfo(token3,st.nextToken(),st.nextToken())));
}
}
if (sc.ioException() != null) {
throw sc.ioException();
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (sc != null) {
sc.close();
}
}
System.out.println("Loading Over.");
System.out.println(System.currentTimeMillis());
System.out.println("Starting Lookup.");
long[] ips = {30503936L};
for(int i = 0 ; i < ips.length;i++){
System.out.println(asnTreeMap.size());
Map.Entry<Long, Range> entry = asnTreeMap.floorEntry(ips[i]);
if (entry == null) {
System.out.println("Value not valid");
} else if (key <= entry.getValue().upper) {
System.out.println("Carrier = " + entry.getValue().asnInfo.toString() + "\n");
} else {
System.out.println("Not found");
}
System.out.println(System.currentTimeMillis());
}
}
}
- 下面是输出运行:1432262970924加载ISP地图。正在加载 。 1432262975089开始查找。 540772未找到 1432262975089 \ n BUILD SUCCESSFUL(总时间:4秒)
请清理你的代码 - 发布*最小*,完整和可验证的例子。用一半*注释出来的例子*不是*最小*,我不打算阅读或试图理解它。 –