@@ -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