Repository navigation
Expand file tree
/
Copy pathIpListImpl.java
More file actions
111 lines (97 loc) · 2.96 KB
/
Copy pathIpListImpl.java
File metadata and controls
111 lines (97 loc) · 2.96 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
109
110
111
package file;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
*
* Created by zhengyong on 17/4/18.
*/
public class IpListImpl implements IpList {
private static final Logger logger = LoggerFactory.getLogger(IpListImpl.class);
/**
* 根据数据量预算空间初始化Set大小
*/
private static Set<Long> set = Collections.synchronizedSet(new HashSet<Long>(100000));
/**
* 构造函数,初始化加载指定路径ip地址
*
* @param filePath ip地址文件路径
*/
public IpListImpl(String filePath){
initIpToCache(filePath);
}
/**
* 初始化加载IP地址到内存(假设一行文件一个ip地址)
*
* @param filePath 文件路径
*/
private void initIpToCache(String filePath) {
FileReader fr = null;
BufferedReader buf = null;
try {
fr = new FileReader(filePath);
buf = new BufferedReader(fr);
String line;
while ((line = buf.readLine()) != null) {
if (StringUtils.isNotBlank(line)) {
Long ipLong = transferIpToLong(line);
if (ipLong != null) {
set.add(ipLong);
}
}
}
} catch (Exception e) {
logger.error("init load ip error", e);
} finally {
try {
if (fr != null) {
buf.close();
}
if (buf != null) {
buf.close();
}
} catch (IOException e) {
logger.error("close buf error", e);
}
}
}
@Override
public boolean isInList(String ip) {
long ipLong = transferIpToLong(ip);
return set.contains(ipLong);
}
/**
* 把ip地址字符串转为long型,节约空间
*
* @param ipString ip地址字符串
* @return long型ip地址值
*/
public Long transferIpToLong(String ipString) {
if (StringUtils.isBlank(ipString)) {
return null;
}
ipString = ipString.trim();
String[] ipGroup = ipString.split("\\.");
if (ipGroup.length != 4) {
return null;
}
Long result = 0L;
result += Long.parseLong(ipGroup[0]) << 24;
result += Long.parseLong(ipGroup[1]) << 16;
result += Long.parseLong(ipGroup[2]) << 8;
result += Long.parseLong(ipGroup[3]);
return result;
}
public static void main(String[] args) {
String ip = "172.168.43.112";
IpListImpl ipList = new IpListImpl("/Users/zhengyong/ip.txt");
boolean result = ipList.isInList(ip);
logger.info("the ip address={}, result={}", ip, result);
}
}