diff --git a/IP.java b/IP.java index d77b909..d0b3a81 100644 --- a/IP.java +++ b/IP.java @@ -58,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); @@ -85,7 +99,7 @@ public static String[] find(String ip) { lock.unlock(); } - return new String(areaBytes, Charset.forName("UTF-8")).split("\t"); + return new String(areaBytes, Charset.forName("UTF-8")).split("\t", -1); } private static void watch() { @@ -166,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 3ecdf80..0ffc4ae 100644 --- a/IPExt.java +++ b/IPExt.java @@ -35,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("\\."); @@ -50,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; } } @@ -66,7 +80,7 @@ public static String[] find(String ip) { lock.unlock(); } - return new String(areaBytes, Charset.forName("UTF-8")).split("\t"); + return new String(areaBytes, Charset.forName("UTF-8")).split("\t", -1); } private static void watch() { @@ -154,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/