diff --git a/IP.java b/IP.java index 548f0ed..d0b3a81 100644 --- a/IP.java +++ b/IP.java @@ -4,17 +4,40 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; +import java.nio.charset.Charset; +import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; class IP { + public static String randomIp() { + Random r = new Random(); + StringBuffer str = new StringBuffer(); + str.append(r.nextInt(1000000) % 255); + str.append("."); + str.append(r.nextInt(1000000) % 255); + str.append("."); + str.append(r.nextInt(1000000) % 255); + str.append("."); + str.append(0); + + return str.toString(); + } + public static void main(String[] args){ IP.load("H:\\loveapp\\codebase\\17mon\\17monipdb.dat"); - System.out.println(Arrays.toString(IP.find("8.8.8.8"))); - System.out.println(Arrays.toString(IP.find("255.255.255.255"))); + Long st = System.nanoTime(); + for (int i = 0; i < 1000000; i++) + { + IP.find(randomIp()); + } + Long et = System.nanoTime(); + System.out.println((et - st) / 1000 / 1000); + + System.out.println(Arrays.toString(IP.find("118.28.8.8"))); } public static boolean enableFileWatch = false; @@ -35,6 +58,20 @@ public static void load(String filename) { } } + public static void load(String filename, boolean strict) throws Exception { + ipFile = new File(filename); + if (strict) { + int contentLength = Long.valueOf(ipFile.length()).intValue(); + if (contentLength < 512 * 1024) { + throw new Exception("ip data file error."); + } + } + load(); + if (enableFileWatch) { + watch(); + } + } + public static String[] find(String ip) { int ip_prefix_value = new Integer(ip.substring(0, ip.indexOf("."))); long ip2long_value = ip2long(ip); @@ -52,6 +89,7 @@ public static String[] find(String ip) { } byte[] areaBytes; + lock.lock(); try { dataBuffer.position(offset + (int) index_offset - 1024); @@ -61,7 +99,7 @@ public static String[] find(String ip) { lock.unlock(); } - return new String(areaBytes).split("\t"); + return new String(areaBytes, Charset.forName("UTF-8")).split("\t", -1); } private static void watch() { @@ -104,13 +142,15 @@ private static void load() { } indexBuffer.order(ByteOrder.BIG_ENDIAN); } catch (IOException ioe) { - + ioe.printStackTrace(); } finally { try { if (fin != null) { fin.close(); } - } catch (IOException e){} + } catch (IOException e){ + e.printStackTrace(); + } lock.unlock(); } } @@ -140,4 +180,4 @@ private static long int2long(int i) { } return l; } -} \ No newline at end of file +} diff --git a/IPExt.java b/IPExt.java index e0de74e..0ffc4ae 100644 --- a/IPExt.java +++ b/IPExt.java @@ -4,6 +4,7 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; +import java.nio.charset.Charset; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; @@ -34,6 +35,20 @@ public static void load(String filename) { watch(); } } + + public static void load(String filename, boolean strict) throws Exception { + ipFile = new File(filename); + if (strict) { + int contentLength = Long.valueOf(ipFile.length()).intValue(); + if (contentLength < 512 * 1024) { + throw new Exception("ip data file error."); + } + } + load(); + if (enableFileWatch) { + watch(); + } + } public static String[] find(String ip) { String[] ips = ip.split("\\."); @@ -49,7 +64,7 @@ public static String[] find(String ip) { tmpInt = int2long(indexBuffer.getInt(start)); if (tmpInt >= ip2long_value) { index_offset = bytesToLong(b, indexBuffer.get(start + 6), indexBuffer.get(start + 5), indexBuffer.get(start + 4)); - index_length = (0xFF & indexBuffer.get(start + 7) << 8) + (0xFF & indexBuffer.get(start + 8)); + index_length = ((0xFF & indexBuffer.get(start + 7)) << 8) + (0xFF & indexBuffer.get(start + 8)); break; } } @@ -65,7 +80,7 @@ public static String[] find(String ip) { lock.unlock(); } - return new String(areaBytes).split("\t"); + return new String(areaBytes, Charset.forName("UTF-8")).split("\t", -1); } private static void watch() { @@ -153,4 +168,4 @@ private static long int2long(int i) { } return l; } -} \ No newline at end of file +} diff --git a/README.md b/README.md index 65a91ed..59e71a0 100644 --- a/README.md +++ b/README.md @@ -14,4 +14,11 @@ IP.find("8.8.8.8");//返回字符串数组["GOOGLE","GOOGLE"] ``` -IPExt的用法与IP的用法相同,只是用来解析datx格式文件。 \ No newline at end of file +IPExt的用法与IP的用法相同,只是用来解析datx格式文件。 + +##特别说明 +```java +IP.java 类仅适用于免费版dat与收费版每周每日版本的dat文件; +IPExt.java 类适用于收费版每日版本的datx文件; +``` +区县库代码请查看 https://github.com/17mon/quxianku/