Skip to content

Commit 101ac11

Browse files
committed
update code
1 parent c6f2eb5 commit 101ac11

1 file changed

Lines changed: 63 additions & 37 deletions

File tree

‎IP.java‎

Lines changed: 63 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,85 @@
44
import java.nio.ByteBuffer;
55
import java.nio.ByteOrder;
66
import java.util.Arrays;
7+
import java.util.concurrent.Executors;
8+
import java.util.concurrent.TimeUnit;
9+
import java.util.concurrent.locks.ReentrantLock;
710

811
class IP {
912

1013
public static void main(String[] args){
11-
1214
IP.load("H:\\loveapp\\codebase\\17mon\\17monipdb.dat");
1315

1416
System.out.println(Arrays.toString(IP.find("8.8.8.8")));
15-
System.out.println(Arrays.toString(IP.find("118.28.38.58")));
1617
System.out.println(Arrays.toString(IP.find("255.255.255.255")));
1718
}
1819

20+
public static boolean enableFileWatch = false;
21+
1922
private static int offset;
2023
private static int[] index = new int[256];
21-
2224
private static ByteBuffer dataBuffer;
2325
private static ByteBuffer indexBuffer;
26+
private static Long lastModifyTime = 0L;
27+
private static File ipFile ;
28+
private static ReentrantLock lock = new ReentrantLock();
2429

2530
public static void load(String filename) {
26-
File ipFile = new File(filename);
31+
ipFile = new File(filename);
32+
load();
33+
if (enableFileWatch)
34+
{
35+
watch();
36+
}
37+
}
38+
39+
public static String[] find(String ip) {
40+
int ip_prefix_value = new Integer(ip.substring(0, ip.indexOf(".")));
41+
long ip2long_value = ip2long(ip);
42+
int start = index[ip_prefix_value];
43+
int max_comp_len = offset - 1028;
44+
long index_offset = -1;
45+
int index_length = -1;
46+
byte b = 0;
47+
for (start = start * 8 + 1024; start < max_comp_len; start += 8) {
48+
if (int2long(indexBuffer.getInt(start)) >= ip2long_value) {
49+
index_offset = bytesToLong(b, indexBuffer.get(start + 6), indexBuffer.get(start + 5), indexBuffer.get(start + 4));
50+
index_length = 0xFF & indexBuffer.get(start + 7);
51+
break;
52+
}
53+
}
54+
55+
lock.lock();
56+
dataBuffer.position(offset + (int) index_offset - 1024);
57+
byte[] areaBytes = new byte[index_length];
58+
dataBuffer.get(areaBytes, 0, index_length);
59+
lock.unlock();
60+
61+
return new String(areaBytes).split("\t");
62+
}
63+
64+
private static void watch() {
65+
66+
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
67+
@Override
68+
public void run() {
2769

28-
final int fileLength = Long.valueOf(ipFile.length()).intValue();
29-
dataBuffer = ByteBuffer.allocate(fileLength);
70+
long time = ipFile.lastModified();
71+
if (time > lastModifyTime) {
72+
lastModifyTime = time;
73+
load();
74+
}
75+
}
76+
}, 1000L, 5000L, TimeUnit.MILLISECONDS);
77+
}
78+
79+
private static void load() {
80+
lastModifyTime = ipFile.lastModified();
3081
FileInputStream fin = null;
3182
try {
83+
lock.lock();
84+
final int fileLength = Long.valueOf(ipFile.length()).intValue();
85+
dataBuffer = ByteBuffer.allocate(fileLength);
3286
fin = new FileInputStream(ipFile);
3387
int readBytesLength;
3488
byte[] chunk = new byte[4096];
@@ -50,49 +104,21 @@ public static void load(String filename) {
50104
}
51105
indexBuffer.order(ByteOrder.BIG_ENDIAN);
52106
} catch (IOException ioe) {
53-
//
54107
} finally {
55108
try {
56109
if (fin != null) {
57110
fin.close();
58111
}
59112
} catch (IOException e){
60-
//
61113
}
114+
lock.unlock();
62115
}
63116
}
64117

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-
93118
private static long bytesToLong(byte a, byte b, byte c, byte d) {
94119
return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
95120
}
121+
96122
private static int str2Ip(String ip) {
97123
String[] ss = ip.split("\\.");
98124
int a, b, c, d;
@@ -114,4 +140,4 @@ private static long int2long(int i) {
114140
}
115141
return l;
116142
}
117-
}
143+
}

0 commit comments

Comments
 (0)