Skip to content

Commit 760909c

Browse files
committed
update code
1 parent 79231cc commit 760909c

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

IP.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,23 @@ public static String[] find(String ip) {
5151
}
5252
}
5353

54+
byte[] areaBytes;
5455
lock.lock();
55-
dataBuffer.position(offset + (int) index_offset - 1024);
56-
byte[] areaBytes = new byte[index_length];
57-
dataBuffer.get(areaBytes, 0, index_length);
58-
lock.unlock();
56+
try {
57+
dataBuffer.position(offset + (int) index_offset - 1024);
58+
areaBytes = new byte[index_length];
59+
dataBuffer.get(areaBytes, 0, index_length);
60+
} finally {
61+
lock.unlock();
62+
}
5963

6064
return new String(areaBytes).split("\t");
6165
}
6266

6367
private static void watch() {
64-
6568
Executors.newScheduledThreadPool(1).scheduleAtFixedRate(new Runnable() {
6669
@Override
6770
public void run() {
68-
6971
long time = ipFile.lastModified();
7072
if (time > lastModifyTime) {
7173
lastModifyTime = time;
@@ -78,10 +80,9 @@ public void run() {
7880
private static void load() {
7981
lastModifyTime = ipFile.lastModified();
8082
FileInputStream fin = null;
83+
lock.lock();
8184
try {
82-
lock.lock();
83-
final int fileLength = Long.valueOf(ipFile.length()).intValue();
84-
dataBuffer = ByteBuffer.allocate(fileLength);
85+
dataBuffer = ByteBuffer.allocate(Long.valueOf(ipFile.length()).intValue());
8586
fin = new FileInputStream(ipFile);
8687
int readBytesLength;
8788
byte[] chunk = new byte[4096];
@@ -103,13 +104,13 @@ private static void load() {
103104
}
104105
indexBuffer.order(ByteOrder.BIG_ENDIAN);
105106
} catch (IOException ioe) {
107+
106108
} finally {
107109
try {
108110
if (fin != null) {
109111
fin.close();
110112
}
111-
} catch (IOException e){
112-
}
113+
} catch (IOException e){}
113114
lock.unlock();
114115
}
115116
}

IPExt.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,13 @@ public static String[] find(String ip) {
5757
byte[] areaBytes;
5858

5959
lock.lock();
60-
dataBuffer.position(offset + (int) index_offset - 262144);
61-
areaBytes = new byte[index_length];
62-
dataBuffer.get(areaBytes, 0, index_length);
63-
lock.unlock();
60+
try {
61+
dataBuffer.position(offset + (int) index_offset - 262144);
62+
areaBytes = new byte[index_length];
63+
dataBuffer.get(areaBytes, 0, index_length);
64+
} finally {
65+
lock.unlock();
66+
}
6467

6568
return new String(areaBytes).split("\t");
6669
}
@@ -78,23 +81,26 @@ public void run() {
7881
}
7982

8083
private static void load() {
81-
lock.lock();
8284
lastModifyTime = ipFile.lastModified();
83-
dataBuffer = ByteBuffer.wrap(getBytesByFile(ipFile));
84-
dataBuffer.position(0);
85-
offset = dataBuffer.getInt(); // indexLength
86-
byte[] indexBytes = new byte[offset];
87-
dataBuffer.get(indexBytes, 0, offset - 4);
88-
indexBuffer = ByteBuffer.wrap(indexBytes);
89-
indexBuffer.order(ByteOrder.LITTLE_ENDIAN);
90-
91-
for (int i = 0; i < 256; i++) {
92-
for (int j = 0; j < 256; j++) {
93-
index[i * 256 + j] = indexBuffer.getInt();
85+
lock.lock();
86+
try {
87+
dataBuffer = ByteBuffer.wrap(getBytesByFile(ipFile));
88+
dataBuffer.position(0);
89+
offset = dataBuffer.getInt(); // indexLength
90+
byte[] indexBytes = new byte[offset];
91+
dataBuffer.get(indexBytes, 0, offset - 4);
92+
indexBuffer = ByteBuffer.wrap(indexBytes);
93+
indexBuffer.order(ByteOrder.LITTLE_ENDIAN);
94+
95+
for (int i = 0; i < 256; i++) {
96+
for (int j = 0; j < 256; j++) {
97+
index[i * 256 + j] = indexBuffer.getInt();
98+
}
9499
}
100+
indexBuffer.order(ByteOrder.BIG_ENDIAN);
101+
} finally {
102+
lock.unlock();
95103
}
96-
indexBuffer.order(ByteOrder.BIG_ENDIAN);
97-
lock.unlock();
98104
}
99105

100106
private static byte[] getBytesByFile(File file) {

0 commit comments

Comments
 (0)