Skip to content

Commit d26dd04

Browse files
author
17mon
committed
IP.java
17mon ipdb java parser
1 parent 4f485d8 commit d26dd04

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

IP.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import java.io.File;
2+
import java.io.FileInputStream;
3+
import java.io.IOException;
4+
import java.nio.ByteBuffer;
5+
import java.nio.ByteOrder;
6+
import java.util.Arrays;
7+
8+
class IP {
9+
10+
public static void main(String[] args){
11+
12+
IP.load("H:\\loveapp\\codebase\\17mon\\17monipdb.dat");
13+
14+
System.out.println(Arrays.toString(IP.find("8.8.8.8")));
15+
System.out.println(Arrays.toString(IP.find("118.28.38.58")));
16+
System.out.println(Arrays.toString(IP.find("255.255.255.255")));
17+
}
18+
19+
private static int offset;
20+
private static int[] index = new int[256];
21+
22+
private static ByteBuffer dataBuffer;
23+
private static ByteBuffer indexBuffer;
24+
25+
public static void load(String filename) {
26+
File ipFile = new File(filename);
27+
28+
final int fileLength = Long.valueOf(ipFile.length()).intValue();
29+
dataBuffer = ByteBuffer.allocate(fileLength);
30+
FileInputStream fin = null;
31+
try {
32+
fin = new FileInputStream(ipFile);
33+
int readBytesLength;
34+
byte[] chunk = new byte[4096];
35+
while (fin.available() > 0) {
36+
readBytesLength = fin.read(chunk);
37+
dataBuffer.put(chunk, 0, readBytesLength);
38+
}
39+
dataBuffer.position(0);
40+
int indexLength = dataBuffer.getInt();
41+
byte[] indexBytes = new byte[indexLength];
42+
dataBuffer.get(indexBytes, 0, indexLength - 4);
43+
indexBuffer = ByteBuffer.wrap(indexBytes);
44+
indexBuffer.order(ByteOrder.LITTLE_ENDIAN);
45+
offset = indexLength;
46+
47+
int loop = 0;
48+
while (loop++ < 256) {
49+
index[loop - 1] = indexBuffer.getInt();
50+
}
51+
indexBuffer.order(ByteOrder.BIG_ENDIAN);
52+
} catch (IOException ioe) {
53+
//
54+
} finally {
55+
try {
56+
if (fin != null) {
57+
fin.close();
58+
}
59+
} catch (IOException e){
60+
//
61+
}
62+
}
63+
}
64+
65+
public static String[] find(String ip) {
66+
int ip_prefix_value = new Integer(ip.substring(0, ip.indexOf(".")));
67+
long ip2long_value = ip2long(ip);
68+
int start = index[ip_prefix_value];
69+
int max_comp_len = offset - 1028;
70+
long tmpInt;
71+
long index_offset = -1;
72+
int index_length = -1;
73+
byte b = 0;
74+
for (start = start * 8 + 1024; start < max_comp_len; start += 8) {
75+
tmpInt = int2long(indexBuffer.getInt(start));
76+
if (tmpInt >= ip2long_value) {
77+
index_offset = bytesToLong(b, indexBuffer.get(start + 6), indexBuffer.get(start + 5), indexBuffer.get(start + 4));
78+
index_length = 0xFF & indexBuffer.get(start + 7);
79+
break;
80+
}
81+
}
82+
83+
byte[] areaBytes;
84+
synchronized (dataBuffer) {
85+
dataBuffer.position(offset + (int) index_offset - 1024);
86+
areaBytes = new byte[index_length];
87+
dataBuffer.get(areaBytes, 0, index_length);
88+
}
89+
90+
return new String(areaBytes).split("\t");
91+
}
92+
93+
private static long bytesToLong(byte a, byte b, byte c, byte d) {
94+
return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
95+
}
96+
private static int str2Ip(String ip) {
97+
String[] ss = ip.split("\\.");
98+
int a, b, c, d;
99+
a = Integer.parseInt(ss[0]);
100+
b = Integer.parseInt(ss[1]);
101+
c = Integer.parseInt(ss[2]);
102+
d = Integer.parseInt(ss[3]);
103+
return (a << 24) | (b << 16) | (c << 8) | d;
104+
}
105+
106+
private static long ip2long(String ip) {
107+
return int2long(str2Ip(ip));
108+
}
109+
110+
private static long int2long(int i) {
111+
long l = i & 0x7fffffffL;
112+
if (i < 0) {
113+
l |= 0x080000000L;
114+
}
115+
return l;
116+
}
117+
}

0 commit comments

Comments
 (0)