|
| 1 | +import java.io.File; |
| 2 | +import java.io.FileInputStream; |
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | +import java.nio.ByteOrder; |
| 6 | +import java.nio.charset.Charset; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.concurrent.locks.ReentrantLock; |
| 9 | + |
| 10 | +public class IPCounty { |
| 11 | + private static int offset; |
| 12 | + private static int[] index = new int[256]; |
| 13 | + private static ByteBuffer dataBuffer; |
| 14 | + private static ByteBuffer indexBuffer; |
| 15 | + private static File ipFile ; |
| 16 | + private static ReentrantLock lock; |
| 17 | + |
| 18 | + static { |
| 19 | + lock = new ReentrantLock(); |
| 20 | + } |
| 21 | + |
| 22 | + public static void main(String[] args) throws Exception { |
| 23 | + load("C:\\lovebizhi\\tiantexin\\framework\\library\\ip\\quxian.dat"); |
| 24 | + System.out.println(Arrays.toString(find("222.180.195.194"))); |
| 25 | + Long st = System.nanoTime(); |
| 26 | + for (int i = 0; i < 10000; i++) |
| 27 | + { |
| 28 | + try { |
| 29 | + find("222.180.195.194"); |
| 30 | +// |
| 31 | + } catch (Exception e) { |
| 32 | + e.printStackTrace(); |
| 33 | + } |
| 34 | + } |
| 35 | + Long et = System.nanoTime(); |
| 36 | + System.out.println((et - st) / 1000 / 1000); |
| 37 | + } |
| 38 | + |
| 39 | + public static void load(String name) { |
| 40 | + ipFile = new File(name); |
| 41 | + load(); |
| 42 | + } |
| 43 | + |
| 44 | + private static void load() { |
| 45 | + FileInputStream fin = null; |
| 46 | + lock.lock(); |
| 47 | + try { |
| 48 | + dataBuffer = ByteBuffer.allocate(Long.valueOf(ipFile.length()).intValue()); |
| 49 | + fin = new FileInputStream(ipFile); |
| 50 | + int readBytesLength; |
| 51 | + byte[] chunk = new byte[4096]; |
| 52 | + while (fin.available() > 0) { |
| 53 | + readBytesLength = fin.read(chunk); |
| 54 | + dataBuffer.put(chunk, 0, readBytesLength); |
| 55 | + } |
| 56 | + dataBuffer.position(0); |
| 57 | + int indexLength = dataBuffer.getInt(); |
| 58 | + byte[] indexBytes = new byte[indexLength]; |
| 59 | + dataBuffer.get(indexBytes, 0, indexLength - 4); |
| 60 | + indexBuffer = ByteBuffer.wrap(indexBytes); |
| 61 | + indexBuffer.order(ByteOrder.LITTLE_ENDIAN); |
| 62 | + offset = indexLength; |
| 63 | + |
| 64 | + int loop = 0; |
| 65 | + while (loop++ < 256) { |
| 66 | + index[loop - 1] = indexBuffer.getInt(); |
| 67 | + } |
| 68 | + indexBuffer.order(ByteOrder.BIG_ENDIAN); |
| 69 | + } catch (IOException ioe) { |
| 70 | + ioe.printStackTrace(); |
| 71 | + } finally { |
| 72 | + try { |
| 73 | + if (fin != null) { |
| 74 | + fin.close(); |
| 75 | + } |
| 76 | + } catch (IOException e){ |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + lock.unlock(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public static String[] find(String ip) throws Exception { |
| 84 | + int ip_prefix_value = new Integer(ip.substring(0, ip.indexOf("."))); |
| 85 | + long ip2long_value = ip2long(ip); |
| 86 | + int start = index[ip_prefix_value]; |
| 87 | + int max_comp_len = offset - 1028; |
| 88 | + long index_offset = -1; |
| 89 | + int index_length = -1; |
| 90 | + byte b = 0; |
| 91 | + for (start = start * 12 + 1024; start < max_comp_len; start += 12) { |
| 92 | + if (indexBuffer.getInt(start) <= ip2long_value) { |
| 93 | + if (int2long(indexBuffer.getInt(start + 4)) >= ip2long_value) { |
| 94 | + index_offset = bytesToLong(b, indexBuffer.get(start + 10), indexBuffer.get(start + 9), indexBuffer.get(start + 8)); |
| 95 | + index_length = 0xFF & indexBuffer.get(start + 11); |
| 96 | + break; |
| 97 | + } |
| 98 | + } else { |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if (index_offset == -1 && index_length == -1) { |
| 104 | + throw new Exception("IP Data Not Found."); |
| 105 | + } |
| 106 | + |
| 107 | + byte[] areaBytes; |
| 108 | + lock.lock(); |
| 109 | + try { |
| 110 | + dataBuffer.position(offset + (int) index_offset - 1024); |
| 111 | + areaBytes = new byte[index_length]; |
| 112 | + dataBuffer.get(areaBytes, 0, index_length); |
| 113 | + } finally { |
| 114 | + lock.unlock(); |
| 115 | + } |
| 116 | + |
| 117 | + return new String(areaBytes, Charset.forName("UTF-8")).split("\t", -1); |
| 118 | + } |
| 119 | + |
| 120 | + private static long bytesToLong(byte a, byte b, byte c, byte d) { |
| 121 | + return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff))); |
| 122 | + } |
| 123 | + |
| 124 | + private static int str2Ip(String ip) { |
| 125 | + String[] ss = ip.split("\\."); |
| 126 | + int a, b, c, d; |
| 127 | + a = Integer.parseInt(ss[0]); |
| 128 | + b = Integer.parseInt(ss[1]); |
| 129 | + c = Integer.parseInt(ss[2]); |
| 130 | + d = Integer.parseInt(ss[3]); |
| 131 | + return (a << 24) | (b << 16) | (c << 8) | d; |
| 132 | + } |
| 133 | + |
| 134 | + private static long ip2long(String ip) { |
| 135 | + return int2long(str2Ip(ip)); |
| 136 | + } |
| 137 | + |
| 138 | + private static long int2long(int i) { |
| 139 | + long l = i & 0x7fffffffL; |
| 140 | + if (i < 0) { |
| 141 | + l |= 0x080000000L; |
| 142 | + } |
| 143 | + return l; |
| 144 | + } |
| 145 | +} |
0 commit comments