-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathIpAreaRange.java
More file actions
109 lines (96 loc) · 3.33 KB
/
IpAreaRange.java
File metadata and controls
109 lines (96 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import com.yoshino.util.datxip.*;
public class IpAreaRange {
private String ipRangeFileName;
private SizeBalancedTree ipRangeTree_S;
private SizeBalancedTree ipRangeTree_T;
private Map<String,Integer> map;
private Map<String,Integer> countOfRegion;
private City city;
public IpAreaRange() {
//this.ipRangeFileName = "" ;
//this.ipRangeTree_S = new SizeBalancedTree();
//this.ipRangeTree_T = new SizeBalancedTree();
//map = new HashMap<String, Integer>();
//map = Collections.synchronizedMap(new HashMap<String, Integer>());
try{
this.map = new ConcurrentHashMap<String, Integer>();
ValueComparator bvc = new ValueComparator(map);
//countOfRegion = new TreeMap<String,Integer>(bvc);
this.countOfRegion = Collections.synchronizedMap(new TreeMap<String, Integer>(bvc));
this.city = new City(FilePath.ipFile);
}
catch (Exception ioex){
ioex.printStackTrace();
}
}
public IpAreaRange(String fileName) {
this.ipRangeFileName = fileName;
this.ipRangeTree_S = new SizeBalancedTree<NodeOfIp>();
this.ipRangeTree_T = new SizeBalancedTree<NodeOfIp>();
this.build();
}
/**
* 建立IP段SBT
*
*/
public void build() {
try {
File file = new File(ipRangeFileName);
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));
BufferedReader reader = new BufferedReader(new InputStreamReader(fis, "utf-8"), 5 * 1024 * 1024);
String line = "";
int value = 0;
String area = "";
while ((line = reader.readLine()) != null) {
if (line.charAt(1) >= '0' && line.charAt(1) <= '9') {
String[] tempIpRange = line.split(" ");
String tempIp_S = tempIpRange[0];
String tempIp_T = tempIpRange[1];
NodeOfIp nodeIP_S = new NodeOfIp(IpV4Util.ipToInt(tempIp_S),area);
NodeOfIp nodeIP_T = new NodeOfIp(IpV4Util.ipToInt(tempIp_T),area);
ipRangeTree_S.insert(nodeIP_S);
ipRangeTree_T.insert(nodeIP_T);
}
else {
area = line;
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
/**
* 传入IP地址,搜索IP数据库的结果,返回省份信息
*
* @param checkIp String
* @return ans
*/
public String search(String checkIp) {
String region = "null";
try {
region = Arrays.toString(this.city.find(checkIp));
insertRegion(region);
}
catch (Exception e) {
System.out.println("Err " + e + " " + checkIp);
}
return region;
}
public void insertRegion(String region) {
if (map.containsKey(region)) {
Integer times = map.get(region);
map.put(region, times+1);
}
else {
map.put(region, 1);
}
}
public Map<String, Integer> getRegionCount() {
countOfRegion.putAll(map);
return this.countOfRegion;
}
}